I am using:
- pytest 6.2.4
- pytest-html 3.1.1
- pytest-json-report 1.2.4
- pytest-metadata 2.0.4
and I have the following simple tests in file test.py:
@pytest.mark.order(2)
@pytest.mark.parametrize('p_iteration', [x for x in range(3)])
def test_a(p_iteration):
logging.info('test_a')
if p_iteration == 2:
logging.debug(f'I am parameter: {p_iteration}')
assert True
else:
pass
@pytest.mark.order(1)
def test_b():
logging.info('test_b')
assert True
and I have in my conftest.py method:
def pytest_html_report_title(report):
report.title = "My very own title!"
@pytest.hookimpl(tryfirst=True)
def pytest_html_results_table_header(cells):
cells.insert(4, html.th('Custom Data Row'))
@pytest.hookimpl(tryfirst=True)
def pytest_html_results_table_row(report, cells):
test_id = report
custom_data = getattr(report, 'custom_data', test_id) # Access the custom data from the report
cells.insert(4, html.td(custom_data))
But when I wanted to add some custom data, e.g. link to the test being executed (not just the test name, like in Test column) I couldn't find the way, how to build up the link into the Custom Data Row column rows.
I also tried generate custom link into the report via hook method:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == "call":
extra.append(pytest_html.extras.url(content="https://github.com" + os.sep + str(item.fspath), name="Link to test code"))
extra.append(pytest_html.extras.text(content=str(report), name="Additional info"))
report.extra = extra
this will generate the link, but of course the absolute path to the test file.
Is it possible to get the path of the test file being executed from the root of the working directory? I would like to prepend it with the link to the Git repository, so I would have links to the Git repository directly inside HTML report.
This is how my HTML report looks like:

I looked at the official documentation for pytest-html: https://pytest-html.readthedocs.io/en/stable/user_guide.html but it didn't help to solve my problem.
don`t know if you managed to get over it but here is what had worked for me so anyway I can share.
My former attempt went with appending the docstring value (as per docs) but I think it is against the intended use. I had also variable within the tests method '_trid' that worked pretty well though it was additional boilerplate.
This example seems to be cleaner but I need to check how it will behave with additional fixtures.
This is a test example (not very good one but hey!):
This is part of conftest.py (majority of the code comes from pytest-html docs anyway):
I tried using extras (like you did) since it seemed the most organic way: extras.append(pytest_html.extras.url(f'{report._trid}')) However, for some reason it didn`t worked out properly.
Should output pytest-html report resulting pytest html report