diff --git a/src/biz/check.go b/src/biz/check.go index e24a7f1714d0a72cea68f4152b1c3fdcbce39ec4..71c71f7cf95553b8dbb328f44565df1b3b562d1e 100644 --- a/src/biz/check.go +++ b/src/biz/check.go @@ -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) +} diff --git a/src/biz/run.go b/src/biz/run.go index ce89659891c53434f827c1b5e323b086cc96d06d..063c2aaf9f0decfc98c5331bf1e942d30b4f4d08 100644 --- a/src/biz/run.go +++ b/src/biz/run.go @@ -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) { diff --git a/src/model/test-model.go b/src/model/test-model.go index 0ecd347d97ee26243d1e0f7875401c84cc5e7da0..9a631b6bb4fdb8985f87667f1ff1db2d97482ac2 100644 --- a/src/model/test-model.go +++ b/src/model/test-model.go @@ -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 +} diff --git a/src/run.go b/src/run.go index 5f90d219b7294fc7682c7468b75295ae32b6cf12..6ac709cc75d716a03ddbd5a2243f2035fe9cd615 100644 --- a/src/run.go +++ b/src/run.go @@ -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) }