测试用例

先查看新建的工程中demo api的用例结构:

image-20210813130349029

接口自定义描述的主要内容包括:name、variables、request、base_url、validate等

name和request部分是必须的,request中的描述形式requests.request完全相同

发送请求

登录请求案例

首先登录要有接口文档,下面我们看下接口文档描述

image-20210818120401823

在工程中编写登录接口的描述信息

image-20210813131827810

1
2
3
4
5
6
7
name: 用户账户登录
request:
url: http://127.0.0.1:8988/v1/user/login
method: GET
json:
username: xiaohong
password: "123123"

以上完成了接口的描述,接下来就可以在终端执行该接口

image-20210813132019259

执行完成后,会生成一个报告,我们可以到工程的reports目录下,查看生成的报告:

image-20210813132128732

打开报告

image-20210813132235697

点击报告中的log-1,查看具体的日志:

image-20210813132432035

校验结果

从上面的案例,我们已经知道如何发送一个请求了,那么,API里面描述登录请求后,如何知道结果对不对呢?

validate校验器,校验(断言)返回结果,支持两种格式:

  • {"comparator_name": [check_item, expect_value]}
  • {"check": check_item, "comparator": comparator_name, "expect": expect_value}

常用的断言方法

我们可以在httprunner源码找到parser.py,查看:

image-20210813133602770

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def get_uniform_comparator(comparator):
""" convert comparator alias to uniform name
"""
if comparator in ["eq", "equals", "==", "is"]:
return "equals"
elif comparator in ["lt", "less_than"]:
return "less_than"
elif comparator in ["le", "less_than_or_equals"]:
return "less_than_or_equals"
elif comparator in ["gt", "greater_than"]:
return "greater_than"
elif comparator in ["ge", "greater_than_or_equals"]:
return "greater_than_or_equals"
elif comparator in ["ne", "not_equals"]:
return "not_equals"
elif comparator in ["str_eq", "string_equals"]:
return "string_equals"
elif comparator in ["len_eq", "length_equals", "count_eq"]:
return "length_equals"
elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
return "length_greater_than"
elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals",
"count_greater_than_or_equals"]:
return "length_greater_than_or_equals"
elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
return "length_less_than"
elif comparator in ["len_le", "count_le", "length_less_than_or_equals",
"count_less_than_or_equals"]:
return "length_less_than_or_equals"
else:
return comparator

有这么多的断言方式,以下罗列下常用的断言:

  • eq equals,判断实际结果和期望结果是否相等,可以用”eq”, “equals”, “==”, “is”
  • lt less_than, 判断实际结果小于期望结果 ,可以用 “lt”, “less_than”
  • le less_than_or_equals,判断实际结果小于等于期望结果 ,可以用 “le”, “less_than_or_equals”
  • gt greater_than,判断实际结果大于期望结果,可以用”gt”, “greater_than”
  • ge greater_than_or_equals,判断实际结果大于等于期望结果,可以用”ge”, “greater_than_or_equals”
  • ne not_equals, 判断实际结果和期望结果不相等,可以用”ne”, “not_equals”
  • str_eq string_equals 判断转字符串后对比 实际结果和期望结果是否相等,可以用”str_eq”, “string_equals”
  • len_eq length_equals 判断字符串或list长度,可以用”len_eq”, “length_equals”, “count_eq”
  • len_gt length_greater_than 判断实际结果的长度大于和期望结果,可以用”len_gt”, “count_gt”, “length_greater_than”, “count_greater_than”
  • len_ge length_greater_than_or_equals 实际结果的长度大于等于期望结果,可以用”len_ge”, “count_ge”, “length_greater_than_or_equals”, “count_greater_than_or_equals”
  • len_lt length_less_than 实际结果的长度小于期望结果,可以用”len_lt”, “count_lt”, “length_less_than”, “count_less_than”
  • len_le length_less_than_or_equals 实际结果的长度小于等于期望结果,可以用”len_le”, “count_le”, “length_less_than_or_equals”, “count_less_than_or_equals”

登录校验案例

先看下登录接口返回的结果:

image-20210813134353976

接下来,我们在接口的描述login_api.yml文件中,增加接口断言的部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
name: 用户账户登录
request:
url: http://127.0.0.1:8988/v1/user/login
method: GET
json:
username: xiaohong
password: "123123"
validate:
- eq:
- status_code
- 200
- eq:
- headers.Content-Type
- application/json;charset=UTF-8
- eq:
- content.code
- "00000"

也可以有如下的写法:

1
2
3
4
5
6
7
8
9
10
11
name: 用户账户登录
request:
url: http://127.0.0.1:8988/v1/user/login
method: GET
json:
username: xiaohong
password: "123123"
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json;charset=UTF-8]
- eq: [content.code, "00000"]

接下来再在终端执行该接口

image-20210813135105516

执行完成后,我们看到结果的都是绿色的,那么表示结果是通过的,接下来打开报告中的日志,就可以看到断言的结果:

image-20210813135249274

content取值