平常写自动化用例会写一些前置的fixture
操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传这个参数,会比较麻烦。
fixture里面有个参数autouse
,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了
调用fixture
三种方法
- 函数或类里面方法直接传
fixture
的函数参数名称 - 使用装饰器
@pytest.mark.usefixtures()
修饰 autouse=True
自动使用
用例传fixture参数
方法一:先定义start
功能,用例全部传start
参数,调用该功能
import pytest
@pytest.fixture(scope="function")
def start(request):
print('\n-----开始执行function----')
def test_a(start):
print("-------用例a执行-------")
class Test_aaa():
def test_01(self, start):
print('-----------用例01--------------')
def test_02(self):
print('-----------用例02--------------')
if __name__ == "__main__":
pytest.main(["-s", "test_fix01.py"])
运行结果:
est_fix01.py::test_a
-----开始执行function----
PASSED [ 33%]---