Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Vladimir Iakolev: Finding leaking tests with pytest

$
0
0

On one project we had a problem with leaking tests, and problem was so huge that some tests was leaking even for a few GB. We tried pytest-leaks, but it was a bit overkill and didn’t worked with our python version. So we wrote a little leak detector by ourselves.

First of all we got consumed RAM with psutil:

importosfrompsutilimportProcess_proc=Process(os.getpid())defget_consumed_ram():return_proc.memory_info().rss

Then created some log of ram usage, where nodeid is a special pytest representation of test, like tests/test_service.py::TestRemoteService::test_connection:

fromcollectionsimportnamedtupleSTART='START'END='END'ConsumedRamLogEntry=namedtuple('ConsumedRamLogEntry',('nodeid','on','consumed_ram'))consumed_ram_log=[]

And logged ram usage from pytest hooks, which we just put in conftest.py:

defpytest_runtest_setup(item):log_entry=ConsumedRamLogEntry(item.nodeid,START,get_consumed_ram())consumed_ram_log.append(log_entry)defpytest_runtest_teardown(item):log_entry=ConsumedRamLogEntry(item.nodeid,END,get_consumed_ram())consumed_ram_log.append(log_entry)

Pytest calls pytest_runtest_setup before each test, and pytest_runtest_teardown after.

And after all tests we print information about tests leaked more than allowed (10MB in our case) from pytest_terminal_summary hook:

fromitertoolsimportgroupbyLEAK_LIMIT=10*1024*1024defpytest_terminal_summary(terminalreporter):grouped=groupby(consumed_ram_log,lambdaentry:entry.nodeid)fornodeid,(start_entry,end_entry)ingrouped:leaked=end_entry.consumed_ram-start_entry.consumed_ramifleaked>LEAK_LIMIT:terminalreporter.write('LEAKED {}MB in {}\n'.format(leaked/1024/1024,nodeid))

So after running tests we got our leaking tests, like:

LEAKED 712MB in tests/test_service.py::TestRemoteService::test_connection

Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>