enhance(tests): 改进测试用例记录与覆盖率报告

- 在 ApiCase 中添加失败原因记录,提升测试结果的可读性
- 更新 CoverageReport 结构,增加总用例、通过用例和失败用例计数
- 优化 PrintCoverage 方法,支持详细输出未通过接口及其失败原因
- 更新 Swagger 生成逻辑,包含失败原因字段,增强调试信息
- 改进 SQL 错误计数逻辑,确保在测试模式下准确记录 SQL 错误
This commit is contained in:
2026-04-13 06:45:16 +08:00
parent 9a9b9c83ff
commit 3430bdec19
9 changed files with 516 additions and 151 deletions
+62 -48
View File
@@ -262,14 +262,62 @@ func (c *ApiCase) execute(method, desc string, expectStatus int, expectArgs ...i
t.Run(desc, func(t *testing.T) {
start := time.Now()
passed := true
var failReason, verifyError string
var body Map
defer func() {
record := TestRecord{
Path: c.api.path,
Desc: desc,
CaseName: desc,
Passed: passed,
Duration: time.Since(start),
Method: method,
Note: c.note,
VerifyError: verifyError,
FailReason: failReason,
}
if c.query != nil {
record.Query = c.query
}
if c.jsonBody != nil {
record.JsonBody = c.jsonBody
}
if c.formBody != nil {
record.FormBody = c.formBody
}
if c.fileContent != nil {
record.HasFile = true
record.FileField = c.fileField
}
if len(body) > 0 {
record.ResponseBody = body
}
expectResp := Map{"status": expectStatus}
if expectStatus != 0 {
if expectMsg != "" {
errorInfo := Map{"msg": expectMsg}
errorType := c.api.app.Config.GetMap("error").GetString(ObjToStr(expectStatus))
if errorType != "" {
errorInfo["type"] = errorType
}
expectResp["result"] = errorInfo
expectResp["error"] = errorInfo
}
} else if hasExpectResult {
expectResp["result"] = expectResult
}
record.ExpectResult = expectResp
c.api.app.collector.Add(record)
}()
c.api.app.Db.ResetTestSQLErrorCount()
req := c.buildRequest(method)
w := httptest.NewRecorder()
c.api.app.Application.ServeHTTP(w, req)
duration := time.Since(start)
body := Map{}
body = Map{}
if len(w.Body.Bytes()) > 0 {
if obj, err := JsonToObj(w.Body.String()); err == nil {
if m, ok := obj.(map[string]interface{}); ok {
@@ -285,9 +333,16 @@ func (c *ApiCase) execute(method, desc string, expectStatus int, expectArgs ...i
t: t,
}
passed := true
if sqlErrCount := c.api.app.Db.TestSQLErrorCount(); sqlErrCount > 0 {
passed = false
failReason = fmt.Sprintf("SQL错误: 请求处理中有 %d 次 SQL 错误", sqlErrCount)
sqlLog := c.api.app.Db.FlushTestLog()
t.Fatalf("请求处理中有 %d 次 SQL 错误:\n%s", sqlErrCount, sqlLog)
}
actualStatus := body.GetInt("status")
if actualStatus != expectStatus {
failReason = fmt.Sprintf("状态码不匹配: 期望 %d, 实际 %d", expectStatus, actualStatus)
t.Errorf("状态码不匹配: 期望 %d, 实际 %d\n响应: %s", expectStatus, actualStatus, w.Body.String())
passed = false
}
@@ -295,6 +350,7 @@ func (c *ApiCase) execute(method, desc string, expectStatus int, expectArgs ...i
if passed && expectMsg != "" {
actualMsg := resp.GetMsg()
if actualMsg != expectMsg {
failReason = fmt.Sprintf("消息不匹配: 期望 %q, 实际 %q", expectMsg, actualMsg)
t.Errorf("消息不匹配: 期望 %q, 实际 %q\n响应: %s", expectMsg, actualMsg, w.Body.String())
passed = false
}
@@ -302,17 +358,18 @@ func (c *ApiCase) execute(method, desc string, expectStatus int, expectArgs ...i
if passed && hasExpectResult {
if !validateShape(body["result"], expectResult, "result", t) {
failReason = "响应结构校验失败"
passed = false
}
}
c.api.lastResp = resp
var verifyError string
if passed && c.verifyFn != nil {
if err := c.verifyFn(c.api); err != nil {
t.Errorf("Verify 校验失败: %s", err.Error())
passed = false
verifyError = err.Error()
failReason = "Verify: " + err.Error()
}
}
@@ -323,49 +380,6 @@ func (c *ApiCase) execute(method, desc string, expectStatus int, expectArgs ...i
} else {
c.api.app.Db.FlushTestLog()
}
record := TestRecord{
Path: c.api.path,
Desc: desc,
CaseName: desc,
Passed: passed,
Duration: duration,
Method: method,
Note: c.note,
VerifyError: verifyError,
}
if c.query != nil {
record.Query = c.query
}
if c.jsonBody != nil {
record.JsonBody = c.jsonBody
}
if c.formBody != nil {
record.FormBody = c.formBody
}
if c.fileContent != nil {
record.HasFile = true
record.FileField = c.fileField
}
if len(body) > 0 {
record.ResponseBody = body
}
expectResp := Map{"status": expectStatus}
if expectStatus != 0 {
if expectMsg != "" {
errorInfo := Map{"msg": expectMsg}
errorType := c.api.app.Config.GetMap("error").GetString(ObjToStr(expectStatus))
if errorType != "" {
errorInfo["type"] = errorType
}
expectResp["result"] = errorInfo
expectResp["error"] = errorInfo
}
} else if hasExpectResult {
expectResp["result"] = expectResult
}
record.ExpectResult = expectResp
c.api.app.collector.Add(record)
})
if resp == nil {