简体   繁体   English

在pytest中执行teardown_method后测试失败

[英]Failing a test after teardown_method has executed in pytest

I am trying to figure out how to write a pytest plugin that can be used to fail a test after it has been run (for anyone who wants more context, this is related to astropy/pytest-openfiles#28 ).我试图弄清楚如何编写一个 pytest 插件,该插件可用于在运行后使测试失败(对于任何想要更多上下文的人,这与astropy/pytest-openfiles#28 相关)。 Let's consider the following simple test file:让我们考虑以下简单的测试文件:

class TestClass:

    def setup_method(self, method):
        print("In setup_method")

    def teardown_method(self, method):
        print("In teardown_method")

    def test_simple(self):
        print("In test")

I can now define a conftest.py file that contains:我现在可以定义一个conftest.py文件,其中包含:

def pytest_runtest_teardown(item, nextitem):
    print("In pytest_runtest_teardown")

In this hook, I can carry out checks - for example in the case I'm interested in, we are checking for unclosed file handles.在这个钩子中,我可以执行检查 - 例如在我感兴趣的情况下,我们正在检查未关闭的文件句柄。 However, the issue is that this hook gets run after setup_method and after the test itself ( test_simple ) but before teardown_method :然而,问题是这个钩子在setup_method之后和测试本身( test_simple )之后但在teardown_method之前运行:

% pytest test.py -vs
...
collected 1 item                                                                                                                                      

test.py::TestClass::test_simple In setup_method
In test
PASSEDIn pytest_runtest_teardown
In teardown_method

I have considered instead using:我考虑过使用:

def pytest_runtest_makereport(item, call):

    if call.when != 'teardown':
        return

    print("In pytest_runtest_makereport")

which does get executed after teardown_method but at that point if I raise an exception pytest will output an INTERNALERROR and will still consider the test as successful.这确实在teardown_method之后被执行,但在那时如果我引发异常 pytest 将输出一个INTERNALERROR并且仍然会认为测试成功。

Is there any way to fail/error a test after teardown_method has been called?在调用teardown_method后,有没有办法使测试失败/出错?

After test_simple is over Pytest consider it done, everything from now own will be outside the test scope. test_simple结束后 Pytest 认为它完成了,从现在开始的所有内容都将超出测试范围。 You could add @pytest.fixture annotation to a function, this will provide you with pytest setup and teardown functionality.您可以将@pytest.fixture注释添加到函数中,这将为您提供pytest设置和拆卸功能。 The test itself will be considered ass passed, but not the all suite.测试本身将被视为通过,但不是所有套件。 TestClass will be marked as failure with the exception raised from teardown_method TestClass将被标记为失败,并引发来自teardown_method的异常

class TestClass:

    @pytest.fixture
    def run_for_test(self):
        self.setup_method()
        yield
        self.teardown_method()

    def setup_method(self, method):
        print("In setup_method")

    def teardown_method(self, method):
        print("In teardown_method")
        # just for the example
        if not method:
            raise AssertionError

    def test_simple(self):
        print("In test")

Output输出

In setup_method
.In test
In teardown_method
>           raise AssertionError
E           AssertionError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM