Skip to content

Commit

Permalink
Merge pull request #285 from lincc-frameworks/issue/264/smoke_test
Browse files Browse the repository at this point in the history
Add documentation for pytest-timeout.
  • Loading branch information
delucchi-cmu authored Sep 8, 2023
2 parents aa6df4a + 7faa4b3 commit 1445c3c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ We think it's really neat, and we hope you do to!
practices/precommit
practices/pypi
practices/unit_testing
practices/pytest_timeout
practices/sphinx
1 change: 1 addition & 0 deletions docs/practices/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Practices
* :doc:`pypi`
* :doc:`sphinx`
* :doc:`git-lfs`
* :doc:`pytest_timeout`
86 changes: 86 additions & 0 deletions docs/practices/pytest_timeout.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
pytest-timeout
===============================================================================

What is it? Why do it?
-------------------------------------------------------------------------------

`pytest-timeout <https://pypi.org/project/pytest-timeout/>`_ is an additional
pytest package that enforces the run length of your unit tests by causing
long-running tests to fail.

This is not a replacement for full-fledged benchmarking, but can be another
good signal that you're introducing a performance regression if tests that
were previously passing are now timing out.

See also:

* :doc:`ci_testing`
* :doc:`ci_benchmarking`

.. note::
Because this requires additional configuration, we do not enable it by
default, but are providing some instructions on enabling it

How to enable in your project
-------------------------------------------------------------------------------

Add the dependency in your ``pyproject.toml`` file, under the ``dev``
section, e.g.:

.. code-block::
[project.optional-dependencies]
dev = [
...
"pytest",
"pytest-timeout", # Used to test for code efficiency
...
]
Then add a default timeout (in seconds) lower down in the file:

.. code-block::
[tool.pytest.ini_options]
timeout = 1
testpaths = [
"tests",
]
With this configuration, any test taking longer than 1 second will time out
and fail.

Note that your local development environment is likely faster than the
machines you will be allocated for github actions, often by a factor of 2-5x.
If your tests take ~1 second locally, you should be setting your timeout to some
larger value so the failures are less common.

How to customize
-------------------------------------------------------------------------------

If you have some tests that you know will take longer than the default (e.g.
end-to-end or integration tests), you can set a longer timeout for those tests
with the pytest annotation:

.. code-block::
@pytest.mark.timeout(5)
def test_long_running_operation():
...
Setting a timeout to 0 seconds disables the timeout entirely.

Other fun things you can do
-------------------------------------------------------------------------------

Even if you're not using this package, you can find slow-running tests in your
suite with the ``--durations`` flag.

.. code-block:: bash
pytest --durations=10
This will run your test suite as normal, outputting a list of the 10 slowest
calls after the execution. Note that this is not the 10 slowest **tests**:
the setup and teardown will be counted separately so put slow, shared fixture
setup in a shared fixture!

0 comments on commit 1445c3c

Please sign in to comment.