'Python/pytest-html : How to add file links created from the test itself

In pytest-html, I run a final-test file which has multiple test functions. Each test function creates its own html report, which in the end I'm adding in the report of final-test as url. All reports I have placed in a common folder.

Now in final-report, the links are hard-coded to this folder where all files are. I have downloaded this report to my local machine, so when I click on URL, I get that hardcoded folder path where the url is trying to locate files, with the error [![enter image description here][1]][1]

How to correctly create this report where all files are locally created by the test on 1 machine and downloaded to other machines.

Adding code attempt:

#added in conftest.py

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin("html")
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, "extra", [])
    if report.when == "call":
        # always add url to report
        test_name = getattr(report, "nodeid", [])
        htmlfile = str(test_name.split("::")[1]) + ".html"
        extra.append(pytest_html.extras.url(os.getcwd() +'common_report_folder' + htmlfile))
        xfail = hasattr(report, "wasxfail")
        if (report.skipped and xfail) or (report.failed and not xfail):
            # only add additional html on failure
            extra.append(pytest_html.extras.html("<div>Additional HTML</div>"))
        report.extra = extra
#main_test file
import os

def test_1():
  ret = os.system("test 1 run command")
  assert ret==0

def test_2():
  ret = os.system("test 2 run command")
  assert ret==0
#command to run main_test file:

pytest --capture sys -rF -rP -rX main_test.py --html=report.html --self-contained-html 

# test 1 run command
pytest --capture sys -rF -rP -rX main_test.py --html=test_1.html --self-contained-html 

#copy test_1.html to common_report_folder
# test 2 run command
pytest --capture sys -rF -rP -rX main_test.py --html=test_2.html --self-contained-html 
#copy test_2.html to common_report_folder

#copy report.html to common_report_folder

All reports are generated as expected, just the links are hard-coded, so in final downloaded report, the linked report gives error. Not sure how to resolve [1]: https://i.stack.imgur.com/8wqMF.png



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source