httprunner3.x详细教程七(三种方式实现参数化数据驱动)
**欢迎加入测试交流群:自动化测试-夜行者(816489363)进行交流学习QAQ**--成都-阿木木
httprunner3.x支持三种格式的参数化数据,Parameters的入参说明如下:
""" parse parameters and generate cartesian product.
Args:
parameters (Dict) parameters: parameter name and value mapping
parameter value may be in three types:
(1) data list, e.g. ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
(2) call built-in parameterize function, "${parameterize(account.csv)}"
(3) call custom function in debugtalk.py, "${gen_app_version()}"
Returns:
list: cartesian product list
Examples:
>>> parameters = {
"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"],
"username-password": "${parameterize(account.csv)}",
"app_version": "${gen_app_version()}",
}
>>> parse_parameters(parameters)
"""
总结来说,支持三种入参,返回一个列表:
1、列表:["iOS/10.1", "iOS/10.2", "iOS/10.3"]
2、Parameterize类的回调,例如csv:${parameterize(account.csv)}
3、debugtalk.py的回调,${gen_app_version()}
在使用参数化之前,首先要导入pytest包,和httprunner的Parameters这个类
#!/user/bin/env python
# -*- coding: utf-8 -*-
"""
------------------------------------
@Project : interfaceDemo
@Time : 2020/8/20 13:47
@Auth : chineseluo
@Email : [email protected]
@File : demo_baidu_request_test.py
@IDE : PyCharm
------------------------------------
"""
import pytest
from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestBaiduRequestTestCase(HttpRunner):
@pytest.mark.parametrize("param", Parameters(
{"username": [111, 222, 333]}
))
def test_start(self, param):
super().test_start(param)
config = (
Config("get user list")
.variables(
**{
"username": "${get_username()}"
}
)
.base_url("https://www.baidu.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get info")
.get("/")
.with_params(**{"username": "$username"})
.validate()
.assert_equal("status_code", 200)
)
]
if __name__ == "__main__":
TestBaiduRequestTestCase().test_start()
如果使用过pytest的参数化的小伙伴一定不会陌生,@pytest.mark.parametrize()会先将param作为一个动态参数,传递给param,然后由httprunner在进行参数化,httprunner在pytest的parametrize上封装了一层,增加了csv及debugtalk.py参数化的支持。
下面演示三种参数化如何设置:
第一种列表:
#!/user/bin/env python
# -*- coding: utf-8 -*-
"""
------------------------------------
@Project : interfaceDemo
@Time : 2020/8/20 13:47
@Auth : chineseluo
@Email : [email protected]
@File : demo_baidu_request_test.py
@IDE : PyCharm
------------------------------------
"""
import pytest
from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestBaiduRequestTestCase(HttpRunner):
@pytest.mark.parametrize("param", Parameters(
{"username": [111, 222, 333]}
))
def test_start(self, param):
super().test_start(param)
config = (
Config("get user list")
.variables(
**{
"username": "${get_username()}"
}
)
.base_url("https://www.baidu.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get info")
.get("/")
.with_params(**{"username": "$username"})
.validate()
.assert_equal("status_code", 200)
)
]
if __name__ == "__main__":
TestBaiduRequestTestCase().test_start()
结果:
collecting ... collected 3 items
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param0]
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param1]
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param2]
第二种debugtalk.py的回调函数:
在debugtalk.py中定义一个函数,返回列表:
def get_username():
return [
{"username": "111111"},
{"username": "222222"},
{"username": "333333"},
{"username": "444444"},
{"username": "555555"},
{"username": "666666"},
]
在demo_baidu_request_test.py文件调用:
#!/user/bin/env python
# -*- coding: utf-8 -*-
"""
------------------------------------
@Project : interfaceDemo
@Time : 2020/8/20 13:47
@Auth : chineseluo
@Email : [email protected]
@File : demo_baidu_request_test.py
@IDE : PyCharm
------------------------------------
"""
import pytest
from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestBaiduRequestTestCase(HttpRunner):
@pytest.mark.parametrize("param", Parameters(
{"username": "${get_username()}"}
))
def test_start(self, param):
super().test_start(param)
config = (
Config("get user list")
.base_url("https://www.baidu.com")
.verify(False)
)
teststeps = [
Step(
RunRequest("get info")
.get("/")
.with_params(**{"username": "$username"})
.validate()
.assert_equal("status_code", 200)
)
]
if __name__ == "__main__":
TestBaiduRequestTestCase().test_start()
结果:
collecting ... collected 6 items
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param0]
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param1]
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param2]
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param3]
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param4]
demo_baidu_request_test.py::TestBaiduRequestTestCase::test_start[param5]
使用csv文件作为参数化输入时注意三点:
1,csv文件中的title要为变量名
2,csv映射的时候,参数名要以“-”分割,name和pwd使用的-进行分割
3,csv的路径要使用相对路径,不支持绝对路径不支持\\符号的路径
**欢迎加入测试交流群:自动化测试-夜行者(816489363)进行交流学习QAQ**