I have identified some long running pytest tests with
py.test --durations=10
I would like to instrument one of those tests now with something
To get cProfile
and line_profiler
to work with py.test
code, I did two things:
Extended the py.test test code with a call to pytest.main(), which made it executable with the python interpreter as the main driver:
# pytest_test.py:
@profile # for line_profiler only
def test_example():
x = 3**32
assert x == 1853020188851841
# for profiling with cProfile and line_profiler
import pytest
pytest.main(__file__)
Now you can run this test without py.test
as the main driver using other tools:
$ kernprof.py -l pytest_test.py
$ python -m line_profiler pytest_test.py.lprof
or
$ python -m cProfile pytest_test.py
To profile py.test-specific functions such as pytest_funcarg*()
with line_profiler
I split them in two to avoid confusion between py.test
and line_profiler
:
def pytest_funcarg__foo(request):
return foo(request)
@profile
def foo(request):
...
The same method works for memory_profiler.