Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cj-darius-lapunas opened this issue May 22, 2024 · 0 comments
Open

Comments

@cj-darius-lapunas
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant