pytest-dev / pytest-dev/pytest-html

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

Open
#332 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement feature
Dominant language
Python
Stars
779
Forks
260
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the custom report section shown in conftest.py and trace how that section is rendered in report.html. Reproduce the failure with the provided pytest hook and inspect the generated HTML for raw ANSI codes. Done means ANSI-colored custom sections render correctly in the HTML report without exposing escape sequences.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.