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

Question: How to fail a leaky test

Open tonybaloney opened this issue 3 years ago • 1 comments

Feature Request

I've got some scripts using memray that run some critical code 100,000 times, I use this in the following way:

mkdir -p .profiles
PYTHONMALLOC=malloc memray run -o .profiles/memray_logrecord.py.bin -f --native memray_logrecord.py
memray flamegraph --leaks -f .profiles/memray_logrecord.py.bin
python -m http.server --directory .profiles 8000

Then I manually look at the HTML report for leaky lines and raise bugs against them. Is there any way to automate this using this pytest package?

The test script is just something like:

def do():
    # setup... 

    for _ in range(100_000):
        ... # do code things

    # assert


if __name__ == "__main__":
    do()

tonybaloney avatar Oct 12 '22 00:10 tonybaloney

Any reason the https://pytest-memray.readthedocs.io/en/latest/usage.html#limit-memory does not help?

gaborbernat avatar Oct 12 '22 00:10 gaborbernat

@gaborbernat thanks for the link. Is there a way to set a default limit for all tests (without the need for adding this to all tests manually) - something like a memory base fence ...?

seebi avatar Oct 19 '22 18:10 seebi

There is not at the moment, but PR welcome. And you can also set it to your project via a pytest hook.

gaborbernat avatar Oct 19 '22 18:10 gaborbernat

Do you have a link to an example for this as well ? :-)

seebi avatar Oct 19 '22 19:10 seebi

You can use any of those hooks: https://docs.pytest.org/en/7.1.x/reference/reference.html

But I would personally recommend using an autouse fixture instead: https://docs.pytest.org/en/6.2.x/fixture.html#autouse-fixtures-fixtures-you-don-t-have-to-request

sarahmonod avatar Nov 09 '22 20:11 sarahmonod

I'm adding a new marker in #52 for checking memory leaks. Please @tonybaloney check if this would work for your use case.

pablogsal avatar Nov 16 '22 12:11 pablogsal