pytest xunit-style setup_function 和 teardown-function

pytest xunit-style setup_function and teardown-function

提问人:Shankar 提问时间:11/8/2023 最后编辑:Shankar 更新时间:11/8/2023 访问量:18

问:

我刚刚开始学习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     
pytest xunit

评论


答:

0赞 Diego Torres Milano 11/8/2023 #1

不知道你的示例来自哪里,但你需要这样的东西foo

@pytest.fixture(autouse=True)
def setup_teardown_function():
    print("set up is called")
    yield
    print("teradown is called..")