report.go 3.1 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1 2 3
package testingService

import (
4
	"encoding/json"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
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
	"fmt"
	"github.com/easysoft/zentaoatf/src/model"
	"github.com/easysoft/zentaoatf/src/utils/common"
	constant "github.com/easysoft/zentaoatf/src/utils/const"
	"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"
	"strings"
	"time"
)

func Print(report model.TestReport, workDir string) {
	startSec := time.Unix(report.StartTime, 0)
	endSec := time.Unix(report.EndTime, 0)

	logs := make([]string, 0)

	logUtils.PrintAndLog(&logs, i118Utils.I118Prt.Sprintf("run_scripts", report.Path, report.Env))

	logUtils.PrintAndLog(&logs, i118Utils.I118Prt.Sprintf("time_from_to",
		startSec.Format("2006-01-02 15:04:05"), endSec.Format("2006-01-02 15:04:05"), report.Duration))

	logUtils.PrintAndLog(&logs, fmt.Sprintf("%s: %d", i118Utils.I118Prt.Sprintf("total"), report.Total))
	logUtils.PrintAndLogColorLn(&logs, fmt.Sprintf("  %s: %d", i118Utils.I118Prt.Sprintf("pass"), report.Pass), color.FgGreen)
	logUtils.PrintAndLogColorLn(&logs, fmt.Sprintf("  %s: %d", i118Utils.I118Prt.Sprintf("fail"), report.Fail), color.FgRed)
	logUtils.PrintAndLogColorLn(&logs, fmt.Sprintf("  %s: %d", i118Utils.I118Prt.Sprintf("skip"), report.Skip), color.FgYellow)

	for _, cs := range report.Cases {
35
		str := "\n %s %d-%d: %s"
36
		status := cs.Status
aaronchen2k2k's avatar
aaronchen2k2k 已提交
37 38
		statusColor := logUtils.ColoredStatus(status)

39
		logs = append(logs, fmt.Sprintf(str, status, cs.Id, cs.IdInTask, cs.Path))
40
		logUtils.Printt(fmt.Sprintf(str+"\n", statusColor, cs.Id, cs.IdInTask, cs.Path))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
41 42 43 44 45 46 47 48

		if len(cs.Steps) > 0 {
			count := 0
			for _, step := range cs.Steps {
				if count > 0 { // 空行
					logUtils.PrintAndLog(&logs, "")
				}

49
				str := "  %s %d:   %s"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
50 51 52
				status := commonUtils.BoolToPass(step.Status)
				statusColor := logUtils.ColoredStatus(status)

53 54
				logs = append(logs, fmt.Sprintf(str, status, step.Id, step.Name))
				logUtils.Printt(fmt.Sprintf(str, statusColor, step.Id, step.Name+"\n"))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
55 56 57 58 59 60 61 62 63

				count1 := 0
				for _, cp := range step.CheckPoints {
					if count1 > 0 { // 空行
						logUtils.PrintAndLog(&logs, "")
					}

					cpStatus := commonUtils.BoolToPass(step.Status)
					cpStatusColored := logUtils.ColoredStatus(cpStatus)
64 65 66
					logs = append(logs, fmt.Sprintf("    %s %d: %s", commonUtils.BoolToPass(cp.Status), cp.Numb,
						i118Utils.I118Prt.Sprintf("checkpoint")))
					logUtils.Printt(fmt.Sprintf("    %s %d: %s\n", cpStatusColored, cp.Numb, i118Utils.I118Prt.Sprintf("checkpoint")))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

					logUtils.PrintAndLog(&logs, fmt.Sprintf("      %s %s", i118Utils.I118Prt.Sprintf("expect_result"), cp.Expect))
					logUtils.PrintAndLog(&logs, fmt.Sprintf("      %s %s", i118Utils.I118Prt.Sprintf("actual_result"), cp.Actual))

					count1++
				}

				count++
			}
		} else {
			logUtils.PrintAndLog(&logs, "   "+i118Utils.I118Prt.Sprintf("no_checkpoints"))
		}
	}

	fileUtils.WriteFile(workDir+constant.LogDir+vari.RunDir+"result.txt", strings.Join(logs, "\n"))
82 83 84

	json, _ := json.Marshal(report)
	fileUtils.WriteFile(workDir+constant.LogDir+vari.RunDir+"result.json", string(json))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
85
}