Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-4080: unittest durations#12271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
bpo-4080: unittest durations #12271
Changes from all commits
Commits
Show all changes
50 commits Select commit Hold shift + click to select a range
6f0b1fe print durations
giampaolo 2b16e57 don't print duration if not verbose
giampaolo 49aa791 add cmdline arg (bool) to toggle durations
giampaolo 4830509 make --durations arg accept a mandatory int value
giampaolo f8a49d3 print durations
giampaolo 875e93d hide durations < 0.000
giampaolo 59dfc67 minor cosmetic changes
giampaolo c92bc24 don't use bare except clause
giampaolo d88087e add a public addDuration method to TestResult
giampaolo 1952ffa fix TypeError occurring on skip
giampaolo 9f308dc Merge branch 'master' into unittest-durations
giampaolo 226288a fix unit tests
giampaolo 89e915f add a couple of tests for runner
giampaolo 968c3af don't add durations multiple times + write durations test
giampaolo a8b38e9 don't print header if there are no durations
giampaolo f95edc7 small refactoring
giampaolo 9beb931 add doc
giampaolo 86fc3f8 update doc
giampaolo a1f5e49 improve test
giampaolo 9d98536 use example from stdlib
giampaolo 7223bc9 ignore me
giampaolo 3c4af91 merge from master
giampaolo 3f4d638 address PR review (amongst which, show slowest tests firsts)
giampaolo b3bb3e0 update doc samples
giampaolo a93b460 fix doc syntax
giampaolo 0a6bcbb Merge branch 'master' into unittest-durations
giampaolo 961ba55 merge from master
giampaolo d0330f6 update doc
giampaolo 9935349 update doc
giampaolo f4cd001 rephrasing
giampaolo 4471928 forgot pdb line
giampaolo 6412439 update whatsnew
giampaolo 1a9c098 update code to latest main branch
giampaolo c917206 updated Misc/NEWS entry
giampaolo 5a4e21b merge from main
giampaolo 2ffe250 address review comments
giampaolo 2fdfe1d add test
giampaolo c0e137e no longer print individual test durations
giampaolo 86a094c remove duplicated test
giampaolo 3649013 calculate duration time after cleanup methods
giampaolo 3217b89 reword doc / cmdline help
giampaolo b2b37ad move TextRunner test in test_runner.py
giampaolo 2b7f3dc improve unit test
giampaolo cfd65b0 remove code which is no longer used
giampaolo 4772f96 Merge branch 'main' into unittest-durations
giampaolo 3ea0be8 Merge branch 'main' into unittest-durations
giampaolo 19f9c4b address @ezio-melotti code review comments
giampaolo ed5f317 Merge branch 'main' into unittest-durations
giampaolo e6d3b58 Update 3.12.rst
giampaolo 90a3d70 Update 3.12.rst
giampaolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
giampaolo marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,8 +8,11 @@ | ||
| import unittest | ||
| from unittest.case import _Outcome | ||
| from test.test_unittest.support import (LoggingResult, | ||
| ResultWithNoStartTestRunStopTestRun) | ||
| from test.test_unittest.support import ( | ||
| BufferedWriter, | ||
| LoggingResult, | ||
| ResultWithNoStartTestRunStopTestRun, | ||
| ) | ||
| def resultFactory(*_): | ||
| @@ -1176,6 +1179,7 @@ def test_init(self): | ||
| self.assertTrue(runner.descriptions) | ||
| self.assertEqual(runner.resultclass, unittest.TextTestResult) | ||
| self.assertFalse(runner.tb_locals) | ||
| self.assertIsNone(runner.durations) | ||
| def test_multiple_inheritance(self): | ||
| class AResult(unittest.TestResult): | ||
| @@ -1362,6 +1366,65 @@ def testSpecifiedStreamUsed(self): | ||
| runner = unittest.TextTestRunner(f) | ||
| self.assertTrue(runner.stream.stream is f) | ||
| def test_durations(self): | ||
| def run(test, expect_durations): | ||
giampaolo marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| stream = BufferedWriter() | ||
| runner = unittest.TextTestRunner(stream=stream, durations=5, verbosity=2) | ||
| result = runner.run(test) | ||
| self.assertEqual(result.durations, 5) | ||
| stream.flush() | ||
| text = stream.getvalue() | ||
| regex = r"\n\d+.\d\d\ds" | ||
| if expect_durations: | ||
| self.assertEqual(len(result.collectedDurations), 1) | ||
| self.assertIn('Slowest test durations', text) | ||
| self.assertRegex(text, regex) | ||
| else: | ||
| self.assertEqual(len(result.collectedDurations), 0) | ||
| self.assertNotIn('Slowest test durations', text) | ||
| self.assertNotRegex(text, regex) | ||
| # success | ||
| class Foo(unittest.TestCase): | ||
| def test_1(self): | ||
| pass | ||
| run(Foo('test_1'), True) | ||
| # failure | ||
| class Foo(unittest.TestCase): | ||
| def test_1(self): | ||
| self.assertEqual(0, 1) | ||
| run(Foo('test_1'), True) | ||
| # error | ||
| class Foo(unittest.TestCase): | ||
| def test_1(self): | ||
| 1 / 0 | ||
| run(Foo('test_1'), True) | ||
| # error in setUp and tearDown | ||
| class Foo(unittest.TestCase): | ||
| def setUp(self): | ||
| 1 / 0 | ||
| tearDown = setUp | ||
| def test_1(self): | ||
| pass | ||
| run(Foo('test_1'), True) | ||
| # skip (expect no durations) | ||
| class Foo(unittest.TestCase): | ||
| @unittest.skip("reason") | ||
| def test_1(self): | ||
| pass | ||
| run(Foo('test_1'), False) | ||
| if __name__ == "__main__": | ||
| unittest.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.