summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2023-04-02 22:12:51 (GMT)
committerGitHub <noreply@github.com>2023-04-02 22:12:51 (GMT)
commit6883007a86bdf0d7cf4560b949fd5e577dab1013 (patch)
treeb5585b8135e1136d9b567dda62e887a27033b392 /Doc
parenta0305c5fdfdef7a362d0262c54399c4a6013d1ea (diff)
downloadcpython-6883007a86bdf0d7cf4560b949fd5e577dab1013.zip
cpython-6883007a86bdf0d7cf4560b949fd5e577dab1013.tar.gz
cpython-6883007a86bdf0d7cf4560b949fd5e577dab1013.tar.bz2
bpo-4080: unittest durations (#12271)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/unittest.rst42
-rw-r--r--Doc/whatsnew/3.12.rst21
2 files changed, 57 insertions, 6 deletions
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index 1577149..d1a977f 100644
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -244,6 +244,10 @@ Command-line options
Show local variables in tracebacks.
+.. cmdoption:: --durations N
+
+ Show the N slowest test cases (N=0 for all).
+
.. versionadded:: 3.2
The command-line options ``-b``, ``-c`` and ``-f`` were added.
@@ -253,10 +257,12 @@ Command-line options
.. versionadded:: 3.7
The command-line option ``-k``.
+.. versionadded:: 3.12
+ The command-line option ``--durations``.
+
The command line can also be used for test discovery, for running all of the
tests in a project or just a subset.
-
.. _unittest-test-discovery:
Test Discovery
@@ -2009,6 +2015,13 @@ Loading and running tests
A list containing :class:`TestCase` instances that were marked as expected
failures, but succeeded.
+ .. attribute:: collectedDurations
+
+ A list containing 2-tuples of :class:`TestCase` instances and floats
+ representing the elapsed time of each test which was run.
+
+ .. versionadded:: 3.12
+
.. attribute:: shouldStop
Set to ``True`` when the execution of tests should stop by :meth:`stop`.
@@ -2160,14 +2173,27 @@ Loading and running tests
.. versionadded:: 3.4
+ .. method:: addDuration(test, elapsed)
+
+ Called when the test case finishes. *elapsed* is the time represented in
+ seconds, and it includes the execution of cleanup functions.
+
+ .. versionadded:: 3.12
-.. class:: TextTestResult(stream, descriptions, verbosity)
+.. class:: TextTestResult(stream, descriptions, verbosity, *, durations=None)
A concrete implementation of :class:`TestResult` used by the
- :class:`TextTestRunner`.
+ :class:`TextTestRunner`. Subclasses should accept ``**kwargs`` to ensure
+ compatibility as the interface changes.
.. versionadded:: 3.2
+ .. versionadded:: 3.12
+ Added *durations* keyword argument.
+
+ .. versionchanged:: 3.12
+ Subclasses should accept ``**kwargs`` to ensure compatibility as the
+ interface changes.
.. data:: defaultTestLoader
@@ -2177,7 +2203,8 @@ Loading and running tests
.. class:: TextTestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, \
- buffer=False, resultclass=None, warnings=None, *, tb_locals=False)
+ buffer=False, resultclass=None, warnings=None, *, \
+ tb_locals=False, durations=None)
A basic test runner implementation that outputs results to a stream. If *stream*
is ``None``, the default, :data:`sys.stderr` is used as the output stream. This class
@@ -2195,14 +2222,17 @@ Loading and running tests
*warnings* to ``None``.
.. versionchanged:: 3.2
- Added the ``warnings`` argument.
+ Added the *warnings* parameter.
.. versionchanged:: 3.2
The default stream is set to :data:`sys.stderr` at instantiation time rather
than import time.
.. versionchanged:: 3.5
- Added the tb_locals parameter.
+ Added the *tb_locals* parameter.
+
+ .. versionchanged:: 3.12
+ Added the *durations* parameter.
.. method:: _makeResult()
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index 93543cb..3df3ef7 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -371,6 +371,27 @@ unicodedata
* The Unicode database has been updated to version 15.0.0. (Contributed by
Benjamin Peterson in :gh:`96734`).
+unittest
+--------
+
+Added ``--durations`` command line option, showing the N slowest test cases::
+
+ python3 -m unittest --durations=3 lib.tests.test_threading
+ .....
+ Slowest test durations
+ ----------------------------------------------------------------------
+ 1.210s test_timeout (Lib.test.test_threading.BarrierTests)
+ 1.003s test_default_timeout (Lib.test.test_threading.BarrierTests)
+ 0.518s test_timeout (Lib.test.test_threading.EventTests)
+
+ (0.000 durations hidden. Use -v to show these durations.)
+ ----------------------------------------------------------------------
+ Ran 158 tests in 9.869s
+
+ OK (skipped=3)
+
+(Contributed by Giampaolo Rodola in :issue:`4080`)
+
uuid
----