• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

pytest_timeout.egg-info/H03-May-2022-362260

LICENSEH A D20-Nov-20191.1 KiB2317

MANIFEST.inH A D20-Nov-2019107 65

PKG-INFOH A D15-Jul-202014.1 KiB362260

README.rstH A D15-Jul-202010.5 KiB337236

failure_demo.pyH A D14-Jun-2020683 3824

pytest_timeout.pyH A D15-Jul-202014.4 KiB460376

setup.cfgH A D15-Jul-202067 85

setup.pyH A D15-Jul-20201.3 KiB4035

test_pytest_timeout.pyH A D15-Jul-202011.2 KiB486375

tox.iniH A D15-Jun-2020476 2721

README.rst

1==============
2pytest-timeout
3==============
4
5|python| |version| |anaconda| |ci|
6
7.. |version| image:: https://img.shields.io/pypi/v/pytest-timeout.svg
8  :target: https://pypi.python.org/pypi/pytest-timeout
9
10.. |anaconda| image:: https://img.shields.io/conda/vn/conda-forge/pytest-timeout.svg
11  :target: https://anaconda.org/conda-forge/pytest-timeout
12
13.. |ci| image:: https://github.com/pytest-dev/pytest-timeout/workflows/build/badge.svg
14  :target: https://github.com/pytest-dev/pytest-timeout/actions
15
16.. |python| image:: https://img.shields.io/pypi/pyversions/pytest-timeout.svg
17  :target: https://pypi.python.org/pypi/pytest-timeout/
18
19This is a plugin which will terminate tests after a certain timeout,
20assuming the test session isn't being debugged. When aborting a test
21it will show a stack dump of all threads running at the time.
22This is useful when running tests under a continuous
23integration server or simply if you don't know why the test suite hangs.
24
25.. note::
26
27   The way this plugin detects whether or not a debugging session is
28   active is by checking if a trace function is set and if one is, it
29   check to see if the module it belongs to is present in a set of
30   known debugging frameworks modules OR if pytest itself drops you
31   into a pbd session.
32
33.. note::
34
35   While by default on POSIX systems pytest will continue to execute
36   the tests after a test has timed out this is not always possible.
37   Often the only sure way to interrupt a hanging test is by
38   terminating the entire process.  As this is a hard termination
39   (``os._exit()``) it will result in no teardown, JUnit XML output
40   etc.  But the plugin will ensure you will have the debugging output
41   on stderr nevertheless, which is the most important part at this
42   stage.  See below for detailed information on the timeout methods
43   and their side-effects.
44
45The pytest-timeout plugin has been tested on python 2.7 or higher,
46including 3.X, pypy and pypy3.  See tox.ini for currently tested
47versions.
48
49
50Usage
51=====
52
53Install is as simple as e.g.::
54
55   pip install pytest-timeout
56
57Now you can run tests using a timeout, in seconds, after which they
58will be terminated::
59
60   pytest --timeout=300
61
62Alternatively you can mark individual tests as having a timeout::
63
64   @pytest.mark.timeout(60)
65   def test_foo():
66       pass
67
68By default the plugin will not time out any tests, you must specify a
69valid timeout for the plugin to interrupt long-running tests.  A
70timeout is always specified as a number of seconds, and can be
71defined in a number of ways, from low to high priority:
72
731. You can set a global timeout in the `pytest configuration file`__
74   using the ``timeout`` option.  E.g.::
75
76      [pytest]
77      timeout = 300
78
792. The ``PYTEST_TIMEOUT`` environment variable sets a global timeout
80   overriding a possible value in the configuration file.
81
823. The ``--timeout`` command line option sets a global timeout
83   overriding both the environment variable and configuration option.
84
854. Using the ``timeout`` marker_ on test items you can specify
86   timeouts on a per-item basis::
87
88      @pytest.mark.timeout(300)
89      def test_foo():
90          pass
91
92__ https://docs.pytest.org/en/latest/reference.html#ini-options-ref
93
94.. _marker: https://docs.pytest.org/en/latest/mark.html
95
96Setting a timeout to 0 seconds disables the timeout, so if you have a
97global timeout set you can still disable the timeout by using the
98mark.
99
100Timeout Methods
101===============
102
103Interrupting tests which hang is not always as simple and can be
104platform dependent.  Furthermore some methods of terminating a test
105might conflict with the code under test itself.  The pytest-timeout
106plugin tries to pick the most suitable method based on your platform,
107but occasionally you may need to specify a specific timeout method
108explicitly.
109
110   If a timeout method does not work your safest bet is to use the
111   *thread* method.
112
113thread
114------
115
116This is the surest and most portable method.  It is also the default
117on systems not supporting the *signal* method.  For each test item the
118pytest-timeout plugin starts a timer thread which will terminate the
119whole process after the specified timeout.  When a test item finishes
120this timer thread is cancelled and the test run continues.
121
122The downsides of this method are that there is a relatively large
123overhead for running each test and that test runs are not completed.
124This means that other pytest features, like e.g. JUnit XML output or
125fixture teardown, will not function normally.  The second issue might
126be alleviated by using the ``--boxed`` option of the pytest-xdist_
127plugin.
128
129.. _pytest-xdist: https://pypi.org/project/pytest-xdist/
130
131The benefit of this method is that it will always work.  Furthermore
132it will still provide you debugging information by printing the stacks
133of all the threads in the application to stderr.
134
135signal
136------
137
138If the system supports the SIGALRM signal the *signal* method will be
139used by default.  This method schedules an alarm when the test item
140starts and cancels it when it finishes.  If the alarm expires during
141the test the signal handler will dump the stack of any other threads
142running to stderr and use ``pytest.fail()`` to interrupt the test.
143
144The benefit of this method is that the pytest process is not
145terminated and the test run can complete normally.
146
147The main issue to look out for with this method is that it may
148interfere with the code under test.  If the code under test uses
149SIGALRM itself things will go wrong and you will have to choose the
150*thread* method.
151
152Specifying the Timeout Method
153-----------------------------
154
155The timeout method can be specified by using the ``timeout_method``
156option in the `pytest configuration file`__, the ``--timeout_method``
157command line parameter or the ``timeout`` marker_.  Simply set their
158value to the string ``thread`` or ``signal`` to override the default
159method.  On a marker this is done using the ``method`` keyword::
160
161   @pytest.mark.timeout(method='thread')
162   def test_foo():
163       pass
164
165__ https://docs.pytest.org/en/latest/reference.html#ini-options-ref
166
167.. _marker: https://docs.pytest.org/en/latest/mark.html
168
169The ``timeout`` Marker API
170==========================
171
172The full signature of the timeout marker is::
173
174   pytest.mark.timeout(timeout=0, method=DEFAULT_METHOD)
175
176You can use either positional or keyword arguments for both the
177timeout and the method.  Neither needs to be present.
178
179See the marker api documentation_ and examples_ for the various ways
180markers can be applied to test items.
181
182.. _documentation: https://docs.pytest.org/en/latest/mark.html
183
184.. _examples: https://docs.pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
185
186
187Timeouts in Fixture Teardown
188============================
189
190The plugin will happily terminate timeouts in the finalisers of
191fixtures.  The timeout specified applies to the entire process of
192setting up fixtures, running the tests and finalising the fixtures.
193However when a timeout occurs in a fixture finaliser and the test
194suite continues, i.e. the signal method is used, it must be realised
195that subsequent fixtures which need to be finalised might not have
196been executed, which could result in a broken test-suite anyway.  In
197case of doubt the thread method which terminates the entire process
198might result in clearer output.
199
200
201Changelog
202=========
203
2041.4.2
205-----
206
207- Fix compatibility when run with pytest pre-releases, thanks
208  Bruno Oliveira,
209- Fix detection of third-party debuggers, thanks Bruno Oliveira.
210
2111.4.1
212-----
213
214- Fix coverage compatibility which was broken by 1.4.0.
215
2161.4.0
217-----
218
219- Better detection of when we are debugging, thanks Mattwmaster58.
220
2211.3.4
222-----
223
224- Give the threads a name to help debugging, thanks Thomas Grainger.
225- Changed location to https://github.com/pytest-dev/pytest-timeout
226  because bitbucket is dropping mercurial support.  Thanks Thomas
227  Grainger and Bruno Oliveira.
228
2291.3.3
230-----
231
232- Fix support for pytest >= 3.10.
233
2341.3.2
235-----
236
237- This changelog was ommitted for the 1.3.2 release and was added
238  afterwards.  Apologies for the confusion.
239- Fix pytest 3.7.3 compatibility.  The capture API had changed
240  slightly and this needed fixing.  Thanks Bruno Oliveira for the
241  contribution.
242
2431.3.1
244-----
245
246- Fix deprecation warning on Python 3.6.  Thanks Mickaël Schoentgen
247- Create a valid tag for the release.  Somehow this didn't happen for
248  1.3.0, that tag points to a non-existing commit.
249
2501.3.0
251-----
252
253- Make it possible to only run the timeout timer on the test function
254  and not the whole fixture setup + test + teardown duration.  Thanks
255  Pedro Algarvio for the work!
256- Use the new pytest marker API, Thanks Pedro Algarvio for the work!
257
2581.2.1
259-----
260
261- Fix for pytest 3.3, thanks Bruno Oliveira.
262- Update supported python versions:
263  - Add CPython 3.6.
264  - Drop CPyhon 2.6 (as did pytest 3.3)
265  - Drop CPyhon 3.3
266  - Drop CPyhon 3.4
267
2681.2.0
269-----
270
271* Allow using floats as timeout instead of only integers, thanks Tom
272  Myers.
273
2741.1.0
275-----
276
277* Report (default) timeout duration in header, thanks Holger Krekel.
278
2791.0.0
280-----
281
282* Bump version to 1.0 to commit to semantic versioning.
283* Fix issue #12: Now compatible with pytest 2.8, thanks Holger Krekel.
284* No longer test with pexpect on py26 as it is no longer supported
285* Require pytest 2.8 and use new hookimpl decorator
286
2870.5
288---
289
290* Timeouts will no longer be triggered when inside an interactive pdb
291  session started by ``pytest.set_trace()`` / ``pdb.set_trace()``.
292
293* Add pypy3 environment to tox.ini.
294
295* Transfer repository to pytest-dev team account.
296
2970.4
298---
299
300* Support timeouts happening in (session scoped) finalizers.
301
302* Change command line option --timeout_method into --timeout-method
303  for consistency with pytest
304
3050.3
306---
307
308* Added the PYTEST_TIMEOUT environment variable as a way of specifying
309  the timeout (closes issue #2).
310
311* More flexible marker argument parsing: you can now specify the
312  method using a positional argument.
313
314* The plugin is now enabled by default.  There is no longer a need to
315  specify ``timeout=0`` in the configuration file or on the command
316  line simply so that a marker would work.
317
318
3190.2
320---
321
322* Add a marker to modify the timeout delay using a @pytest.timeout(N)
323  syntax, thanks to Laurant Brack for the initial code.
324
325* Allow the timeout marker to select the timeout method using the
326  ``method`` keyword argument.
327
328* Rename the --nosigalrm option to --method=thread to future proof
329  support for eventlet and gevent.  Thanks to Ronny Pfannschmidt for
330  the hint.
331
332* Add ``timeout`` and ``timeout_method`` items to the configuration
333  file so you can enable and configure the plugin using the ini file.
334  Thanks to Holger Krekel and Ronny Pfannschmidt for the hints.
335
336* Tested (and fixed) for python 2.6, 2.7 and 3.2.
337