/
ztfTestReport.go
115 lines (99 loc) · 3.69 KB
/
ztfTestReport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package testingService
import (
"encoding/json"
"fmt"
"github.com/easysoft/zentaoatf/src/model"
commonUtils "github.com/easysoft/zentaoatf/src/utils/common"
"github.com/easysoft/zentaoatf/src/utils/file"
i118Utils "github.com/easysoft/zentaoatf/src/utils/i118"
"github.com/easysoft/zentaoatf/src/utils/log"
"github.com/easysoft/zentaoatf/src/utils/vari"
"github.com/fatih/color"
"github.com/mattn/go-runewidth"
"strconv"
"strings"
"time"
)
func GenZTFTestReport(report model.TestReport, pathMaxWidth int) {
if len(report.FuncResult) == 0 {
return
}
// print failed case
failedCount := 0
failedCaseLines := make([]string, 0)
failedCaseLinesWithCheckpoint := make([]string, 0)
for _, cs := range report.FuncResult {
if cs.Status == "fail" {
if failedCount > 0 {
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, "")
}
failedCount++
path := cs.Path
lent := runewidth.StringWidth(path)
if pathMaxWidth > lent {
postFix := strings.Repeat(" ", pathMaxWidth-lent)
path += postFix
}
line := fmt.Sprintf("[%s] %d.%s", cs.Path, cs.Id, cs.Title)
failedCaseLines = append(failedCaseLines, line)
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, line)
if len(cs.Steps) > 0 {
stepNumb := 0
for _, step := range cs.Steps {
if step.Status {
continue
}
if stepNumb > 0 {
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, "")
}
stepNumb++
step.Id = strings.TrimRight(step.Id, ".")
status := i118Utils.Sprintf(commonUtils.BoolToPass(step.Status))
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, fmt.Sprintf("Step %s: %s", step.Id, status))
for idx1, cp := range step.CheckPoints {
//cpStatus := commonUtils.BoolToPass(step.Status)
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, fmt.Sprintf("[Expect] %s", cp.Expect))
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, fmt.Sprintf("[Actual] %s", cp.Actual))
if idx1 < len(step.CheckPoints)-1 {
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, "")
}
}
}
} else {
failedCaseLinesWithCheckpoint = append(failedCaseLinesWithCheckpoint, " "+i118Utils.Sprintf("no_checkpoints"))
}
}
}
if failedCount > 0 {
logUtils.ScreenAndResult("\n" + i118Utils.Sprintf("failed_scripts"))
logUtils.Screen(strings.Join(failedCaseLines, "\n"))
logUtils.Result(strings.Join(failedCaseLinesWithCheckpoint, "\n"))
}
secTag := ""
if vari.Config.Language == "en" && report.Duration > 1 {
secTag = "s"
}
// 生成统计行
fmtStr := "%d(%.1f%%) %s"
passStr := fmt.Sprintf(fmtStr, report.Pass, float32(report.Pass*100/report.Total), i118Utils.Sprintf("pass"))
failStr := fmt.Sprintf(fmtStr, report.Fail, float32(report.Fail*100/report.Total), i118Utils.Sprintf("fail"))
skipStr := fmt.Sprintf(fmtStr, report.Skip, float32(report.Skip*100/report.Total), i118Utils.Sprintf("skip"))
// 打印到结果文件
logUtils.Result("\n" + time.Now().Format("2006-01-02 15:04:05") + " " +
i118Utils.Sprintf("run_scripts",
report.Total, report.Duration, secTag,
passStr, failStr, skipStr,
" "+vari.LogDir+"result.txt ",
))
// 打印到屏幕
logUtils.Screen("\n" + time.Now().Format("2006-01-02 15:04:05") + " " +
i118Utils.Sprintf("run_scripts",
report.Total, report.Duration, secTag,
color.GreenString(passStr), color.RedString(failStr), color.YellowString(skipStr),
" "+vari.LogDir+"result.txt ",
))
//println("===" + vari.LogDir)
report.ProductId, _ = strconv.Atoi(vari.ProductId)
json, _ := json.Marshal(report)
fileUtils.WriteFile(vari.LogDir+"result.json", string(json))
}