Skip to content

Commit db26444

Browse files
authored
Document more than just timeout (#550)
1 parent 9fb9b9d commit db26444

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

docs/practices/ci.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Continuous Integration
1010
ci_precommit
1111
code_coverage
1212
unit_testing
13-
pytest_timeout
13+
pytest_timing

docs/practices/overview.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Practices
1919
* :doc:`precommit`
2020
* :doc:`pypi`
2121
* :doc:`sphinx`
22-
* :doc:`pytest_timeout`
22+
* :doc:`pytest_timing`
2323

2424
Still have questions?
2525
--------------------------------
Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
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+
18
pytest-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
7784
suite 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
8491
calls after the execution. Note that this is not the 10 slowest **tests**:
8592
the 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

Comments
 (0)