Skip to content

Commit bc913d1

Browse files
Streamline checks for verbose option (#12706) (#12778)
Instead of calling `Config.option.verbose`, call the new `Config.get_verbosity` function to determine the verbosity level. This enables pytest to run correctly with the terminal plugin disabled. Fix #9422 (cherry picked from commit 72c682f) Co-authored-by: GTowers1 <130098608+GTowers1@users.noreply.github.com>
1 parent 01cfcc9 commit bc913d1

File tree

11 files changed

+22
-12
lines changed

11 files changed

+22
-12
lines changed

changelog/9422.bugfix.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix bug where disabling the terminal plugin via ``-p no:terminal`` would cause crashes related to missing the ``verbose`` option.
2+
3+
-- by :user:`GTowers1`

doc/en/example/simple.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ display more information if applicable:
460460
461461
462462
def pytest_report_header(config):
463-
if config.getoption("verbose") > 0:
463+
if config.get_verbosity() > 0:
464464
return ["info1: did you know that ...", "did you?"]
465465
466466
which will add info only when run with "--v":

src/_pytest/cacheprovider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def get_last_failed_paths(self) -> set[Path]:
347347
return {x for x in result if x.exists()}
348348

349349
def pytest_report_collectionfinish(self) -> str | None:
350-
if self.active and self.config.getoption("verbose") >= 0:
350+
if self.active and self.config.get_verbosity() >= 0:
351351
return f"run-last-failure: {self._report_status}"
352352
return None
353353

src/_pytest/config/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ def get_verbosity(self, verbosity_type: str | None = None) -> int:
17441744
print(config.get_verbosity()) # 1
17451745
print(config.get_verbosity(Config.VERBOSITY_ASSERTIONS)) # 2
17461746
"""
1747-
global_level = self.option.verbose
1747+
global_level = self.getoption("verbose", default=0)
17481748
assert isinstance(global_level, int)
17491749
if verbosity_type is None:
17501750
return global_level

src/_pytest/fixtures.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ def pytestconfig(request: FixtureRequest) -> Config:
13481348
Example::
13491349
13501350
def test_foo(pytestconfig):
1351-
if pytestconfig.getoption("verbose") > 0:
1351+
if pytestconfig.get_verbosity() > 0:
13521352
...
13531353
13541354
"""
@@ -1807,7 +1807,7 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None:
18071807
session.perform_collect()
18081808
invocation_dir = config.invocation_params.dir
18091809
tw = _pytest.config.create_terminal_writer(config)
1810-
verbose = config.getvalue("verbose")
1810+
verbose = config.get_verbosity()
18111811

18121812
def get_best_relpath(func) -> str:
18131813
loc = getlocation(func, invocation_dir)
@@ -1866,7 +1866,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
18661866
session.perform_collect()
18671867
invocation_dir = config.invocation_params.dir
18681868
tw = _pytest.config.create_terminal_writer(config)
1869-
verbose = config.getvalue("verbose")
1869+
verbose = config.get_verbosity()
18701870

18711871
fm = session._fixturemanager
18721872

src/_pytest/logging.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ def pytest_runtestloop(self, session: Session) -> Generator[None, object, object
794794
if session.config.option.collectonly:
795795
return (yield)
796796

797-
if self._log_cli_enabled() and self._config.getoption("verbose") < 1:
797+
if self._log_cli_enabled() and self._config.get_verbosity() < 1:
798798
# The verbose flag is needed to avoid messy test progress output.
799799
self._config.option.verbose = 1
800800

src/_pytest/nodes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,12 @@ def _repr_failure_py(
435435
else:
436436
style = "long"
437437

438-
if self.config.getoption("verbose", 0) > 1:
438+
if self.config.get_verbosity() > 1:
439439
truncate_locals = False
440440
else:
441441
truncate_locals = True
442442

443-
truncate_args = False if self.config.getoption("verbose", 0) > 2 else True
443+
truncate_args = False if self.config.get_verbosity() > 2 else True
444444

445445
# excinfo.getrepr() formats paths relative to the CWD if `abspath` is False.
446446
# It is possible for a fixture/test to change the CWD while this code runs, which

src/_pytest/python.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def importtestmodule(
512512
) from e
513513
except ImportError as e:
514514
exc_info = ExceptionInfo.from_current()
515-
if config.getoption("verbose") < 2:
515+
if config.get_verbosity() < 2:
516516
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
517517
exc_repr = (
518518
exc_info.getrepr(style="short")

src/_pytest/runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def pytest_addoption(parser: Parser) -> None:
7171
def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None:
7272
durations = terminalreporter.config.option.durations
7373
durations_min = terminalreporter.config.option.durations_min
74-
verbose = terminalreporter.config.getvalue("verbose")
74+
verbose = terminalreporter.config.get_verbosity()
7575
if durations is None:
7676
return
7777
tr = terminalreporter

src/_pytest/stepwise.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
113113
self.lastfailed = None
114114

115115
def pytest_report_collectionfinish(self) -> str | None:
116-
if self.config.getoption("verbose") >= 0 and self.report_status:
116+
if self.config.get_verbosity() >= 0 and self.report_status:
117117
return f"stepwise: {self.report_status}"
118118
return None
119119

testing/acceptance_test.py

+7
Original file line numberDiff line numberDiff line change
@@ -1486,3 +1486,10 @@ def my_fixture(self, request):
14861486
raise AssertionError(
14871487
f"pytest command failed:\n{exc.stdout=!s}\n{exc.stderr=!s}"
14881488
) from exc
1489+
1490+
1491+
def test_no_terminal_plugin(pytester: Pytester) -> None:
1492+
"""Smoke test to ensure pytest can execute without the terminal plugin (#9422)."""
1493+
pytester.makepyfile("def test(): assert 1 == 2")
1494+
result = pytester.runpytest("-pno:terminal", "-s")
1495+
assert result.ret == ExitCode.TESTS_FAILED

0 commit comments

Comments
 (0)