在 pytest-html 中,我运行了一个包含多个测试功能的最终测试文件。每个测试函数都会创建自己的 html 报告,最后我将其添加到 final-test 的报告中作为 url。我放置在一个公共文件夹中的所有报告。
现在在最终报告中,链接被硬编码到所有文件所在的文件夹。我已将此报告下载到我的本地计算机,因此当我单击 URL 时,我会得到该 url 试图查找文件的硬编码文件夹路径,错误 [![enter image description here][1]][1]
如何正确创建此报告,其中所有文件均由测试在 1 台机器上本地创建并下载到其他机器。
添加代码尝试:
#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
所有报告都按预期生成,只是链接是硬编码的,所以在最终下载的报告中,链接的报告会出错。不确定如何解决 [1]:https ://i.stack.imgur.com/8wqMF.png