From 4304947abfcf771dcec3e5f675540709800f7221 Mon Sep 17 00:00:00 2001 From: Aaron Chen <462826@qq.com> Date: Tue, 9 Jul 2019 15:38:06 +0800 Subject: [PATCH] generate test scripts from sample manual testcase --- src/gen-project.go | 56 ++++++++++------------------------------- src/model/test-model.go | 20 +++++++++++++++ src/utils/utils.go | 12 +++++++++ 3 files changed, 45 insertions(+), 43 deletions(-) create mode 100644 src/model/test-model.go create mode 100644 src/utils/utils.go diff --git a/src/gen-project.go b/src/gen-project.go index 090bb307..f8ad5503 100644 --- a/src/gen-project.go +++ b/src/gen-project.go @@ -3,49 +3,20 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "model" "os" "strconv" "strings" + "utils" ) -type Response struct { - Code int - Cases []TestCase -} - -type TestCase struct { - Id int - Title string - Steps []TestStep -} - -type TestStep struct { - Id int - Title string - Steps []TestStep - - Expect string - IsGroup bool - IsCheckPoint bool -} - -func ReadAll(filePth string) []byte { - buf, err := ioutil.ReadFile(filePth) - if err != nil { - return nil - } - - return buf -} - -func DealwithTestCase(tc TestCase, langType string) { +func DealwithTestCase(tc model.TestCase, langType string) { caseId := tc.Id caseTitle := tc.Title - steps := []string{} - expects := []string{} - srcCode := []string{} + steps := make([]string, 0) + expects := make([]string, 0) + srcCode := make([]string, 0) level := 1 @@ -53,21 +24,21 @@ func DealwithTestCase(tc TestCase, langType string) { DealwithTestStep(ts, langType, level, &steps, &expects, &srcCode) } - template := ReadAll("xdoc/script-template.txt") + template := utils.ReadFile("xdoc/script-template.txt") content := string(fmt.Sprintf(string(template), langType, caseTitle, caseId, strings.Join(steps, "\n"), strings.Join(expects, "\n"), strings.Join(srcCode, "\n"))) fmt.Println(content) } -func DealwithTestStep(ts TestStep, langType string, level int, +func DealwithTestStep(ts model.TestStep, langType string, level int, steps *[]string, expects *[]string, srcCode *[]string) { isGroup := ts.IsGroup isCheckPoint := ts.IsCheckPoint stepId := ts.Id stepTitle := ts.Title - stepExpect := ts.Expect + // stepExpect := ts.Expect // 处理steps stepLine := "" @@ -94,12 +65,11 @@ func DealwithTestStep(ts TestStep, langType string, level int, *steps = append(*steps, stepLine) // 处理expects - if isCheckPoint { expectsLine := "" - expectsLine = "# " + stepLineSimple + " " + stepExpect + "\n" - expectsLine += "<期望值字符串, 可以有多行>\n" + expectsLine = "# \n" // + stepLineSimple + " " + stepExpect + "\n" + expectsLine += "<" + stepType + strconv.Itoa(stepId) + " 期望结果, 可以有多行>\n" *expects = append(*expects, expectsLine) } @@ -128,9 +98,9 @@ func main() { } caseFile, langType := os.Args[1], os.Args[2] - buf := ReadAll(caseFile) + buf := utils.ReadFile(caseFile) - var resp Response + var resp model.Response json.Unmarshal(buf, &resp) if resp.Code != 1 { diff --git a/src/model/test-model.go b/src/model/test-model.go new file mode 100644 index 00000000..0ecd347d --- /dev/null +++ b/src/model/test-model.go @@ -0,0 +1,20 @@ +package model + +type Response struct { + Code int + Cases []TestCase +} + +type TestCase struct { + Id int + Title string + Steps []TestStep +} + +type TestStep struct { + TestCase + + Expect string + IsGroup bool + IsCheckPoint bool +} diff --git a/src/utils/utils.go b/src/utils/utils.go new file mode 100644 index 00000000..6d313ad9 --- /dev/null +++ b/src/utils/utils.go @@ -0,0 +1,12 @@ +package utils + +import "io/ioutil" + +func ReadFile(filePth string) []byte { + buf, err := ioutil.ReadFile(filePth) + if err != nil { + return nil + } + + return buf +} -- GitLab