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

use struct to save the logs

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