pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function"
Posted moonpool
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function"相关的知识,希望对你有一定的参考价值。
conftest.py
import pytest @pytest.fixture(scope="class") def class_auto(): print("") print("class-begin") yield print("class-end")
test_autouse.py
1 import pytest 2 3 4 @pytest.mark.usefixtures("class_auto") 5 class TestClass(object): 6 7 @pytest.fixture(scope="function", autouse=True) 8 def funcion_auto(self): 9 print("begin") 10 yield 11 print("end") 12 13 def test_case1(self): 14 print("test_case1:") 15 assert 0 == 0 16 17 def test_case2(self): 18 print("test_case2:") 19 assert 0 == 0
执行命令
pytest -s test_autouse.py
执行结果:
注意:
1.fixture中的yield需要注意,不能缺
2.上面conftest.py中的fixture,也可以放在test_autouse.py中。效果是一样的
以上是关于pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function"的主要内容,如果未能解决你的问题,请参考以下文章
pytest---分布式执行用例(pytest-xdist)
30-pytest-重复执行用例-pytest-repeat
pytest文档40-pytest.ini配置用例查找规则(面试题)