简体   繁体   中英

Is it possible to raise exception that fail (and not error) test in pytest finalizer?

Note : I'm using the version 2.7.3 of pytest with old-style for fixture. I have to use this version at the moment. I'm using python 2.7 on Linux.

I currently working on tests with pytest that required a check at the end of each test. To proceed I have wrote a fixture with a finalizer which perform the check but when I raise an exception I have a strange result.

Here is the code (edited for SO, you can copy/paste to test):

# copy/past to foobar.py and run `python -m pytest -v foobar.py`
import pytest

def check():
    assert 0

@pytest.fixture(autouse=True, scope='function')
def fixture_foobar(request):
    def tear_down():
        check()

    request.addfinalizer(tear_down)

def test_ok():
    assert 1

def test_nok():
    assert 0

So the code currently works but the output looks weird and wrong :

# test pass and finalizer raise exception
# expected result : foobar.py::test_ok FAILED
foobar.py::test_ok PASSED
foobar.py::test_ok ERROR


# test fail and finalizer raise exception
# expected result : foobar.py::test_nok FAILED
foobar.py::test_nok FAILED
foobar.py::test_nok ERROR

The case where finaliser do nothing stay good.

So if somebody have a idea how to fix it. May be I don't have the good approach but finalizer sounds like a good idea.

Thanks.

EDIT : I have the same comportment with the pytest_runtest_teardown hook.

So the solution was not so hard.

Pytest provide a lot of hooks and I have to use pytest_pyfunc_call which is run after test execution.

Solution :

# conftest.py
def check():
    assert 0

def pytest_pyfunc_call(pyfuncitem):
    check()


# test_foobar.py
def test_ok():
    assert 1

def test_nok():
    assert 0

And the result :

test_foobar.py::test_ok FAILED
test_foobar.py::test_nok FAILED

Improve it :

By default, if test failed and check() failed only the AssertionError of check() is traced. If I found a solution to ignore check() in case of test failure I'll update the answer. If you have a solution, you are welcome !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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