Merge 5978009ac3
into c01a5c177b
This commit is contained in:
commit
cebf12b254
|
@ -3,6 +3,7 @@ import bdb
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
import warnings
|
||||||
|
from operator import attrgetter
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from typing import cast
|
from typing import cast
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
@ -68,35 +69,43 @@ def pytest_addoption(parser: Parser) -> None:
|
||||||
|
|
||||||
|
|
||||||
def pytest_terminal_summary(terminalreporter: "TerminalReporter") -> None:
|
def pytest_terminal_summary(terminalreporter: "TerminalReporter") -> None:
|
||||||
durations = terminalreporter.config.option.durations
|
durations = terminalreporter.config.getoption("durations")
|
||||||
durations_min = terminalreporter.config.option.durations_min
|
|
||||||
verbose = terminalreporter.config.getvalue("verbose")
|
|
||||||
if durations is None:
|
if durations is None:
|
||||||
return
|
return
|
||||||
tr = terminalreporter
|
|
||||||
dlist = []
|
|
||||||
for replist in tr.stats.values():
|
|
||||||
for rep in replist:
|
|
||||||
if hasattr(rep, "duration"):
|
|
||||||
dlist.append(rep)
|
|
||||||
if not dlist:
|
|
||||||
return
|
|
||||||
dlist.sort(key=lambda x: x.duration, reverse=True) # type: ignore[no-any-return]
|
|
||||||
if not durations:
|
|
||||||
tr.write_sep("=", "slowest durations")
|
|
||||||
else:
|
|
||||||
tr.write_sep("=", "slowest %s durations" % durations)
|
|
||||||
dlist = dlist[:durations]
|
|
||||||
|
|
||||||
for i, rep in enumerate(dlist):
|
durations_min = terminalreporter.config.getoption("durations_min")
|
||||||
if verbose < 2 and rep.duration < durations_min:
|
verbose = terminalreporter.config.getvalue("verbose")
|
||||||
tr.write_line("")
|
reporter = terminalreporter
|
||||||
tr.write_line(
|
durations_list = []
|
||||||
"(%s durations < %gs hidden. Use -vv to show these durations.)"
|
for report_list in reporter.stats.values():
|
||||||
% (len(dlist) - i, durations_min)
|
for report in report_list:
|
||||||
|
if hasattr(report, "duration"):
|
||||||
|
durations_list.append(report)
|
||||||
|
if not durations_list:
|
||||||
|
return
|
||||||
|
|
||||||
|
durations_list.sort(key=attrgetter("duration"), reverse=True)
|
||||||
|
|
||||||
|
if not durations:
|
||||||
|
reporter.write_sep("=", "slowest durations")
|
||||||
|
else:
|
||||||
|
reporter.write_sep("=", "slowest %s durations" % durations)
|
||||||
|
durations_list = durations_list[:durations]
|
||||||
|
|
||||||
|
for index, test_report in enumerate(durations_list):
|
||||||
|
if verbose < 2 and test_report.duration < durations_min:
|
||||||
|
total = len(durations_list) - index
|
||||||
|
pronoun, subject = (
|
||||||
|
("this", "duration") if total == 1 else ("these", "durations")
|
||||||
|
)
|
||||||
|
reporter.write_line("")
|
||||||
|
reporter.write_line(
|
||||||
|
f"({total} {subject} slower than {durations_min:02.3f}s hidden. Use -vv to show {pronoun} {subject}.)"
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
tr.write_line(f"{rep.duration:02.2f}s {rep.when:<8} {rep.nodeid}")
|
reporter.write_line(
|
||||||
|
f"{test_report.duration:02.2f}s {test_report.when:<8} {test_report.nodeid}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def pytest_sessionstart(session: "Session") -> None:
|
def pytest_sessionstart(session: "Session") -> None:
|
||||||
|
|
|
@ -869,7 +869,9 @@ class TestDurations:
|
||||||
)
|
)
|
||||||
|
|
||||||
result.stdout.fnmatch_lines(
|
result.stdout.fnmatch_lines(
|
||||||
["(8 durations < 0.005s hidden. Use -vv to show these durations.)"]
|
[
|
||||||
|
"(8 durations slower than 0.005s hidden. Use -vv to show these durations.)"
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_calls_show_2(self, pytester: Pytester, mock_timing) -> None:
|
def test_calls_show_2(self, pytester: Pytester, mock_timing) -> None:
|
||||||
|
@ -1295,3 +1297,59 @@ def test_no_brokenpipeerror_message(pytester: Pytester) -> None:
|
||||||
|
|
||||||
# Cleanup.
|
# Cleanup.
|
||||||
popen.stderr.close()
|
popen.stderr.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_multiple_durations_pluralized_tr_summary(pytester: Pytester) -> None:
|
||||||
|
pytester.makepyfile(
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
def test_delays():
|
||||||
|
assert True
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
result = pytester.runpytest("--durations=5", "--durations-min=10")
|
||||||
|
assert result.ret == ExitCode.OK
|
||||||
|
result.stdout.fnmatch_lines(
|
||||||
|
["(3 durations slower than 10.000s hidden. Use -vv to show these durations.)"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_single_duration_pluralized_tr_summary(pytester: Pytester) -> None:
|
||||||
|
pytester.makepyfile(
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
import time
|
||||||
|
def _sleep(delay):
|
||||||
|
time.sleep(delay)
|
||||||
|
def test_one():
|
||||||
|
_sleep(0.05)
|
||||||
|
def test_two():
|
||||||
|
_sleep(0.25)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
result = pytester.runpytest("--durations=2", "--durations-min=0.1")
|
||||||
|
assert result.ret == ExitCode.OK
|
||||||
|
result.stdout.fnmatch_lines(
|
||||||
|
["(1 duration slower than 0.100s hidden. Use -vv to show this duration.)"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_durations_zero_lists_all(pytester: Pytester) -> None:
|
||||||
|
pytester.makepyfile(
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
import time
|
||||||
|
@pytest.mark.parametrize('_', range(6))
|
||||||
|
def test_this(_):
|
||||||
|
time.sleep(0.1)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
result = pytester.runpytest("--durations=0")
|
||||||
|
assert result.ret == ExitCode.OK
|
||||||
|
# Check 6x setup & 6x teardown are hidden, but 6x call are shown
|
||||||
|
insertable_line = "*test_durations_zero_lists_all.py::test_this*{}*"
|
||||||
|
lines = [insertable_line.format(x) for x in range(6)]
|
||||||
|
lines.append(
|
||||||
|
"(12 durations slower than 0.005s hidden. Use -vv to show these durations.)"
|
||||||
|
)
|
||||||
|
result.stdout.fnmatch_lines_random(lines)
|
||||||
|
|
Loading…
Reference in New Issue