
正文
pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
conftest.py
import pytest @pytest.fixture(scope="class")
def class_auto():
print("")
print("class-begin")
yield
print("class-end")
test_autouse.py
import pytest
@pytest.mark.usefixtures("class_auto")
class TestClass(object):
@pytest.fixture(scope="function", autouse=True)
def funcion_auto(self):
print("begin")
yield
print("end")
def test_case1(self):
print("test_case1:")
assert 0 == 0
def test_case2(self):
print("test_case2:")
assert 0 == 0
执行命令
pytest -s test_autouse.py
执行结果:
注意:
1.fixture中的yield需要注意,不能缺
2.上面conftest.py中的fixture,也可以放在test_autouse.py中。效果是一样的








