1.fixture重命名
格式 @pytest.fixture(name=xxx) # name为别名,testcase可以引用别名
说明 【命令行】pytest —fixtures test_xxx.py # pytest将列举所有可供测试使用的fixture(包含重命名name)
JsonData=myJsonData(filePath="../fixtureDemo/resource/my.json") # 数据与脚本分离
@pytest.fixture(params=JsonData,name="jsondata") # name为别名,testcase可以引用别名
def getJsonData(request):
return request.param
def test_hello2(jsondata):
assert add(jsondata["a"], jsondata["b"]) == jsondata["result"]
2.fixture的hook函数
1.作用
2.hook函数与参数化的区别
hook | 【命令行】动态传参 |
---|---|
参数化 | 不具备动态传参【参数化的数据是写死了的】 |
# conftest.py
def pytest_addoption(parser):
parser.addoption('--foo',action='store',default='cooling',help='') # 根据实际的“参数值”进行定义
@pytest.fixture()
def getFoo(request):
return request.config.getoption('--foo') # 根据实际的“参数值”进行定义
# -----------------------------------------------------------------------------
def test_hello1(getFoo):
print("目前:\t",getFoo)
3.fixture的cache
1.作用
2.应用场景
3.参数说明
- —lf参数:只会运行上次“失败的测试用例”
- —ff参数:首先运行上次“失败的用测”,然后运行其他测试用例
- —cache-show参数 : 回顾错误的信息【说明:—cache-show后面不能带任何值】
- —cache-clear参数 : 用于在测试用例开始之前清空cache的内容
格式 pytest —cache-clear 测试目录/测试文件等
4.pytest.ini配置cache位置
[pytest]
cache_dir = .pytest-cache2021 # 自定义pytest-cache的存放位置,【推荐】位置放在项目根目录