1+ pytest timing
2+ ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3+
4+ Slow unit tests can dis-incentivize writing or running unit tests. Luckily,
5+ there are many packages in the python ecosystem to help with this issue.
6+ This page lists some of the resources we find useful.
7+
18pytest-timeout
29===============================================================================
310
@@ -70,10 +77,10 @@ with the pytest annotation:
7077
7178 Setting a timeout to 0 seconds disables the timeout entirely.
7279
73- Other fun things you can do
74- -------------------------------------------------------------------------------
80+ Find slow tests
81+ ===============================================================================
7582
76- Even if you're not using this package, you can find slow-running tests in your
83+ Even if you're not using the timeout package, you can find slow-running tests in your
7784suite with the ``--durations `` flag.
7885
7986.. code-block :: console
@@ -83,4 +90,45 @@ suite with the ``--durations`` flag.
8390 This will run your test suite as normal, outputting a list of the 10 slowest
8491calls after the execution. Note that this is not the 10 slowest **tests **:
8592the setup and teardown will be counted separately so put slow, shared fixture
86- setup in a shared fixture!
93+ setup in a shared fixture!
94+
95+ Profile slow tests
96+ ===============================================================================
97+
98+ pytest-profiling
99+ -------------------------------------------------------------------------------
100+
101+ The `pytest-profiling <https://pypi.org/project/pytest-profiling/ >`__ package
102+ is a plugin for pytest that will profile selected test cases, and optionally
103+ provide a heat map of where your tests are spending their time.
104+
105+ .. code-block :: console
106+
107+ pip install pytest-profiling
108+
109+ To get a list of the functions that your code is spending the most time in, just
110+ pass the ``--profile `` argument to the pytest invocation. You can limit the test
111+ targets as you would with any other ``pytest `` execution using the ``-k <pattern> ``
112+ argument. e.g.
113+
114+ .. code-block :: console
115+
116+ pytest --profile -k test_cone_search_filters_correct_points
117+ pytest --profile-svg -k test_cone_search_filters_correct_points
118+
119+ py-spy
120+ -------------------------------------------------------------------------------
121+
122+ The `py-spy <https://github.com/benfred/py-spy >`__ package is a more general
123+ purpose sampling profiler for any python program. The python program you're
124+ profiling is the ``pytest `` execution.
125+
126+ Profiling the same target as above, using ``py-spy `` might look like:
127+
128+ .. code-block :: console
129+
130+ py-spy record -o profile.svg -- pytest -k test_cone_search_filters_correct_points
131+
132+ You will get a profiling flame chart saved to ``profile.svg ``. These are not as
133+ easy to read as some other flame charts, but they're better than sifting through the
134+ raw results!
0 commit comments