Pytest - 作用域执行顺序
-
如果有多个、不同作用域的需要执行,要弄清楚它们将执行的顺序;
-
pytest首先执行范围更高的fixtrue;
-
在请求fixture函数中,先执行较高作用域范围的fixture(session,package,module);
-
再执行在较低作用域的fixture(class,function);
-
test_py.py
import pytest
@pytest.fixture
def func(scope="function"):
print("Function")
@pytest.fixture(scope="class")
def cls():
print("Class")
@pytest.fixture(scope="module")
def mod():
print("Module")
@pytest.fixture(scope="package")
def pack():
print("Package")
@pytest.fixture(scope="session")
def sess():
print("\nSession")
class TestClass:
# 将作用域的顺序打乱,查看执行结果
def test_s1(self, cls, mod, sess, func, pack):
print("\n测试用例")
if __name__ == "__main__":
pytest.main("-s", "test_py.py")
-
执行顺序结果如图,按照作用域的高至低的顺序为:
- Seesion > Package > Module > Class > Function
标签:作用域,Fixture,fixture,pytest,Pytest,print,scope,def From: https://www.cnblogs.com/mzline/p/17443848.html