diff --git a/README.md b/README.md index 4b58310..5910b1d 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ autotest extract -x "//title" -j '[ } ]' ``` +## Test Report +![report](https://github.com/vearne/autotest/raw/main/img/result_html.jpg) ## TODO * [x] 1) support utilizing the script language Lua to ascertain the conformity of HTTP responses with expectations. diff --git a/README_zh.md b/README_zh.md index 0eca281..8ca5628 100644 --- a/README_zh.md +++ b/README_zh.md @@ -92,6 +92,8 @@ autotest extract -x "//title" -j '[ } ]' ``` +## 测试报告 +![report](https://github.com/vearne/autotest/raw/main/img/result_html.jpg) ## TODO * [x] 1) 支持使用脚本语言Lua判断HTTP response是否符合预期 diff --git a/config_files/autotest.yml b/config_files/autotest.yml index dbb9e10..c1793a0 100644 --- a/config_files/autotest.yml +++ b/config_files/autotest.yml @@ -4,7 +4,7 @@ global: worker_num: 5 # default: true ignore_testcase_fail: true - debug: true + debug: false # Timeout setting for each request request_timeout: 5s @@ -20,8 +20,8 @@ global: http_rule_files: - "./config_files/my_http_api.yml" -grpc_rule_files: - - "./config_files/my_grpc_api.yml" +#grpc_rule_files: +# - "./config_files/my_grpc_api.yml" diff --git a/go.mod b/go.mod index 640d77f..550d00e 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/golang/protobuf v1.5.4 github.com/jhump/protoreflect v1.16.0 github.com/lianggaoqiang/progress v0.0.1 - github.com/spf13/cast v1.6.0 + github.com/spf13/cast v1.7.0 github.com/stretchr/testify v1.9.0 github.com/urfave/cli/v3 v3.0.0-alpha9 github.com/vearne/executor v0.0.3 diff --git a/go.sum b/go.sum index 95bfcfe..68fe9f7 100644 --- a/go.sum +++ b/go.sum @@ -61,6 +61,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w= +github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/img/result_html.jpg b/img/result_html.jpg new file mode 100644 index 0000000..614cf05 Binary files /dev/null and b/img/result_html.jpg differ diff --git a/internal/command/http_automate.go b/internal/command/http_automate.go index 1ec8d7c..52a3d18 100644 --- a/internal/command/http_automate.go +++ b/internal/command/http_automate.go @@ -3,6 +3,7 @@ package command import ( "context" "embed" + "fmt" "github.com/lianggaoqiang/progress" "github.com/vearne/autotest/internal/config" "github.com/vearne/autotest/internal/model" @@ -30,6 +31,14 @@ type ResultInfo struct { FailedCount int } +type CaseShow struct { + ID uint64 + Description string + State string + Reason string + Link string +} + func HttpAutomateTest(httpTestCases map[string][]*config.TestCaseHttp) { total := 0 for _, testcases := range httpTestCases { @@ -89,10 +98,17 @@ func GenReportFileHttp(testCasefilePath string, tcResultList []HttpTestCaseResul util.WriterCSV(reportPath, records) // 2. html file dirName := util.MD5(reportDirPath + name) + + var caseResults []CaseShow + for _, item := range tcResultList { + caseResults = append(caseResults, CaseShow{ID: item.ID, Description: item.Desc, + State: item.State.String(), Reason: item.Reason.String(), + Link: fmt.Sprintf("./%v/%v.html", dirName, item.ID)}) + } + obj := map[string]any{ "info": info, - "tcResultList": tcResultList, - "dirName": dirName, + "tcResultList": caseResults, } // index file err := RenderTpl(mytpl, "template/index.tpl", obj, filepath.Join(reportDirPath, name+".html")) @@ -127,6 +143,14 @@ func RenderTpl(fs embed.FS, key string, obj map[string]any, targetPath string) e slog.Error("template Parse, %v", err) return err } + dirPath := filepath.Dir(targetPath) + if !pathExists(dirPath) { + err = os.Mkdir(dirPath, 0755) + if err != nil { + return err + } + } + file, err := os.Create(targetPath) if err != nil { slog.Error("Create file, %v", err) @@ -136,6 +160,12 @@ func RenderTpl(fs embed.FS, key string, obj map[string]any, targetPath string) e return t.Execute(file, obj) } +func pathExists(path string) bool { + _, err := os.Stat(path) + // os.IsNotExist 判断错误是否为文件或目录不存在 + return !os.IsNotExist(err) +} + func HandleSingleFileHttp(workerNum int, filePath string) (*ResultInfo, []HttpTestCaseResult) { workerNum = min(workerNum, 10) testcases := resource.HttpTestCases[filePath] diff --git a/internal/command/http_call.go b/internal/command/http_call.go index 84a5f19..bc6acef 100644 --- a/internal/command/http_call.go +++ b/internal/command/http_call.go @@ -43,7 +43,7 @@ type HttpTestCaseResult struct { func (t *HttpTestCaseResult) ReqDetail() string { var builder strings.Builder - builder.WriteString(fmt.Sprintf("%v %v\n", t.Request.Method, t.Request.URL)) + builder.WriteString(fmt.Sprintf("%v %v\n", strings.ToUpper(t.Request.Method), t.Request.URL)) u, _ := url.Parse(t.Request.URL) builder.WriteString(fmt.Sprintf("HOST: %v\n", u.Host)) builder.WriteString("HEADERS:\n") @@ -76,7 +76,7 @@ func (t *HttpTestCaseResult) RespDetail() string { } var builder strings.Builder - builder.WriteString(fmt.Sprintf("STATUS: %v %v\n", t.Response.StatusCode, t.Response.Status)) + builder.WriteString(fmt.Sprintf("STATUS: %v\n", t.Response.Status())) builder.WriteString("HEADERS:\n") for key, values := range t.Response.Header() { builder.WriteString(fmt.Sprintf("%v: %v\n", key, strings.Join(values, ","))) diff --git a/internal/command/template/case.tpl b/internal/command/template/case.tpl index 880b943..caf64bc 100644 --- a/internal/command/template/case.tpl +++ b/internal/command/template/case.tpl @@ -17,19 +17,15 @@
- {{ .reqDetail }} -+
{{ .reqDetail }}
- {{ .respDetail }} -+
{{ .respDetail }}{{end}}
id | @@ -56,13 +56,17 @@|||||||||
---|---|---|---|---|---|---|---|---|---|
{{ .ID }} | -{{ .Desc }} | -{{ .State.String() }} | -{{ .Reason.String() }} | -View Details | +{{ $element.ID }} | +{{ $element.Description }} | +{{ $element.State }} | +{{ $element.Reason }} | ++ {{ if ne $element.Reason "ReasonDependentItemFailed" }} + View Details + {{ end }} + |