Skip to content

Commit 9fb305b

Browse files
authored
Merge pull request #4086 from jeffreyrack/4063-exclude-0-durations
Exclude durations that are 0.00 seconds long.
2 parents 27d2683 + 3683d92 commit 9fb305b

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

changelog/4063.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Exclude 0.00 second entries from ``--duration`` output unless ``-vv`` is passed on the command-line.

doc/en/usage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ To get a list of the slowest 10 test durations::
269269

270270
pytest --durations=10
271271

272+
By default, pytest will not show test durations that are too small (<0.01s) unless ``-vv`` is passed on the command-line.
272273

273274
Creating JUnitXML format files
274275
----------------------------------------------------

src/_pytest/runner.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def pytest_addoption(parser):
3030

3131
def pytest_terminal_summary(terminalreporter):
3232
durations = terminalreporter.config.option.durations
33+
verbose = terminalreporter.config.getvalue("verbose")
3334
if durations is None:
3435
return
3536
tr = terminalreporter
@@ -49,6 +50,10 @@ def pytest_terminal_summary(terminalreporter):
4950
dlist = dlist[:durations]
5051

5152
for rep in dlist:
53+
if verbose < 2 and rep.duration < 0.005:
54+
tr.write_line("")
55+
tr.write_line("(0.00 durations hidden. Use -vv to show these durations.)")
56+
break
5257
nodeid = rep.nodeid.replace("::()::", "::")
5358
tr.write_line("%02.2fs %-8s %s" % (rep.duration, rep.when, nodeid))
5459

testing/acceptance_test.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,11 @@ def test_calls(self, testdir):
806806
result = testdir.runpytest("--durations=10")
807807
assert result.ret == 0
808808
result.stdout.fnmatch_lines_random(
809-
["*durations*", "*call*test_3*", "*call*test_2*", "*call*test_1*"]
809+
["*durations*", "*call*test_3*", "*call*test_2*"]
810+
)
811+
assert "test_something" not in result.stdout.str()
812+
result.stdout.fnmatch_lines(
813+
["(0.00 durations hidden. Use -vv to show these durations.)"]
810814
)
811815

812816
def test_calls_show_2(self, testdir):
@@ -820,6 +824,18 @@ def test_calls_showall(self, testdir):
820824
testdir.makepyfile(self.source)
821825
result = testdir.runpytest("--durations=0")
822826
assert result.ret == 0
827+
for x in "23":
828+
for y in ("call",): # 'setup', 'call', 'teardown':
829+
for line in result.stdout.lines:
830+
if ("test_%s" % x) in line and y in line:
831+
break
832+
else:
833+
raise AssertionError("not found {} {}".format(x, y))
834+
835+
def test_calls_showall_verbose(self, testdir):
836+
testdir.makepyfile(self.source)
837+
result = testdir.runpytest("--durations=0", "-vv")
838+
assert result.ret == 0
823839
for x in "123":
824840
for y in ("call",): # 'setup', 'call', 'teardown':
825841
for line in result.stdout.lines:
@@ -830,9 +846,9 @@ def test_calls_showall(self, testdir):
830846

831847
def test_with_deselected(self, testdir):
832848
testdir.makepyfile(self.source)
833-
result = testdir.runpytest("--durations=2", "-k test_1")
849+
result = testdir.runpytest("--durations=2", "-k test_2")
834850
assert result.ret == 0
835-
result.stdout.fnmatch_lines(["*durations*", "*call*test_1*"])
851+
result.stdout.fnmatch_lines(["*durations*", "*call*test_2*"])
836852

837853
def test_with_failing_collection(self, testdir):
838854
testdir.makepyfile(self.source)
@@ -852,13 +868,15 @@ def test_with_not(self, testdir):
852868

853869
class TestDurationWithFixture(object):
854870
source = """
871+
import pytest
855872
import time
856-
frag = 0.001
857-
def setup_function(func):
858-
time.sleep(frag * 3)
859-
def test_1():
860-
time.sleep(frag*2)
861-
def test_2():
873+
frag = 0.01
874+
875+
@pytest.fixture
876+
def setup_fixt():
877+
time.sleep(frag)
878+
879+
def test_1(setup_fixt):
862880
time.sleep(frag)
863881
"""
864882

0 commit comments

Comments
 (0)