pytest-dev / pytest-html

Plugin for generating HTML reports for pytest results
Other
707 stars 236 forks source link

Extra attachment content kept in memory until end of session #814

Open cj-darius-lapunas opened 6 months ago

cj-darius-lapunas commented 6 months ago

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.