pytest-html icon indicating copy to clipboard operation
pytest-html copied to clipboard

ANSI color codes are adding in extra section of report, are not converted in html report

Open heaventud opened this issue 5 years ago • 0 comments
trafficstars

Use case: I want to add custom section in pytest report and color it by red.

Implementation: conftest.py:

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    if (report.when == "call" or report.when == 'teardown') and report.failed:
        config = item.funcargs.get('config')
        if config is not None:
            artifacts_dir = config.get('artifacts_folder', '<ARTIFACTS_DIR>')
        else:
            config = sys.modules.get("config")
            artifacts_dir = getattr(config, 'results_dir', '<ARTIFACTS_DIR>')
        filepath = str(item.fspath).rsplit('tests')[1].lstrip('/')
        test_path = os.path.join(filepath, str(item.name))
        summary = """
Test failed: {test_path}
See artifacts in:
    (ansible run) {artifacts_folder}/{test_path}
        or   {artifacts_folder}/report.html
    (Teamcity run) Artifacts tab --> *.tar.gz/{test_path}
        or   Artifacts tab --> *.tar.gz/report.html
""".format(test_path=test_path, artifacts_folder=artifacts_dir)
        report.longrepr.addsection("Details", summary)

then html report looks like 2020-08-25_14-40-07

and ANSI codes were added as is.

heaventud avatar Aug 25 '20 11:08 heaventud