提交 82c462f3 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

generate test scripts from sample manual testcase

上级 86c49e2a
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
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) {
caseId := tc.Id
caseTitle := tc.Title
steps := []string{}
expects := []string{}
srcCode := []string{}
level := 1
for _, ts := range tc.Steps {
DealwithTestStep(ts, langType, level, &steps, &expects, &srcCode)
}
template := ReadAll("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,
steps *[]string, expects *[]string, srcCode *[]string) {
isGroup := ts.IsGroup
isCheckPoint := ts.IsCheckPoint
stepId := ts.Id
stepTitle := ts.Title
stepExpect := ts.Expect
// 处理steps
stepLine := ""
var stepType string
if isGroup {
stepType = "group"
} else {
stepType = "step"
}
stepLine += stepType + strconv.Itoa(stepId) + " // " + stepTitle
if isCheckPoint {
stepLine = "@" + stepLine
}
i := level
for {
stepLine = " " + stepLine
i--
if i == 0 {
break
}
}
stepLineSimple := strings.Replace(strings.TrimLeft(stepLine, " "), "//", "-", -1)
*steps = append(*steps, stepLine)
// 处理expects
if isCheckPoint {
expectsLine := ""
expectsLine = "# " + stepLineSimple + " " + stepExpect + "\n"
expectsLine += "<期望值字符串, 可以有多行>\n"
*expects = append(*expects, expectsLine)
}
// 处理srcCode
if isCheckPoint {
codeLine := ""
codeLine = "# " + stepLineSimple + "\n"
codeLine += "// 此处编写上述验证点代码 \n"
*srcCode = append(*srcCode, codeLine)
}
if isGroup {
for _, tsChild := range ts.Steps {
DealwithTestStep(tsChild, langType, level+1, steps, expects, srcCode)
}
}
}
func main() {
if len(os.Args) < 2 {
fmt.Println("usage: gen-project.go <path> <langType>")
}
caseFile, langType := os.Args[1], os.Args[2]
buf := ReadAll(caseFile)
var resp Response
json.Unmarshal(buf, &resp)
if resp.Code != 1 {
fmt.Println(string(buf))
return
}
for _, testCase := range resp.Cases {
DealwithTestCase(testCase, langType)
}
}
package main
import "fmt"
func main() {
fmt.Println("helloworld")
}
<?%s
<<<TC
title: with multi lines.
caseId: %d
steps:
@group1:
step1.1
step1.2
step1.3
@step2
step3
@step4
expects:
#
expect line1 point 1
expect line2 point 1
#
expect for point 2
#
expect for point 3
TC;
echo '#';
scriptsForPoint1();
echo '#';
scriptsForPoint2();
echo '#';
scriptsForPoint3();
?>
<?%s
<<<TC
title: %s
caseId: %d
steps:
%s
expects:
%s
TC;
// 此处编写操作步骤代码
%s
?>
......@@ -7,21 +7,24 @@
"steps": [
{
"id": 1000,
"action": "打开登录页面",
"title": "打开登录页面",
"expect": "",
"group": false
"isGroup": false,
"isCheckPoint": false
},
{
"id": 1010,
"action": "输入正确的用户名和密码",
"title": "输入正确的用户名和密码",
"expect": "",
"group": false
"isGroup": false,
"isCheckPoint": false
},
{
"id": 1020,
"action": "点击'登录'按钮",
"title": "点击'登录'按钮",
"expect": "用户成功登录",
"group": false
"isGroup": false,
"isCheckPoint": true
}
]
},
......@@ -31,45 +34,39 @@
"steps": [
{
"id": 2000,
"action": "连续输入3次错误的密码",
"expect": "",
"group": false
"title": "连续输入3次错误的密码"
},
{
"id": 2010,
"action": "第4次尝试登录",
"title": "第4次尝试登录",
"expect": "系统提示账号被锁定",
"group": false
"isCheckPoint": true
},
{
"id": 2100,
"action": "不连续输入3次错误的密码",
"expect": "",
"group": true,
"title": "不连续输入3次错误的密码",
"isGroup": true,
"steps": [
{
"id": 2101,
"action": "输入2次错误的密码",
"expect": "",
"group": true
"title": "输入2次错误的密码",
"isGroup": false
},
{
"id": 2102,
"action": "输入1次正确的密码",
"expect": "",
"group": true
"title": "输入1次正确的密码",
"isGroup": false
},
{
"id": 2103,
"action": "再输入1次错误的密码",
"expect": "",
"group": true
"title": "再输入1次错误的密码",
"isGroup": false
},
{
"id": 2104,
"action": "再输入1次正确的密码",
"title": "再输入1次正确的密码",
"expect": "登录成功,账号未被锁定",
"group": true
"isCheckPoint": true
}
]
}
......
<?php
<<<TC
title: 登录失败账号锁定策略
caseId: 200
steps:
step2000 // 连续输入3次错误的密码
@step2010 // 第4次尝试登录
group2100 // 不连续输入3次错误的密码
step2101 // 输入2次错误的密码
step2102 // 输入1次正确的密码
step2103 // 再输入1次错误的密码
@step2104 // 再输入1次正确的密码
expects:
# @step2010 - 第4次尝试登录 系统提示账号被锁定
<期望值字符串, 可以有多行>
# @step2104 - 再输入1次正确的密码 登录成功,账号未被锁定
<期望值字符串, 可以有多行>
TC;
// 此处编写操作步骤代码
# @step2010 - 第4次尝试登录
// 此处编写上述验证点代码
# @step2104 - 再输入1次正确的密码
// 此处编写上述验证点代码
?>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册