Pytest - Fixture(8) - 作用域依赖关系
Pytest - 作用域依赖关系
-
添加了
@pytest.fixture,如果fixture还想依赖其他fixture,需要用函数传参的方式:-
当一个函数请求另一个函数时,首先执行另一个函数。
-
如果函数
b请求函数a,函数a将首先执行,因为b依赖于a,没有a就无法运行。 -
即使
b不需要a的结果 ,它仍然可以请求a是否需要,确保在之后执行a。
test_py.py
import pytest @pytest.fixture() def a(): print("\n第一个Fixture:a") @pytest.fixture() def b(a): print("第二个Fixture:b") @pytest.fixture() def c(a, b): print("第三个Fixture:c") @pytest.fixture() def d(a): print("第四个Fixture:d") @pytest.fixture() def z(d, c): print("第五个Fixture:z") # 测试用例只调用 函数z def test_s1(z): print("\n用例test_s1:Fixture 相互调用") if __name__ == '__main__': pytest.main(['-s', 'test_py.py']) -
-
执行依赖关系结果如图:
-
单个fixture被多个fixture调用,只会执行一次;例如函数
a被函数b、c、d调用,但只被执行了一次; -
优先执行顺序靠前的fixture;例如
def z(d, c):,优先执行函数d; -
优先执行最先被调用的
fixture;例如函数a为最先被 函数d调用的,函数d为最先被函数z调用;

-

浙公网安备 33010602011771号