我刚刚开始学习 pytest,我想知道为什么每个测试用例都没有调用 setup_function 和teardown_function
我的项目树如下所示
PytestProject
|
├───codebase
│ └───code.py
│ └───__init__.py
|
└───testcases
└───test_py_1.py
└───__init__.py
test_py_1.py的内容如下
import pytest
from codebase.code import *
x = 99
def setup_function(foo):
print("set up is called")
def teardown_function(foo):
print("teradown is called..")
@pytest.mark.parametrize("a,b",[(1,2),(99,100)])
@pytest.mark.groupone
def test_1(a,b):
assert sample(a,b) == -1
@pytest.mark.grouptwo
def test_2():
assert sample(3,2) == 1
def test_3():
with pytest.raises(TypeError) as ex:
assert sample("a",1)
@pytest.mark.skip(reason="xxxxxx")
def test_4():
pass
@pytest.mark.skipif(x==99,reason="x is 99")
def test_5():
pass
def test_6():
print("test6.....")
我尝试运行如下所示的 pytest 测试用例,但我没有看到每个测试用例后都调用 setup_function 和teardown_function
PytestProject\testcases> python -m pytest -v
========================================================================================================================== test session starts ==========================================================================================================================
platform win32 -- Python 3.7.0, pytest-7.4.3, pluggy-1.2.0 -- C:\Python\python.exe
cachedir: .pytest_cache
rootdir: PytestProject\testcases
collected 7 items
test_py_1.py::test_1[1-2] PASSED [ 14%]
test_py_1.py::test_1[99-100] PASSED [ 28%]
test_py_1.py::test_2 PASSED [ 42%]
test_py_1.py::test_3 PASSED [ 57%]
test_py_1.py::test_4 SKIPPED (xxxxxx) [ 71%]
test_py_1.py::test_5 SKIPPED (x is 99) [ 85%]
test_py_1.py::test_6 PASSED
不知道您的示例中的
foo
来自哪里,但您需要这样的东西
@pytest.fixture(autouse=True)
def setup_teardown_function():
print("set up is called")
yield
print("teradown is called..")