Skip to content

Commit

Permalink
Streamline checks for verbose option (#12706)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
GTowers1 committed Sep 5, 2024
1 parent 09e386e commit 72c682f
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 12 deletions.
3 changes: 3 additions & 0 deletions changelog/9422.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug where disabling the terminal plugin via ``-p no:terminal`` would cause crashes related to missing the ``verbose`` option.

-- by :user:`GTowers1`
2 changes: 1 addition & 1 deletion doc/en/example/simple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ display more information if applicable:
def pytest_report_header(config):
if config.getoption("verbose") > 0:
if config.get_verbosity() > 0:
return ["info1: did you know that ...", "did you?"]
which will add info only when run with "--v":
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def get_last_failed_paths(self) -> set[Path]:
return {x for x in result if x.exists()}

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

Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@ def get_verbosity(self, verbosity_type: str | None = None) -> int:
print(config.get_verbosity()) # 1
print(config.get_verbosity(Config.VERBOSITY_ASSERTIONS)) # 2
"""
global_level = self.option.verbose
global_level = self.getoption("verbose", default=0)
assert isinstance(global_level, int)
if verbosity_type is None:
return global_level
Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ def pytestconfig(request: FixtureRequest) -> Config:
Example::
def test_foo(pytestconfig):
if pytestconfig.getoption("verbose") > 0:
if pytestconfig.get_verbosity() > 0:
...
"""
Expand Down Expand Up @@ -1807,7 +1807,7 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None:
session.perform_collect()
invocation_dir = config.invocation_params.dir
tw = _pytest.config.create_terminal_writer(config)
verbose = config.getvalue("verbose")
verbose = config.get_verbosity()

def get_best_relpath(func) -> str:
loc = getlocation(func, invocation_dir)
Expand Down Expand Up @@ -1866,7 +1866,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
session.perform_collect()
invocation_dir = config.invocation_params.dir
tw = _pytest.config.create_terminal_writer(config)
verbose = config.getvalue("verbose")
verbose = config.get_verbosity()

fm = session._fixturemanager

Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def pytest_runtestloop(self, session: Session) -> Generator[None, object, object
if session.config.option.collectonly:
return (yield)

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

Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,12 @@ def _repr_failure_py(
else:
style = "long"

if self.config.getoption("verbose", 0) > 1:
if self.config.get_verbosity() > 1:
truncate_locals = False
else:
truncate_locals = True

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

# excinfo.getrepr() formats paths relative to the CWD if `abspath` is False.
# It is possible for a fixture/test to change the CWD while this code runs, which
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def importtestmodule(
) from e
except ImportError as e:
exc_info = ExceptionInfo.from_current()
if config.getoption("verbose") < 2:
if config.get_verbosity() < 2:
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
exc_repr = (
exc_info.getrepr(style="short")
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def pytest_addoption(parser: Parser) -> None:
def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None:
durations = terminalreporter.config.option.durations
durations_min = terminalreporter.config.option.durations_min
verbose = terminalreporter.config.getvalue("verbose")
verbose = terminalreporter.config.get_verbosity()
if durations is None:
return
tr = terminalreporter
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/stepwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
self.lastfailed = None

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

Expand Down
7 changes: 7 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,3 +1486,10 @@ def my_fixture(self, request):
raise AssertionError(
f"pytest command failed:\n{exc.stdout=!s}\n{exc.stderr=!s}"
) from exc


def test_no_terminal_plugin(pytester: Pytester) -> None:
"""Smoke test to ensure pytest can execute without the terminal plugin (#9422)."""
pytester.makepyfile("def test(): assert 1 == 2")
result = pytester.runpytest("-pno:terminal", "-s")
assert result.ret == ExitCode.TESTS_FAILED

0 comments on commit 72c682f

Please sign in to comment.