Skip to content

Commit 3656885

Browse files
authored
Fix verbosity bug in --collect-only (#5391)
Fix verbosity bug in --collect-only
2 parents be84ba8 + 577b0df commit 3656885

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

changelog/5383.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``-q`` has again an impact on the style of the collected items
2+
(``--collect-only``) when ``--log-cli-level`` is used.

src/_pytest/logging.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,6 @@ def __init__(self, config):
409409
"""
410410
self._config = config
411411

412-
# enable verbose output automatically if live logging is enabled
413-
if self._log_cli_enabled() and config.getoption("verbose") < 1:
414-
config.option.verbose = 1
415-
416412
self.print_logs = get_option_ini(config, "log_print")
417413
self.formatter = self._create_formatter(
418414
get_option_ini(config, "log_format"),
@@ -628,6 +624,15 @@ def pytest_sessionstart(self):
628624
@pytest.hookimpl(hookwrapper=True)
629625
def pytest_runtestloop(self, session):
630626
"""Runs all collected test items."""
627+
628+
if session.config.option.collectonly:
629+
yield
630+
return
631+
632+
if self._log_cli_enabled() and self._config.getoption("verbose") < 1:
633+
# setting verbose flag is needed to avoid messy test progress output
634+
self._config.option.verbose = 1
635+
631636
with self.live_logs_context():
632637
if self.log_file_handler is not None:
633638
with catching_logs(self.log_file_handler, level=self.log_file_level):

testing/logging/test_reporting.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -916,14 +916,45 @@ def test_collection_live_logging(testdir):
916916

917917
result = testdir.runpytest("--log-cli-level=INFO")
918918
result.stdout.fnmatch_lines(
919-
[
920-
"collecting*",
921-
"*--- live log collection ---*",
922-
"*Normal message*",
923-
"collected 0 items",
924-
]
919+
["*--- live log collection ---*", "*Normal message*", "collected 0 items"]
920+
)
921+
922+
923+
@pytest.mark.parametrize("verbose", ["", "-q", "-qq"])
924+
def test_collection_collect_only_live_logging(testdir, verbose):
925+
testdir.makepyfile(
926+
"""
927+
def test_simple():
928+
pass
929+
"""
925930
)
926931

932+
result = testdir.runpytest("--collect-only", "--log-cli-level=INFO", verbose)
933+
934+
expected_lines = []
935+
936+
if not verbose:
937+
expected_lines.extend(
938+
[
939+
"*collected 1 item*",
940+
"*<Module test_collection_collect_only_live_logging.py>*",
941+
"*no tests ran*",
942+
]
943+
)
944+
elif verbose == "-q":
945+
assert "collected 1 item*" not in result.stdout.str()
946+
expected_lines.extend(
947+
[
948+
"*test_collection_collect_only_live_logging.py::test_simple*",
949+
"no tests ran in * seconds",
950+
]
951+
)
952+
elif verbose == "-qq":
953+
assert "collected 1 item*" not in result.stdout.str()
954+
expected_lines.extend(["*test_collection_collect_only_live_logging.py: 1*"])
955+
956+
result.stdout.fnmatch_lines(expected_lines)
957+
927958

928959
def test_collection_logging_to_file(testdir):
929960
log_file = testdir.tmpdir.join("pytest.log").strpath

0 commit comments

Comments
 (0)