Repository navigation
Replies: 2 comments 1 reply
|
If your analysts mean pytest's terminal status and summary, For example, in import pytest
from your_framework.errors import DUTSetupError
@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport(item, call):
report = yield
report.dut_setup_failure = (
report.when == "setup"
and report.failed
and call.excinfo is not None
and isinstance(call.excinfo.value, DUTSetupError)
)
return report
@pytest.hookimpl(tryfirst=True)
def pytest_report_teststatus(report):
if getattr(report, "dut_setup_failure", False):
return "failed", "F", "FAILED"
The important limit is JUnit XML and other report consumers: this changes terminal presentation, not the test phase. The built-in JUnit plugin still emits an I checked on Python 3.12.10 / pytest 9.0.3 using a simulated DUT exception, dependent fixture, normal setup error, teardown error, assertion failure and passing test. The terminal summary changed from References: Prepared with AI assistance; the behavior described above was checked locally. |
|
Preliminary work for external test blockers is done in pytest-external-blockers I haven't done the junitxml mapping there |
Uh oh!
There was an error while loading. Please reload this page.
Pytest has the really great approach that unhandeled exceptions or a call to
pytest.failin a fixture reports the test as error instead of failed.We're testing devices (DUT, device under test) with a pytest-based framework. And before each test, we use fixtures to ensure that the device is in a certain state. If somehow the DUT fails to enter the correct state due to a DUT-related problem (which will raise a specific exception in the fixture) the test analysts want to see the according test marked as failed and not as error as the root-cause is DUT-related and not environment-related.
So far, I could not find an easy solution for this. Can I do this via a hook function? If yes, which hook function would be the correct one?
I've tried
pytest_runtest_makereportas there I have acces to the call object after the test has run (or failed), but there I cannot altercall.resultand callingpytest.failinside this hook will raise an internal error and caues pytest to exit.Another approach was to just catch the exception in a try/except block, append some error infomation to the request.node object and continue fixture setup. Then we would evaluate that information in the
pytest_runtest_callhook and callpytest.failfrom there. This works, but the big downside here is that other fixtures that might depend on the failed fixture are still being setup. And as the device is already in a bad state, those fixture might have undefined behavior. In this case I would need an option to avoid setting up other fixtures (without needing to add checks in each and every other fixture).All reactions