1.config类
三个常用的方法:base_url:
variables:指定测试用例的公共变量,每个测试步骤都可以引用在步骤变量中设置的配置变量
export:指定导出测试用例会话变量,把变量暴露出来,设置为全局变量
创建login_test.py
from httprunner import HttpRunner,Config,Step,RunRequest,RunTestCase
class HttpRunner(object):
config=(Config("登陆用例")
.base_url("http://49.235.92.12:8201")
.variables({"user":"test","psw":"123456"})
.export(*["token"]))
teststeps=[Step(RunTestCase("step-login")
.post("/api/v1/login")
.with_json({"username":"test","password":"123456"})
.extract()
.with_jemspath("body.token","token")
.assert_equal("body.code",0,message="code")
)]
2.RunRequest测试步骤
创建login_info_test.py
from httprunner import HttpRunner,Config,Step,RunRequest,RunTestCase
class TestInfoCase(HttpRunner):
config=(Config("登陆用例")
.base_url("http://49.235.92.12:8201")
.variables(**{"user":"test","psw":"123456"})#**代表分开传入键值对
.export(*["token"]))
teststeps=[Step(RunRequest("step1-login")
.post("/api/v1/login")
.with_json({"username":"$user","password":"$psw"})
.extract()
.with_jemspath("body.token","token")
.assert_equal("body.code",0,message="code")
),Step(RunRequest("step2-updatainfo")
.post("/api/v1/updatainfo")
.with_json({"name":"test","sex":"M","age":20,"mail":"xxx@qq.com"})
.with_header(**{"Authorization":"Token $token")
.validate()
.with_jemspath("body.token","token")
.assert_equal("body.code",0,message="code")]
teststeps=[Step(RunRequest("step-getgoods")
.post("/api/v1/goods")
.with_params(**{"page":1,"size":2}) #参数在url?后面
.validate()
.with_jemspath("body.token","token")
.assert_equal("body.code",0,message="code")
with_data post请求Content-Type:application/x-www-from-urlencoded
with_json post请求Content-Type:application/json
from httprunner import HttpRunner,Config,Step,RunRequest,RunTestCase
class TestLoginV4Case(object):
config=(Config("登陆用例V4")
.base_url("http://49.235.92.12:8201")
.variables({"user":"test","psw":"123456"})
.export(*["token"]))
teststeps=[Step(RunTestCase("step-login")
.post("/api/v1/login")
.with_data({"username":"test","password":"123456"})
.validate()
.with_jemspath("body.token","token")
.assert_equal("body.code",0,message="code")
.assert_equal("status_code",0,200)
)]
extract 提取结果设置变量
with_jmespath(jmes_path:表达式,var_name:变量名称)
validate 校验结果
assert_断言(jmes_path:表达式,expected_value:期望结果,message:断言失败输出)
导入另一个测试用例
teststeps=[
Step(RunTestCase("step1-login")
.call(TestLoginCase) #指定引用的测试用例类,需要先导入这个类
.export(*["token"])),
Step(RunTestCase("step2-info")
.get("/api/v1/userinfo")
.with_header(**{"Authorization":"Token $token"),
.validate()
.assert_equal("body.code",0,message="code")
.assert_equal("status_code",0,200)
]