Table of contents
Object: a collection of key-value pairs (key:value)
Array: A set of values arranged in order
Literal: a single, indivisible value (number, string, boolean)
Read use case data in yml file
In the previous articles, we learned how to send data requests and how to process the results returned by the parsing interface. Next, let's learn how to manage test cases. Today we introduce the use of yml files for use case management, so first let's do it together Learn about YAML and its simple usage.
YAML
YAML (Yet Another Markup Language), you may have come into contact with this file format more or less at work. It is a programming language based on Unicode used to express data sequences. It is easy to read and interact with script languages. After the blogger's project is connected to k8s, basically all configurations use yml.
I think the biggest advantage of yml compared to json file format is that it supports comments, while json data definition does not support comments, and the writing format is stricter than yml. Let's take a look at the syntax of yml.
basic grammar
1. The yml file
uses indentation to represent hierarchical relationships. 2. Tabs are not allowed for indentation. Only spaces can be used
. 3. The number of spaces is not important, as long as elements of the same level are left aligned.
4. Case sensitive
. 5. The data format is , name: (space) value
Object: a collection of key-value pairs (key:value)
> 字符串不用使用双引号或单引号圈起来
> 双引号圈住时不会转义字符串中的特殊字符
> 单引号圈住时会转义字符串中的特殊字符
Array: A set of values arranged in order
> 数组名:
> -元素1
> -元素2
> 行内写法:
> 数组名:[元素1,元素2,元素3]
Literal: a single, indivisible value (number, string, boolean)
Test cases in yml format
Define yml file
Let's take login as an example to write a test case, save the following information to testCase.yml
, and our strategy will be completed:
Testsuit: 登录
description: 用户中心登录测试
commonparam:
username: name
passwd: pwd
TestCases:
-
url: /api/user/login
detail: 正常登陆
method: Get
data:
username: name
passwd: pwd
check:
- pr
- userId
-
url: /api/user/login
detail: 密码错误
method: post
data:
username: name
passwd: pwd
check:
- 密码错误
-
url: /api/user/login
detail: 必填参数未填,不填密码
method: post
data:
username: name
check:
- 必填参数未填
-
url: /api/user/login
detail: 密码错误
method: post
data:
username: name
passwd: pwd
check:
- 密码错误
-
url: /api/user/login
detail: 密码错误
method: post
data:
username: name
passwd: pwd
check:
- 密码错误
Next, let's learn how to read the use case information defined above.
Create structure
By observing the above yml file combined with the basic syntax of yml learned earlier, we can easily know that the specific data of TestCases is an array, and the data sent is also an array, so we can define the following structure to match The corresponding:
// TestSuit 测试用例结构体
type TestSuit struct {
testsuit string
description string
TestCases []struct {
URL string
Detail string
Method string
Check []string
Data struct {
Username string `json:"username"`
Passwd string `json:"passwd"`
}
}
Read use case data in yml file
What we use to parse YML this time is a third-party library. The acquisition method is: go get github.com/ghodss/yaml
, first the code:
func main() {
// 读取数据并解析到json中
data, _ := ioutil.ReadFile("testCase.yml")
t := TestSuit{}
err := yaml.Unmarshal(data, &t)
if err != nil {
fmt.Println(err.Error())
}
j, _ := json.Marshal(t.TestCases)
fmt.Println(string(j))
// 遍历yml文件中定义的test case信息
for i := 0; i < len(t.TestCases); i++ {
Path := t.TestCases[i].URL
Method := t.TestCases[i].Method
Param := t.TestCases[i].Data
CheckData := t.TestCases[i].Check
j, _ := json.Marshal(Param)
fmt.Println("==================我是华丽的分割线==================")
fmt.Println("请求参数:", string(j))
fmt.Println("请求路径:", Path)
upperMethod := strings.ToUpper(Method)
// 检验请求方式是否合法
fmt.Println(upperMethod)
fmt.Println(CheckMethod(upperMethod))
fmt.Println("校验数据:", CheckData)
// 根据不同的请求方式选择不同的请求URL
if upperMethod == "POST" {
_, body, _ := gorequest.New().Post(fmt.Sprintf("http://httpbin.org%s", Path)).
Send(fmt.Sprintf("%s", string(j))).
Set("Content-Type", "application/json").
End()
fmt.Println(body)
} else if upperMethod == "GET" {
fmt.Println(fmt.Sprintf("http://%s/%s", BaseURI, Path))
_, body, _ := gorequest.New().Get(fmt.Sprintf("http://%s%s", BaseURI, Path)).
Send(fmt.Sprintf("%s", string(j))).
Set("Content-Type", "application/json").
End()
fmt.Println(body)
}
}
}
// CheckMethod 校验请求的方法是否正确
func CheckMethod(method string) bool {
upperMethod := strings.ToUpper(method)
if upperMethod == "POST" || upperMethod == "GET" {
return true
}
return false
}
The attention in the code has been given relatively clearly, and this time it is used for http://httpbin.org
debugging. Careful friends may have noticed the last function, which verified the request method. Others such as the legality of the path can be Expanded for verification. Next, let's test whether the above code can really obtain the use case information and send the request correctly.
Commissioning
The result of running:
It should be noted that because the path we access is not monitored /api/user/login
in http://httpbin.org
the interface, the 404 returned by the interface actually indicates that the request is successful, but we cannot see the specific request data and URL, so we modify the path in the yml file to For http://httpbin.org
those who have already listened, /post
let’s see the results.
The modified data is:
test:
You can see that the parameters/path/IP and other information we requested are consistent with the content filled in our test case. At this point, our test cases and tests are completed
Summarize
- YAML
- Parse information in yml file
- Send data in yml format
How to obtain information
【Message 777】
Friends who want to get the source code and other tutorial materials, please like + comment + collect , three times in a row!
After three consecutive rounds , I will send you private messages one by one in the comment area~