Repository navigation
|
For infrastructure tests it is nice to have a link to the infrastructure documentation e.g. "how to fix this failure" in AssertionError message. Is it possible to somehow assign a text property with an url to the test function / test class and then have some hook which will take assertion message from the test and append the property to it if the property exists? |
Replies: 1 comment
|
Hi @atatarn I would attach the URL as a custom marker and add it to the test report, rather than trying to rewrite every There is no general public hook for safely rewriting the text of every arbitrary assertion failure. For example: # conftest.py
import pytest
def pytest_configure(config):
config.addinivalue_line(
"markers",
"fix_doc(url): documentation explaining how to fix this test",
)
@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport(item, call):
report = yield
if report.when == "call" and report.failed:
mark = item.get_closest_marker("fix_doc")
if mark and mark.args:
report.sections.append(
("How to fix this failure", str(mark.args[0]))
)
return reportThen: @pytest.mark.fix_doc("https://example.com/docs/how-to-fix-this")
def test_something():
assert False
The failure keeps its normal traceback/assertion explanation, and the documentation URL is added as a separate failure-report section, so future changes to the assertion itself cannot accidentally remove it. Please mark this answer as accepted if it helped, thank you. |
Hi @atatarn
I would attach the URL as a custom marker and add it to the test report, rather than trying to rewrite every
AssertionError.There is no general public hook for safely rewriting the text of every arbitrary assertion failure.
pytest_runtest_makereportis a better fit because it lets you augment the failure while preserving pytest's original assertion diagnostics.For example: