1、生成pytest的测试用例
(1)fiddler抓包,生成har文件,放到har目录中,File > Export Sessions > Selected Sessions > httparchive v1.2
录制脚本,如何选取你需要接口?
(1)先获取登录接口,登录就一个接口,找有入参的那个,其他别管。
(2)找生成文本的接口,就是你输入了内容那个接口。
(3)查下文本生成的接口,查找它的入参,发现有一个入参是前面是随机的,找到生成这个随机值的接口,就不用手动写入这个值了,确保顺利跑通,所以说这里只需要三个接口就可以实现这个功能了。
另外注意:如果入参跟前面的没关系,则只用创建文本的接口就可以了,其他的杂乱信息全部干掉。接口就是实现了一个功能。1个足矣。
以下是我提取的代码,可以跑通,为方便,我留了账号密码:
# NOTE: Generated By HttpRunner v3.1.4
# FROM: har\login_and_creat.har
"""
测试用例:跨脚本引用某个接口,每个用例都存在不少接口
测试步骤:
1种是转换的runquest()方法,1种是调用用例runtestcase()方法,具体参考源码的example
问题解决思路:
(1)不会的先参考下产品文档,再参考示例 (2)最后再输出csdn (3)给大家做个培训,巩固下
如何拿到douid的值?
通过with_jmespath("body.args.foo2", "foo3")方法将值作为变量输出,下面的测试步骤引用:$foo3
如何跨脚本使用变量?
exact()方法是一个py文件也就是用例的输出,其他的py文件可以在步骤中调用这个参数,具体参考request_with_testcase_reference_test示例。
"""
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseLoginAndCreat(HttpRunner):
config = Config("testcase description").verify(False)
teststeps = [
Step(
RunRequest("登录")
.post("https://mubu.com/api/login/submit")
.with_headers(
**{
"Host": "mubu.com",
"Connection": "keep-alive",
"Content-Length": "47",
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "https://mubu.com",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://mubu.com/login/password",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cookie": "data_unique_id=5af8ec14-0e06-45fb-948e-5852d15f593a; _ga=GA1.2.575825054.1617539185; language=en-US; country=US; _gid=GA1.2.1533022006.1617805441; Hm_lvt_4426cbb0486a79ea049b4ad52d81b504=1617619713,1617621135,1617625021,1617805446; reg_entrance=https%3A%2F%2Fmubu.com%2F; csrf_token=7251fd26-582d-40ab-94b9-1c18fa01838e; SESSION=b35ccba8-741a-4f9c-b591-32b5d429b22c; _gat=1; reg_prepareId=178acec5c13-178acec5c0e-41ad-8d2b-9795cc3d20d9; reg_focusId=7d2b4ded-9eb2-41ad-8d2b-178acec615a; SLARDAR_WEB_ID=0fe1264b-47de-4b0d-8272-c65571fd7483; Hm_lpvt_4426cbb0486a79ea049b4ad52d81b504=1617808876",
}
)
.with_cookies(
**{
"data_unique_id": "5af8ec14-0e06-45fb-948e-5852d15f593a",
"_ga": "GA1.2.575825054.1617539185",
"language": "en-US",
"country": "US",
"_gid": "GA1.2.1533022006.1617805441",
"Hm_lvt_4426cbb0486a79ea049b4ad52d81b504": "1617619713,1617621135,1617625021,1617805446",
"reg_entrance": "https%3A%2F%2Fmubu.com%2F",
"csrf_token": "7251fd26-582d-40ab-94b9-1c18fa01838e",
"SESSION": "b35ccba8-741a-4f9c-b591-32b5d429b22c",
"_gat": "1",
"reg_prepareId": "178acec5c13-178acec5c0e-41ad-8d2b-9795cc3d20d9",
"reg_focusId": "7d2b4ded-9eb2-41ad-8d2b-178acec615a",
"SLARDAR_WEB_ID": "0fe1264b-47de-4b0d-8272-c65571fd7483",
"Hm_lpvt_4426cbb0486a79ea049b4ad52d81b504": "1617808876",
}
)
.with_data(
{"phone": "18710748230", "password": "123456", "remember": "true"}
)
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json;charset=UTF-8")
.assert_equal("body.code", 0)
.assert_equal("body.msg", None)
),
Step(
RunRequest("创建文件,获取一个随机docId,并作为入参传入下一个接口")
.post("https://api2.mubu.com/v3/api/list/create_doc")
.with_headers(
**{
"Host": "api2.mubu.com",
"Connection": "keep-alive",
"Content-Length": "25",
"Accept": "application/json, text/plain, */*",
"Jwt-Token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhcHAiOiJtdWJ1Iiwic3ViIjoiMTE2MjM5NTEiLCJleHAiOjE2MjA0MDAxOTMsImlhdCI6MTYxNzgwODE5M30.cCLeXbGv8bBcbjAjywGUbjQA_vFdtR6RgazmAWnW9RETKno_y1qpMCPek0-y9UJnatbohr_wBQh0EkjfCCwNAw",
"Content-Type": "application/json;charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
"data-unique-id": "5af8ec14-0e06-45fb-948e-5852d15f593a",
"x-request-id": "3ea699c8-c82f-4fe9-9a0a-e7c611dec3d8",
"version": "3.0.0.21310",
"Origin": "https://mubu.com",
"Sec-Fetch-Site": "same-site",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://mubu.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
}
)
.with_json({"folderId": "0", "type": 0})
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json;charset=UTF-8")
.assert_equal("body.code", 0)
),
Step(
RunRequest("给创建的文件输入内容")
.post("https://api2.mubu.com/v3/api/refer/search_refers")
.with_headers(
**{
"Host": "api2.mubu.com",
"Connection": "keep-alive",
"Content-Length": "57",
"Accept": "application/json, text/plain, */*",
"Jwt-Token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhcHAiOiJtdWJ1Iiwic3ViIjoiMTE2MjM5NTEiLCJleHAiOjE2MjA0MDAxOTMsImlhdCI6MTYxNzgwODE5M30.cCLeXbGv8bBcbjAjywGUbjQA_vFdtR6RgazmAWnW9RETKno_y1qpMCPek0-y9UJnatbohr_wBQh0EkjfCCwNAw",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
"data-unique-id": "5af8ec14-0e06-45fb-948e-5852d15f593a",
"x-request-id": "278aa778-8931-4dcf-99fe-2ead0d51ff26",
"Content-Type": "application/json;charset=UTF-8",
"Origin": "https://mubu.com",
"Sec-Fetch-Site": "same-site",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://mubu.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
}
)
.with_json({"docId": "3PCZY46pa3_", "keywords": "zhangtong", "option": 1})
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json;charset=UTF-8")
.assert_equal("body.code", 0)
),
]
if __name__ == "__main__":
TestCaseLoginAndCreat().test_start()
(2)har2case test.har【默认生成pytest的用例】
har2case -2y har/xxx.har【生成yml格式的测试用例】
har2case -2j har/xxx.har【生成json格式的测试用例】
设置断言:
2、运行用例,生成报告:
(1)运行test_test.py
注意:执行用例的路径和生成报告的路径
C:\Users\Administrator\Desktop\api_test>hrun testcases/ test_test.py --html=reports/test.html
(2)运行所有用例
C:\Users\Administrator\Desktop\api_test>hrun testcases --html=reports/test.html