httprunner脚本

httpRunner脚本执行命令:hrun testcase/xxx.py --html=reports/test.html

 

post请求:


class TestCaseKtpLogin(HttpRunner):
   # 全局配置
config = (
Config("登录")
.verify(False)
.base_url("https://openapiv5.ketangpai.com") # 提取请求中的主域名
.variables(url_host="www.ketangpai.com")
.variables(host="openapiv5.ketangpai.com")
.variables(uagent="httpRunner 3.0 "+ str(os.getpid())) #声明全局变量
.variables(**{"email": "9xxxx","password": "xxxxxxx"}) # 声明全局变量
.export(*["token"]) #提取接口返回结果中的token
)

teststeps = [
Step(
RunRequest("//UserApi/login") # 步骤名称
.post("//UserApi/login") # post请求url
.with_headers( # 请求头
**{
"Host": "${host}", # 引用全局变量
"User-Agent": "${uagent}",
"Origin": "https://${url_host}"
}
)
.with_json( # 请求参数json串
{
"email": "${email}",
"password": "${password}",
"remember": "0",
"code": "",
"mobile": "",
"type": "login",
"reqtimestamp": "${get_timestamp()}", # 调用debugtalk.py文件中的方法
}
)
.extract() # 从请求结果中提取参数
.with_jmespath("body.data.token", "token") # 通过jmespath提取关联参数
.validate() # 开始校验
.assert_equal("status_code", 200) # 校验语句
.assert_equal('headers."Content-Type"', "application/json; charset=utf-8")
.assert_equal("body.message", "访问成功")
)
]

 

 

get请求:


class TestCaseKtpisTour(HttpRunner):
config = (
Config("路由")
.verify(False)
.base_url("https://openapiv5.ketangpai.com")
.variables(url_host="www.ketangpai.com")
.variables(host="openapiv5.ketangpai.com")
.variables(uagent="httpRunner 3.0 " + str(os.getpid()))
.variables(**{"email": "xxxxx", "password": "xxxxx"})
)

teststeps = [
Step(
RunTestCase("登录")
.with_variables(**{"email": "${email}", "password": "${password}"}) # 给登录用例传参,就近原则
.call(tl) # 引用登录的测试用例
# .export("body.data.token", "token") # 提取登录接口返回结果中的token
            .export(*["token"]) # 提取登录接口返回结果中的token
),
Step(
RunRequest("step2 isTour")
.get("//TourApi/isTourV2") # 发送get请求
.with_params(**{"version": "31", "courseid": "0"}) # 以params方式传参
.with_headers( # 传请求头
**{
"Host": "${host}",
"Accept": "application/json, text/plain, */*",
"User-Agent": "${uagent}",
"token": "${token}",
"Origin": "https://${url_host}",
"Referer": "https://${url_host}/"
}
)
.validate() # 开始校验
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json; charset=utf-8")
.assert_equal("body.status", 1)
.assert_equal("body.data.info", "该页面已经引导过")
),
]

参数化:

方式一:


@pytest.mark.parametrize(
"param",
Parameters(
{
"email": ["xxxxx"], # 参数结果:email、password是笛卡尔积
"password": ["xxxxx"]
}
)
)
class TestCaseKtpLogin(HttpRunner):

def test_start(self, param):
super().test_start(param)

  

方式二:

@pytest.mark.parametrize(
"param",
Parameters(
{
"email-password": [ # 实现一一对应
("xxxx@qq.com", "xxxx."),
("xxx@qq.com", "xxxx.")
]
}
)
)
class TestCaseKtpLogin(HttpRunner):

def test_start(self, param):
super().test_start(param)

 

方式三(未实验):

'accounts-pwd': '${parameterize(data.csv)}'

csv文件内容:

accounts,pwd

1111,2222

333,4444

 

方式四(未实验):

在debugtalk.py文件中写方法返回参数

def get_user():
    return [
        {'accounts': 'ss', 'pwd': '123456'},
        {'accounts': 'bull', 'pwd': ''}
    ]
posted @ 2022-04-18 21:41  狒狒桑  阅读(88)  评论(0编辑  收藏  举报