8000 bpo-4080: unittest durations by giampaolo · Pull Request #12271 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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
merged 50 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6f0b1fe
print durations
giampaolo Mar 10, 2019
2b16e57
don't print duration if not verbose
giampaolo Mar 10, 2019
49aa791
add cmdline arg (bool) to toggle durations
giampaolo Mar 10, 2019
4830509
make --durations arg accept a mandatory int value
giampaolo Mar 10, 2019
f8a49d3
print durations
giampaolo Mar 10, 2019
875e93d
hide durations < 0.000
giampaolo Mar 10, 2019
59dfc67
minor cosmetic changes
giampaolo Mar 10, 2019
c92bc24
don't use bare except clause
giampaolo Mar 10, 2019
d88087e
add a public addDuration method to TestResult
giampaolo Mar 10, 2019
1952ffa
fix TypeError occurring on skip
giampaolo Mar 10, 2019
9f308dc
Merge branch 'master' into unittest-durations
giampaolo Mar 10, 2019
226288a
fix unit tests
giampaolo Mar 11, 2019
89e915f
add a couple of tests for runner
giampaolo Mar 11, 2019
968c3af
don't add durations multiple times + write durations test
giampaolo Mar 11, 2019
a8b38e9
don't print header if there are no durations
giampaolo Mar 11, 2019
f95edc7
small refactoring
giampaolo Mar 11, 2019
9beb931
add doc
giampaolo Mar 11, 2019
86fc3f8
update doc
giampaolo Mar 11, 2019
a1f5e49
improve test
giampaolo Mar 11, 2019
9d98536
use example from stdlib
giampaolo Mar 11, 2019
7223bc9
ignore me
giampaolo Mar 11, 2019
3c4af91
merge from master
giampaolo Apr 26, 2019
3f4d638
address PR review (amongst which, show slowest tests firsts)
giampaolo Apr 26, 2019
b3bb3e0
update doc samples
giampaolo Apr 26, 2019
a93b460
fix doc syntax
giampaolo Apr 29, 2019
0a6bcbb
Merge branch 'master' into unittest-durations
giampaolo Apr 29, 2019
961ba55
merge from master
giampaolo Feb 15, 2020
d0330f6
update doc
giampaolo Feb 15, 2020
9935349
update doc
giampaolo Feb 15, 2020
f4cd001
rephrasing
giampaolo Feb 16, 2020
4471928
forgot pdb line
giampaolo Feb 16, 2020
6412439
update whatsnew
giampaolo May 26, 2020
1a9c098
update code to latest main branch
giampaolo Nov 24, 2022
c917206
updated Misc/NEWS entry
giampaolo Nov 24, 2022
5a4e21b
merge from main
giampaolo Nov 24, 2022
2ffe250
address review comments
giampaolo Nov 24, 2022
2fdfe1d
add test
giampaolo Nov 24, 2022
c0e137e
no longer print individual test durations
giampaolo Nov 24, 2022
86a094c
remove duplicated test
giampaolo Nov 24, 2022
3649013
calculate duration time after cleanup methods
giampaolo Nov 24, 2022
3217b89
reword doc / cmdline help
giampaolo Nov 24, 2022
b2b37ad
move TextRunner test in test_runner.py
giampaolo Nov 24, 2022
2b7f3dc
improve unit test
giampaolo Nov 24, 2022
cfd65b0
remove code which is no longer used
giampaolo Nov 24, 2022
4772f96
Merge branch 'main' into unittest-durations
giampaolo Nov 24, 2022
3ea0be8
Merge branch 'main' into unittest-durations
giampaolo Nov 28, 2022
19f9c4b
address @ezio-melotti code review comments
giampaolo Nov 28, 2022
ed5f317
Merge branch 'main' into unittest-durations
giampaolo Apr 2, 2023
e6d3b58
Update 3.12.rst
giampaolo Apr 2, 2023
90a3d70
Update 3.12.rst
giampaolo Apr 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
print durations
  • Loading branch information
giampaolo committed Mar 10, 2019
commit f8a49d3bd4075fe128ae564adc0396da56f6812c
2 changes: 2 additions & 0 deletions Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ def run(self, result=None):
return result
finally:
result.stopTest(self)
if result.durations is not None:
result.addDuration(self, result.runTime)
if orig_result is None:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
Expand Down
4 changes: 4 additions & 0 deletions Lib/unittest/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, stream=None, descriptions=None, verbosity=None):
self.skipped = []
self.expectedFailures = []
self.unexpectedSuccesses = []
self.collectedDurations = []
self.shouldStop = False
self.buffer = False
self.tb_locals = False
Expand Down Expand Up @@ -157,6 +158,9 @@ def addUnexpectedSuccess(self, test):
"""Called when a test was expected to fail, but succeed."""
self.unexpectedSuccesses.append(test)

def addDuration(self, test, elapsed):
self.collectedDurations.append((test, elapsed))

def wasSuccessful(self):
"""Tells whether or not this result was a success."""
# The hasattr check is for test_result's OldResult test. That
Expand Down
16 changes: 15 additions & 1 deletion Lib/unittest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, stream, descriptions, verbosity, *, durations=None):

def _fmtRes(self, s):
if self.durations is not None:
return "%.2f %s" % (self.runTime, s)
return "%.3f %s" % (self.runTime, s)
else:
return s

Expand Down Expand Up @@ -163,6 +163,18 @@ def _makeResult(self):
return self.resultclass(self.stream, self.descriptions,
self.verbosity)

def _printDurations(self, result):
self.stream.writeln("Slowest test durations")
if hasattr(result, 'separator2'):
self.stream.writeln(result.separator2)
ls = result.collectedDurations
ls.sort(key=lambda x: x[1])
if self.durations > 0:
ls = ls[-self.durations:]
for test, elapsed in ls:
print("%-12s %s" % ("%.3fs" % elapsed, str(test)))
self.stream.writeln("")

def run(self, test):
"Run the given test case or test suite."
result = self._makeResult()
Expand Down Expand Up @@ -196,6 +208,8 @@ def run(self, test):
stopTime = time.perf_counter()
timeTaken = stopTime - startTime
result.printErrors()
if getattr(self, 'durations', None) is not None:
self._printDurations(result)
if hasattr(result, 'separator2'):
self.stream.writeln(result.separator2)
run = result.testsRun
Expand Down
0