httprunner-pytest风格用例

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)             
]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值