Detect if the test passed in the instrument

I am debugging an intermittent test. To do this, I want to reset a lot of debugging information if the test fails. Fail-safe debugging material is a rather slow process that yields a lot of data, so I don’t want to do this for every test.

I am using pytest and autouse fixture output should work fine

@pytest.yield_fixture(scope="function", autouse=True) def dump_on_failue(request): prepare_debug_dump() yield if test_failed(request): debug_dump() 

The problem is that I cannot figure out how to determine if the test completed or not. There was already a question and even a note on the pytest website :

  if request.node.rep_setup.failed: print ("setting up a test failed!", request.node.nodeid) elif request.node.rep_setup.passed: if request.node.rep_call.failed: print ("executing test failed", request.node.nodeid) 

Unfortunately, this code no longer works. There are no rep_setup and rep_calls characters in the node object. I tried to query the request and the node object, but no luck.

Does anyone know how to determine if a test passed?

+5
source share
1 answer

There are no rep_setup and rep_calls characters in the node object.

The characters norep_setup and rep_calls still exist.

Add this code to your root conftest.py . It will check the skip / failure for each test function.

 import pytest @pytest.mark.tryfirst def pytest_runtest_makereport(item, call, __multicall__): rep = __multicall__.execute() setattr(item, "rep_" + rep.when, rep) return rep @pytest.fixture(scope='function', autouse=True) def test_debug_log(request): def test_result(): if request.node.rep_setup.failed: print ("setting up a test failed!", request.node.nodeid) elif request.node.rep_setup.passed: if request.node.rep_call.failed: print ("executing test failed", request.node.nodeid) request.addfinalizer(test_result) 
0
source

All Articles