pytest-dev / pytest-dev/pytest-html

Extra attachment content kept in memory until end of session

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

Nobody has claimed this yet.

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

Description

Issue

When attaching extra files, the file content is kept in memory even after files are written, effectively leaking memory. Mainly relevant if --self-contained-html is not used.

extras.append(pytest_html.extras.text('some very large content'))

This is a problem when we use large attachments, for example log files. With long test suites, memory usage constantly climbs. Based on my debugging, this is caused by extras being stored on the TestReport object.

Expected behavior

References to contents are dropped after files are written, letting GC claim them. Alternatively, the API accepts file paths and copies them into assets.

Workaround

Save files yourself, in a file under the report directory. Attach to report as a relative url.

Simplified example:

def example_test(pytestconfig, extras):
    report_dir = Path(pytestconfig.getoption('htmlpath')).parent
    (report_dir / 'extras').mkdir(parents=True, exist_ok=True)
    
    relative_path = Path('extras') / 'my_attachment.txt'
    absolute_path = report_dir / relative_path
    absolute_path.write_text('my large contents')

    extras.append(pytest_html.extras.url(str(relative_path), 'Text'))

This produces a nearly identical report, without the memory usage.

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 by tracing how extras are stored on the TestReport object and how attachment files are written when --self-contained-html is not used. Verify that attachment content references are released after writing while the generated report remains usable, including the large-content example described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.