HttpRunner-03:生成测试用例
1.前言
在编写测试用例之前,我们应该了解API的详细信息,使用一些抓包工具,比如:Charles Proxy、Fiddler、Chrome DevTools
等捕获网络会话流量。
例如,以postman-echo.com提供的API为例说明,我们利用网络抓包工具,捕获HTTP请求,导出HAR格式文件,使用har2case
生成pytest、JSON、YAML
格式的测试用例,我简单画了流程图来理解生成测试用例此过程。
2.捕获HTTP请求和响应
本例中,我以谷歌浏览器的DevTools作为抓包工具,Chrome的DevTools可以捕获所有流量并将其保存到HAR文件中。
谷歌浏览器发起请求:https://postman-echo.com/get?foo1=bar1&foo2=bar2
按下 F12
或 右键检查
,出现一个DevTools面板(如果你找不到任何请求链接,通过ctrl+R
快捷键重新请求一遍URL即可)
3.导出HAR文件har目录
从前面的操作步骤,我们拿到har文件,并导出到本地,我把har文件导出到demo项目下的har目录
,名为postman-echo.com.har
4.生成测试用例(pytest)
由于 HttpRunner 3.0.7,har2case默认情况下会将 HAR 文件转换为 pytest,并且非常建议以 pytest
格式而不是以前的YAML/JSON格式编写和维护测试用例。
执行har2case har/postman-echo.com.har
,生成pytest格式的测试用例,操作如下:
- 查看生成的测试用例
postman_echo_com_test.py
文件
# NOTE: Generated By HttpRunner v3.1.6
# FROM: har/postman-echo.com.har
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCasePostmanEchoCom(HttpRunner):
config = Config("testcase description").verify(False)
teststeps = [
Step(
RunRequest("/get")
.get("https://postman-echo.com/get")
.with_params(**{"foo1": "bar1", "foo2": "bar2"})
.with_headers(
**{
"pragma": "no-cache",
"cache-control": "no-cache",
"sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"dnt": "1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
}
)
.with_cookies(
**{
"sails.sid": "s%3AEFVfR_Ur7l24-eh6qebBWjlkxIgh0PNS.Yb0onNxWZuRdZ%2BNs%2BzfkO%2BgKA08zxh2Lary5oaRlZbE"
}
)
.validate()
.assert_equal("status_code", 200)
.assert_equal(
"body.url", "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
)
),
]
if __name__ == "__main__":
TestCasePostmanEchoCom().test_start()
5.生成测试用例(JSON)
执行har2case -2j har/postman-echo.com.har
,生成JSON格式的测试用例
- 查看生成的测试用例
postman-echo.com.json
{
"config": {
"name": "testcase description",
"variables": {},
"verify": false
},
"teststeps": [
{
"name": "/get",
"request": {
"url": "https://postman-echo.com/get",
"params": {
"foo1": "bar1",
"foo2": "bar2"
},
"method": "GET",
"cookies": {
"sails.sid": "s%3AEFVfR_Ur7l24-eh6qebBWjlkxIgh0PNS.Yb0onNxWZuRdZ%2BNs%2BzfkO%2BgKA08zxh2Lary5oaRlZbE"
},
"headers": {
"pragma": "no-cache",
"cache-control": "no-cache",
"sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Google Chrome\";v=\"96\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"dnt": "1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8"
}
},
"validate": [
{
"eq": [
"status_code",
200
]
},
{
"eq": [
"body.url",
"https://postman-echo.com/get?foo1=bar1&foo2=bar2"
]
}
]
}
]
}
6.生成测试用例(YAML)
执行har2case -2y har/postman-echo.com.har
,生成YAML格式测试用例
- 查看生成的测试用例
postman-echo.com.yml
config:
name: testcase description
variables: {}
verify: false
teststeps:
- name: /get
request:
cookies:
sails.sid: s%3AEFVfR_Ur7l24-eh6qebBWjlkxIgh0PNS.Yb0onNxWZuRdZ%2BNs%2BzfkO%2BgKA08zxh2Lary5oaRlZbE
headers:
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9,en;q=0.8
cache-control: no-cache
dnt: '1'
pragma: no-cache
sec-ch-ua: '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"'
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: '"Windows"'
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: '1'
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
method: GET
params:
foo1: bar1
foo2: bar2
url: https://postman-echo.com/get
validate:
- eq:
- status_code
- 200
- eq:
- body.url
- https://postman-echo.com/get?foo1=bar1&foo2=bar