fixture的优势
fixture是在测试函数运行前后,由pytest执行的外壳函数。fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集,配置测试前系统的初始状态,为批量测试提供数据源,等等。命名方式灵活,不局限于 setup 和teardown 这几个命名
fixture调用方式
1、使用fixture名字作为参数
import pytest @pytest.fixture() def login(): print("这是一个登录方法") def test_1(login): print("test01需要登录") if __name__ == '__main__': pytest.main()
============================= test session starts =============================
collecting ... collected 1 item
fixture.py::test_1
============================== 1 passed in 0.09s ==============================
这是一个登录方法
PASSED [100%]test01需要登录
2、使用@pytest.mark.usefixtures('fixture') 装饰器
import pytest @pytest.fixture() def login(): print("这是一个登录方法")
@pytest.mark.usefixtures('login') def test_1(): print("test01需要登录") def test_2(): print("test02需要登录")============================= test session starts ============================= collecting ... collected 3 items fixture.py::test_3 fixture.py::test_1 fixture.py::test_2 ============================== 3 passed in 0.09s ============================== PASSED [ 33%]test03不需要登录 这是一个登录方法 PASSED [ 66%]test01需要登录 PASSED [100%]test02需要登录
usefixtures与传fixture区别
如果fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。
当fixture需要用到return出来的参数时,只能讲参数名称直接当参数传入,不需要用到return出来的参数时,两种方式都可以
3、单个fixture传递测试数据
import pytest @pytest.fixture() def fixturefun(): return (1,2,3,4) def test_1(fixturefun): assert fixturefun[0] == 1
============================= test session starts =============================
collecting ... collected 1 item
fixture.py::test_1
============================== 1 passed in 0.09s ==============================
PASSED [100%]
4、多个fixture传递测试数据
import pytest @pytest.fixture() def fixturefun1(): return (1,2,3,4) @pytest.fixture() def fixturefun2(): return (1,2,3,4) def test_1(fixturefun1,fixturefun2): assert fixturefun1[0] == fixturefun2[0]
============================= test session starts =============================
collecting ... collected 1 item
fixture.py::test_1
============================== 1 passed in 0.09s ==============================
PASSED [100%]
Process finished with exit code 0
5、Fixture的相互调用
import pytest
@pytest.fixture()
def fixturefun():
print("第一层fixture")
a = "zhangsan"
return a
@pytest.fixture()
def login(fixturefun):
print("第二层fixture")
class Test_Class1():
def test_two(self,login):
print("直接使用第二层fixture,返回值为{}".format(login))
def test_three(self,fixturefun):
print("直接使用第一层fixture,返回值为{}".format(fixturefun))
if __name__ == '__main__':
pytest.main()
============================= test session starts =============================
collecting ... collected 2 items
test1.py::Test_Class1::test_two
test1.py::Test_Class1::test_three
============================== 2 passed in 0.09s ==============================
第一层fixture #fixture互相调用时会先找到所有fixture再一层一层往上找
第二层fixture
PASSED [ 50%]直接使用第二层fixture,返回值为None #当有多层调用,直接被调用的fixture,不会将上一层的返回值自动回传
第一层fixture
PASSED [100%]直接使用第一层fixture,返回值为zhangsan
注:
- 即使fixture之间支持相互调用,但普通函数直接使用fixture是不支持的,一定是在测试函数内调用才会逐级调用生效
- 有多层fixture调用时,最先执行的是最后一层fixture,而不是先执行传入测试函数的fixture
- 上层fixture的值不会自动return,这里就类似函数相互调用一样的逻辑