Debian Codegraph @

PKG-INFO

raw
1 Metadata-Version: 2.4
2 Name: pytest-timeout
3 Version: 2.4.0
4 Summary: pytest plugin to abort hanging tests
5 Home-page: https://github.com/pytest-dev/pytest-timeout
6 Author: Floris Bruynooghe
7 Author-email: [email protected]
8 License: MIT
9 Classifier: Development Status :: 5 - Production/Stable
10 Classifier: Environment :: Console
11 Classifier: Environment :: Plugins
12 Classifier: Intended Audience :: Developers
13 Classifier: License :: DFSG approved
14 Classifier: License :: OSI Approved :: MIT License
15 Classifier: Operating System :: OS Independent
16 Classifier: Programming Language :: Python
17 Classifier: Programming Language :: Python :: Implementation :: PyPy
18 Classifier: Programming Language :: Python :: Implementation :: CPython
19 Classifier: Programming Language :: Python :: 3
20 Classifier: Programming Language :: Python :: 3 :: Only
21 Classifier: Programming Language :: Python :: 3.7
22 Classifier: Programming Language :: Python :: 3.8
23 Classifier: Programming Language :: Python :: 3.9
24 Classifier: Programming Language :: Python :: 3.10
25 Classifier: Programming Language :: Python :: 3.11
26 Classifier: Programming Language :: Python :: 3.12
27 Classifier: Programming Language :: Python :: 3.13
28 Classifier: Topic :: Software Development :: Testing
29 Classifier: Framework :: Pytest
30 Requires-Python: >=3.7
31 Description-Content-Type: text/x-rst
32 License-File: LICENSE
33 Requires-Dist: pytest>=7.0.0
34 Dynamic: license-file
35
36 ==============
37 pytest-timeout
38 ==============
39
40 |python| |version| |anaconda| |ci| |pre-commit|
41
42 .. |version| image:: https://img.shields.io/pypi/v/pytest-timeout.svg
43 :target: https://pypi.python.org/pypi/pytest-timeout
44
45 .. |anaconda| image:: https://img.shields.io/conda/vn/conda-forge/pytest-timeout.svg
46 :target: https://anaconda.org/conda-forge/pytest-timeout
47
48 .. |ci| image:: https://github.com/pytest-dev/pytest-timeout/workflows/build/badge.svg
49 :target: https://github.com/pytest-dev/pytest-timeout/actions
50
51 .. |python| image:: https://img.shields.io/pypi/pyversions/pytest-timeout.svg
52 :target: https://pypi.python.org/pypi/pytest-timeout/
53
54 .. |pre-commit| image:: https://results.pre-commit.ci/badge/github/pytest-dev/pytest-timeout/master.svg
55 :target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest-timeout/master
56
57
58 .. warning::
59
60 Please read this README carefully and only use this plugin if you
61 understand the consequences. This plugin is designed to catch
62 excessively long test durations like deadlocked or hanging tests,
63 it is not designed for precise timings or performance regressions.
64 Remember your test suite should aim to be **fast**, with timeouts
65 being a last resort, not an expected failure mode.
66
67 This plugin will time each test and terminate it when it takes too
68 long. Termination may or may not be graceful, please see below, but
69 when aborting it will show a stack dump of all thread running at the
70 time. This is useful when running tests under a continuous
71 integration server or simply if you don't know why the test suite
72 hangs.
73
74 .. note::
75
76 While by default on POSIX systems pytest will continue to execute
77 the tests after a test has timed out this is not always possible.
78 Often the only sure way to interrupt a hanging test is by
79 terminating the entire process. As this is a hard termination
80 (``os._exit()``) it will result in no teardown, JUnit XML output
81 etc. But the plugin will ensure you will have the debugging output
82 on stderr nevertheless, which is the most important part at this
83 stage. See below for detailed information on the timeout methods
84 and their side-effects.
85
86 The pytest-timeout plugin has been tested on Python 3.6 and higher,
87 including PyPy3. See tox.ini for currently tested versions.
88
89
90 Usage
91 =====
92
93 Install is as simple as e.g.::
94
95 pip install pytest-timeout
96
97 Now you can run the test suite while setting a timeout in seconds, any
98 individual test which takes longer than the given duration will be
99 terminated::
100
101 pytest --timeout=300
102
103 Furthermore you can also use a decorator to set the timeout for an
104 individual test. If combined with the ``--timeout`` flag this will
105 override the timeout for this individual test:
106
107 .. code:: python
108
109 @pytest.mark.timeout(60)
110 def test_foo():
111 pass
112
113 By default the plugin will not time out any tests, you must specify a
114 valid timeout for the plugin to interrupt long-running tests. A
115 timeout is always specified as a number of seconds, and can be
116 defined in a number of ways, from low to high priority:
117
118 1. You can set a global timeout in the `pytest configuration file`__
119 using the ``timeout`` option. E.g.:
120
121 .. code:: ini
122
123 [pytest]
124 timeout = 300
125
126 2. The ``PYTEST_TIMEOUT`` environment variable sets a global timeout
127 overriding a possible value in the configuration file.
128
129 3. The ``--timeout`` command line option sets a global timeout
130 overriding both the environment variable and configuration option.
131
132 4. Using the ``timeout`` marker_ on test items you can specify
133 timeouts on a per-item basis:
134
135 .. code:: python
136
137 @pytest.mark.timeout(300)
138 def test_foo():
139 pass
140
141 __ https://docs.pytest.org/en/latest/reference.html#ini-options-ref
142
143 .. _marker: https://docs.pytest.org/en/latest/mark.html
144
145 Setting a timeout to 0 seconds disables the timeout, so if you have a
146 global timeout set you can still disable the timeout by using the
147 mark.
148
149 Timeout Methods
150 ===============
151
152 Interrupting tests which hang is not always as simple and can be
153 platform dependent. Furthermore some methods of terminating a test
154 might conflict with the code under test itself. The pytest-timeout
155 plugin tries to pick the most suitable method based on your platform,
156 but occasionally you may need to specify a specific timeout method
157 explicitly.
158
159 If a timeout method does not work your safest bet is to use the
160 *thread* method.
161
162 thread
163 ------
164
165 This is the surest and most portable method. It is also the default
166 on systems not supporting the *signal* method. For each test item the
167 pytest-timeout plugin starts a timer thread which will terminate the
168 whole process after the specified timeout. When a test item finishes
169 this timer thread is cancelled and the test run continues.
170
171 The downsides of this method are that there is a relatively large
172 overhead for running each test and that test runs are not completed.
173 This means that other pytest features, like e.g. JUnit XML output or
174 fixture teardown, will not function normally. The second issue might
175 be alleviated by using the ``--forked`` option of the pytest-forked_
176 plugin.
177
178 .. _pytest-forked: https://pypi.org/project/pytest-forked/
179
180 The benefit of this method is that it will always work. Furthermore
181 it will still provide you debugging information by printing the stacks
182 of all the threads in the application to stderr.
183
184 signal
185 ------
186
187 If the system supports the SIGALRM signal the *signal* method will be
188 used by default. This method schedules an alarm when the test item
189 starts and cancels the alarm when the test finishes. If the alarm expires
190 during the test the signal handler will dump the stack of any other threads
191 running to stderr and use ``pytest.fail()`` to interrupt the test.
192
193 The benefit of this method is that the pytest process is not
194 terminated and the test run can complete normally.
195
196 The main issue to look out for with this method is that it may
197 interfere with the code under test. If the code under test uses
198 SIGALRM itself things will go wrong and you will have to choose the
199 *thread* method.
200
201 Specifying the Timeout Method
202 -----------------------------
203
204 The timeout method can be specified by using the ``timeout_method``
205 option in the `pytest configuration file`__, the ``--timeout_method``
206 command line parameter or the ``timeout`` marker_. Simply set their
207 value to the string ``thread`` or ``signal`` to override the default
208 method. On a marker this is done using the ``method`` keyword:
209
210 .. code:: python
211
212 @pytest.mark.timeout(method="thread")
213 def test_foo():
214 pass
215
216 __ https://docs.pytest.org/en/latest/reference.html#ini-options-ref
217
218 .. _marker: https://docs.pytest.org/en/latest/mark.html
219
220 The ``timeout`` Marker API
221 ==========================
222
223 The full signature of the timeout marker is:
224
225 .. code:: python
226
227 pytest.mark.timeout(timeout=0, method=DEFAULT_METHOD)
228
229 You can use either positional or keyword arguments for both the
230 timeout and the method. Neither needs to be present.
231
232 See the marker api documentation_ and examples_ for the various ways
233 markers can be applied to test items.
234
235 .. _documentation: https://docs.pytest.org/en/latest/mark.html
236
237 .. _examples: https://docs.pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
238
239
240 Timeouts in Fixture Teardown
241 ============================
242
243 The plugin will happily terminate timeouts in the finalisers of
244 fixtures. The timeout specified applies to the entire process of
245 setting up fixtures, running the tests and finalising the fixtures.
246 However when a timeout occurs in a fixture finaliser and the test
247 suite continues, i.e. the signal method is used, it must be realised
248 that subsequent fixtures which need to be finalised might not have
249 been executed, which could result in a broken test-suite anyway. In
250 case of doubt the thread method which terminates the entire process
251 might result in clearer output.
252
253 Avoiding timeouts in Fixtures
254 =============================
255
256 The timeout applies to the entire test including any fixtures which
257 may need to be setup or torn down for the test (the exact affected
258 fixtures depends on which scope they are and whether other tests will
259 still use the same fixture). If the timeouts really are too short to
260 include fixture durations, firstly make the timeouts larger ;). If
261 this really isn't an option a ``timeout_func_only`` boolean setting
262 exists which can be set in the pytest ini configuration file, as
263 documented in ``pytest --help``.
264
265 For the decorated function, a decorator will override
266 ``timeout_func_only = true`` in the pytest ini file to the default
267 value. If you need to keep this option for a decorated test, you
268 must specify the option explicitly again:
269
270 .. code:: python
271
272 @pytest.mark.timeout(60, func_only=True)
273 def test_foo():
274 pass
275
276
277 Debugger Detection
278 ==================
279
280 This plugin tries to avoid triggering the timeout when a debugger is
281 detected. This is mostly a convenience so you do not need to remember
282 to disable the timeout when interactively debugging.
283
284 The way this plugin detects whether or not a debugging session is
285 active is by checking if a trace function is set and if one is, it
286 check to see if the module it belongs to is present in a set of known
287 debugging frameworks modules OR if pytest itself drops you into a pdb
288 session using ``--pdb`` or similar.
289
290 This functionality can be disabled with the ``--disable-debugger-detection`` flag
291 or the corresponding ``timeout_disable_debugger_detection`` ini setting / environment
292 variable.
293
294
295 Extending pytest-timeout with plugins
296 =====================================
297
298 ``pytest-timeout`` provides two hooks that can be used for extending the tool. These
299 hooks are used for setting the timeout timer and cancelling it if the timeout is not
300 reached.
301
302 For example, ``pytest-asyncio`` can provide asyncio-specific code that generates better
303 traceback and points on timed out ``await`` instead of the running loop iteration.
304
305 See `pytest hooks documentation
306 <https://docs.pytest.org/en/latest/how-to/writing_hook_functions.html>`_ for more info
307 regarding to use custom hooks.
308
309 ``pytest_timeout_set_timer``
310 ----------------------------
311
312 .. code:: python
313
314 @pytest.hookspec(firstresult=True)
315 def pytest_timeout_set_timer(item, settings):
316 """Called at timeout setup.
317
318 'item' is a pytest node to setup timeout for.
319
320 'settings' is Settings namedtuple (described below).
321
322 Can be overridden by plugins for alternative timeout implementation strategies.
323 """
324
325
326 ``Settings``
327 ------------
328
329 When ``pytest_timeout_set_timer`` is called, ``settings`` argument is passed.
330
331 The argument has ``Settings`` namedtuple type with the following fields:
332
333 +-----------+-------+--------------------------------------------------------+
334 |Attribute | Index | Value |
335 +===========+=======+========================================================+
336 | timeout | 0 | timeout in seconds or ``None`` for no timeout |
337 +-----------+-------+--------------------------------------------------------+
338 | method | 1 | Method mechanism, |
339 | | | ``'signal'`` and ``'thread'`` are supported by default |
340 +-----------+-------+--------------------------------------------------------+
341 | func_only | 2 | Apply timeout to test function only if ``True``, |
342 | | | wrap all test function and its fixtures otherwise |
343 +-----------+-------+--------------------------------------------------------+
344
345 ``pytest_timeout_cancel_timer``
346 -------------------------------
347
348 .. code:: python
349
350 @pytest.hookspec(firstresult=True)
351 def pytest_timeout_cancel_timer(item):
352 """Called at timeout teardown.
353
354 'item' is a pytest node which was used for timeout setup.
355
356 Can be overridden by plugins for alternative timeout implementation strategies.
357 """
358
359 ``is_debugging``
360 ----------------
361
362 When the timeout occurs, user can open the debugger session. In this case, the timeout
363 should be discarded. A custom hook can check this case by calling ``is_debugging()``
364 function:
365
366 .. code:: python
367
368 import pytest
369 import pytest_timeout
370
371
372 def on_timeout():
373 if pytest_timeout.is_debugging():
374 return
375 pytest.fail("+++ Timeout +++")
376
377
378
379 Session Timeout
380 ===============
381
382 The above mentioned timeouts are all per test function.
383 The "per test function" timeouts will stop an individual test
384 from taking too long. We may also want to limit the time of the entire
385 set of tests running in one session. A session all of the tests
386 that will be run with one invokation of pytest.
387
388 A session timeout is set with `--session-timeout` and is in seconds.
389
390 The following example shows a session timeout of 10 minutes (600 seconds)::
391
392 pytest --session-timeout=600
393
394 You can also set the session timeout the pytest configuration file using the ``session_timeout`` option:
395
396 .. code:: ini
397
398 [pytest]
399 session_timeout = 600
400
401 Cooperative timeouts
402 --------------------
403
404 Session timeouts are cooperative timeouts. pytest-timeout checks the
405 session time at the end of each test function, and stops further tests
406 from running if the session timeout is exceeded. The session will
407 results in a test failure if this occurs.
408
409 In particular this means if a test does not finish of itself, it will
410 only be interrupted if there is also a function timeout set. A
411 session timeout is not enough to ensure that a test-suite is
412 guaranteed to finish.
413
414 Combining session and function timeouts
415 ---------------------------------------
416
417 It works fine to combine both session and function timeouts. In fact
418 when using a session timeout it is recommended to also provide a
419 function timeout.
420
421 For example, to limit test functions to 5 seconds and the full session
422 to 100 seconds::
423
424 pytest --timeout=5 --session-timeout=100
425
426
427 Changelog
428 =========
429
430 2.4.0
431 -----
432
433 - Detect debuggers registered with sys.monitoring. Thanks Rich
434 Chiodo.
435 - Make it clear the timeout message comes from pytest-timeout. Thanks
436 Pedro Brochado.
437
438 2.3.1
439 -----
440
441 - Fixup some build errors, mostly README syntax which stopped twine
442 from uploading.
443
444 2.3.0
445 -----
446
447 - Fix debugger detection for recent VSCode, this compiles pydevd using
448 cython which is now correctly detected. Thanks Adrian Gielniewski.
449 - Switched to using Pytest's ``TerminalReporter`` instead of writing
450 directly to ``sys.{stdout,stderr}``.
451 This change also switches all output from ``sys.stderr`` to ``sys.stdout``.
452 Thanks Pedro Algarvio.
453 - Pytest 7.0.0 is now the minimum supported version. Thanks Pedro Algarvio.
454 - Add ``--session-timeout`` option and ``session_timeout`` setting.
455 Thanks Brian Okken.
456
457 2.2.0
458 -----
459
460 - Add ``--timeout-disable-debugger-detection`` flag, thanks
461 Michael Peters
462
463 2.1.0
464 -----
465
466 - Get terminal width from shutil instead of deprecated py, thanks
467 Andrew Svetlov.
468 - Add an API for extending ``pytest-timeout`` functionality
469 with third-party plugins, thanks Andrew Svetlov.
470
471 2.0.2
472 -----
473
474 - Fix debugger detection on OSX, thanks Alexander Pacha.
475
476 2.0.1
477 -----
478
479 - Fix Python 2 removal, thanks Nicusor Picatureanu.
480
481 2.0.0
482 -----
483
484 - Increase pytest requirement to >=5.0.0. Thanks Dominic Davis-Foster.
485 - Use thread timeout method when plugin is not called from main
486 thread to avoid crash.
487 - Fix pycharm debugger detection so timeouts are not triggered during
488 debugger usage.
489 - Dropped support for Python 2, minimum pytest version supported is 5.0.0.
490
491 1.4.2
492 -----
493
494 - Fix compatibility when run with pytest pre-releases, thanks
495 Bruno Oliveira,
496 - Fix detection of third-party debuggers, thanks Bruno Oliveira.
497
498 1.4.1
499 -----
500
501 - Fix coverage compatibility which was broken by 1.4.0.
502
503 1.4.0
504 -----
505
506 - Better detection of when we are debugging, thanks Mattwmaster58.
507
508 1.3.4
509 -----
510
511 - Give the threads a name to help debugging, thanks Thomas Grainger.
512 - Changed location to https://github.com/pytest-dev/pytest-timeout
513 because bitbucket is dropping mercurial support. Thanks Thomas
514 Grainger and Bruno Oliveira.
515
516 1.3.3
517 -----
518
519 - Fix support for pytest >= 3.10.
520
521 1.3.2
522 -----
523
524 - This changelog was omitted for the 1.3.2 release and was added
525 afterwards. Apologies for the confusion.
526 - Fix pytest 3.7.3 compatibility. The capture API had changed
527 slightly and this needed fixing. Thanks Bruno Oliveira for the
528 contribution.
529
530 1.3.1
531 -----
532
533 - Fix deprecation warning on Python 3.6. Thanks Mickaƫl Schoentgen
534 - Create a valid tag for the release. Somehow this didn't happen for
535 1.3.0, that tag points to a non-existing commit.
536
537 1.3.0
538 -----
539
540 - Make it possible to only run the timeout timer on the test function
541 and not the whole fixture setup + test + teardown duration. Thanks
542 Pedro Algarvio for the work!
543 - Use the new pytest marker API, Thanks Pedro Algarvio for the work!
544
545 1.2.1
546 -----
547
548 - Fix for pytest 3.3, thanks Bruno Oliveira.
549 - Update supported python versions:
550 - Add CPython 3.6.
551 - Drop CPyhon 2.6 (as did pytest 3.3)
552 - Drop CPyhon 3.3
553 - Drop CPyhon 3.4
554
555 1.2.0
556 -----
557
558 * Allow using floats as timeout instead of only integers, thanks Tom
559 Myers.
560
561 1.1.0
562 -----
563
564 * Report (default) timeout duration in header, thanks Holger Krekel.
565
566 1.0.0
567 -----
568
569 * Bump version to 1.0 to commit to semantic versioning.
570 * Fix issue #12: Now compatible with pytest 2.8, thanks Holger Krekel.
571 * No longer test with pexpect on py26 as it is no longer supported
572 * Require pytest 2.8 and use new hookimpl decorator
573
574 0.5
575 ---
576
577 * Timeouts will no longer be triggered when inside an interactive pdb
578 session started by ``pytest.set_trace()`` / ``pdb.set_trace()``.
579
580 * Add pypy3 environment to tox.ini.
581
582 * Transfer repository to pytest-dev team account.
583
584 0.4
585 ---
586
587 * Support timeouts happening in (session scoped) finalizers.
588
589 * Change command line option --timeout_method into --timeout-method
590 for consistency with pytest
591
592 0.3
593 ---
594
595 * Added the PYTEST_TIMEOUT environment variable as a way of specifying
596 the timeout (closes issue #2).
597
598 * More flexible marker argument parsing: you can now specify the
599 method using a positional argument.
600
601 * The plugin is now enabled by default. There is no longer a need to
602 specify ``timeout=0`` in the configuration file or on the command
603 line simply so that a marker would work.
604
605
606 0.2
607 ---
608
609 * Add a marker to modify the timeout delay using a @pytest.timeout(N)
610 syntax, thanks to Laurant Brack for the initial code.
611
612 * Allow the timeout marker to select the timeout method using the
613 ``method`` keyword argument.
614
615 * Rename the --nosigalrm option to --method=thread to future proof
616 support for eventlet and gevent. Thanks to Ronny Pfannschmidt for
617 the hint.
618
619 * Add ``timeout`` and ``timeout_method`` items to the configuration
620 file so you can enable and configure the plugin using the ini file.
621 Thanks to Holger Krekel and Ronny Pfannschmidt for the hints.
622
623 * Tested (and fixed) for python 2.6, 2.7 and 3.2.