提交 0c8ba34f 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

use struct to save the logs

上级 67d5c5d7
......@@ -2,22 +2,16 @@ package biz
import (
"fmt"
"model"
"regexp"
"sort"
"strconv"
"strings"
"time"
"utils"
)
func CheckResults(dir string, langType string,
summaryMap *map[string]interface{}, resultMap *map[string]bool, checkpointMap *map[string][]string) {
func CheckResults(dir string, langType string, report *model.TestReport) {
fmt.Printf("\n=== Begin to analyse test result ===\n\n")
(*summaryMap)["pass"] = 0
(*summaryMap)["fail"] = 0
(*summaryMap)["total"] = 0
scriptFiles, _ := utils.GetAllFiles(dir, langType)
for _, scriptFile := range scriptFiles {
......@@ -29,16 +23,15 @@ func CheckResults(dir string, langType string,
expectContent = strings.Trim(expectContent, "\n")
logContent = strings.Trim(logContent, "\n")
Compare(scriptFile, expectContent, logContent, summaryMap, resultMap, checkpointMap)
Compare(scriptFile, expectContent, logContent, report)
}
}
func Compare(scriptFile string, expectContent string, logContent string,
summaryMap *map[string]interface{}, resultMap *map[string]bool, checkpointMap *map[string][]string) {
func Compare(scriptFile string, expectContent string, logContent string, report *model.TestReport) {
expectArr := strings.Split(expectContent, "\n")
logArr := strings.Split(logContent, "\n")
checkpoints := make([]string, 0)
checkpoints := make([]model.CheckPointLog, 0)
result := true
......@@ -60,69 +53,58 @@ func Compare(scriptFile string, expectContent string, logContent string,
result = false
}
checkpoints = append(checkpoints, "Line "+strconv.Itoa(numb+1)+": "+strconv.FormatBool(result))
checkpoints = append(checkpoints, "Expect "+line)
checkpoints = append(checkpoints, "Actual "+log)
cp := model.CheckPointLog{Numb: numb + 1, Status: result, Expect: line, Actual: log}
checkpoints = append(checkpoints, cp)
}
if !result {
(*summaryMap)["fail"] = (*summaryMap)["fail"].(int) + 1
report.Fail = report.Fail + 1
} else {
(*summaryMap)["pass"] = (*summaryMap)["pass"].(int) + 1
report.Pass = report.Pass + 1
}
(*summaryMap)["total"] = (*summaryMap)["total"].(int) + 1
report.Total = report.Total + 1
(*resultMap)[scriptFile] = result
(*checkpointMap)[scriptFile] = checkpoints
cs := model.CaseLog{Path: scriptFile, Status: result, CheckPoints: checkpoints}
report.Cases = append(report.Cases, cs)
}
func Print(summaryMap map[string]interface{}, resultMap map[string]bool, checkpointMap map[string][]string, workDir string) {
startSec := time.Unix(summaryMap["startTime"].(int64), 0)
endSec := time.Unix(summaryMap["endTime"].(int64), 0)
func Print(report model.TestReport, workDir string) {
startSec := time.Unix(report.StartTime, 0)
endSec := time.Unix(report.EndTime, 0)
var log string
logs := make([]string, 0)
log = fmt.Sprintf("From %s to %s, duration %d sec",
startSec.Format("2006-01-02 15:04:05"),
endSec.Format("2006-01-02 15:04:05"),
summaryMap["duration"])
logs = append(logs, log)
fmt.Println(log)
log = fmt.Sprintf("Total: %d, Fail: %d, Pass: %d",
summaryMap["total"], summaryMap["pass"], summaryMap["fail"])
logs = append(logs, log)
fmt.Println(log)
var sslice []string
for key, _ := range resultMap {
sslice = append(sslice, key)
}
sort.Strings(sslice)
PrintAndLog(&logs, fmt.Sprintf("From %s to %s, duration %d sec",
startSec.Format("2006-01-02 15:04:05"), endSec.Format("2006-01-02 15:04:05"), report.Duration))
for _, script := range sslice {
count := 0
log = fmt.Sprintf("\n%s: %t", script, resultMap[script])
logs = append(logs, log)
fmt.Println(log)
PrintAndLog(&logs, fmt.Sprintf("Total: %d, Fail: %d, Pass: %d",
report.Total, report.Pass, report.Fail))
checkpoints := checkpointMap[script]
for _, cs := range report.Cases {
PrintAndLog(&logs, fmt.Sprintf("\n%s: %t", cs.Path, cs.Status))
for _, line := range checkpoints {
if count > 0 && strings.Index(line, "Line ") > -1 {
logs = append(logs, "")
fmt.Println("")
}
if len(cs.CheckPoints) > 0 {
count := 0
for _, cp := range cs.CheckPoints {
if count > 0 {
PrintAndLog(&logs, "")
}
log = fmt.Sprintf(" %s", line)
logs = append(logs, log)
fmt.Println(log)
PrintAndLog(&logs, fmt.Sprintf(" Line %d: %t", cp.Numb, cp.Status))
PrintAndLog(&logs, fmt.Sprintf(" Expect %s", cp.Expect))
PrintAndLog(&logs, fmt.Sprintf(" Actual %s", cp.Actual))
count++
count++
}
} else {
PrintAndLog(&logs, " No checkpoints")
}
}
utils.WriteFile(workDir+"/logs/report.log", strings.Join(logs, "\n"))
}
func PrintAndLog(logs *[]string, str string) {
*logs = append(*logs, str)
fmt.Println(str)
}
......@@ -2,17 +2,18 @@ package biz
import (
"fmt"
"model"
"runtime"
"strings"
"time"
"utils"
)
func RunScripts(files []string, dir string, langType string, summaryMap *map[string]interface{}) {
func RunScripts(files []string, dir string, langType string, report *model.TestReport) {
fmt.Println("=== Begin to run test scripts ===")
startTime := time.Now().Unix()
(*summaryMap)["startTime"] = startTime
report.StartTime = startTime
for _, file := range files {
RunScript(file, langType, dir)
......@@ -20,8 +21,9 @@ func RunScripts(files []string, dir string, langType string, summaryMap *map[str
endTime := time.Now().Unix()
secs := endTime - startTime
(*summaryMap)["endTime"] = startTime
(*summaryMap)["duration"] = secs
report.EndTime = startTime
report.Duration = secs
}
func RunScript(file string, langType string, dir string) {
......
......@@ -18,3 +18,27 @@ type TestStep struct {
IsGroup bool
IsCheckPoint bool
}
type TestReport struct {
Pass int
Fail int
Total int
StartTime int64
EndTime int64
Duration int64
Cases []CaseLog
}
type CaseLog struct {
Numb int
Path string
Status bool
CheckPoints []CheckPointLog
}
type CheckPointLog struct {
Numb int
Expect string
Actual string
Status bool
}
......@@ -3,6 +3,7 @@ package main
import (
"biz"
"flag"
"model"
"os"
"utils"
)
......@@ -20,11 +21,14 @@ func main() {
files, _ := utils.GetAllFiles(*workDir, *langType)
summaryMap := make(map[string]interface{})
biz.RunScripts(files, *workDir, *langType, &summaryMap)
var report model.TestReport
report.Pass = 0
report.Fail = 0
report.Total = 0
report.Cases = make([]model.CaseLog, 0)
resultMap := make(map[string]bool)
checkpointMap := make(map[string][]string)
biz.CheckResults(*workDir, *langType, &summaryMap, &resultMap, &checkpointMap)
biz.Print(summaryMap, resultMap, checkpointMap, *workDir)
biz.RunScripts(files, *workDir, *langType, &report)
biz.CheckResults(*workDir, *langType, &report)
biz.Print(report, *workDir)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册