Skip to content

Commit

Permalink
Fix usages of "verbose" option
Browse files Browse the repository at this point in the history
With `-qq` `bool(config.getoption("verbose"))` is True; it needs to be
checked for `> 0`.
  • Loading branch information
blueyed committed Mar 22, 2019
1 parent 7764312 commit b617532
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion doc/en/builtin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a
Example::
def test_foo(pytestconfig):
if pytestconfig.getoption("verbose"):
if pytestconfig.getoption("verbose") > 0:
...
record_property
Add an extra properties the calling test.
Expand Down
18 changes: 9 additions & 9 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def isiterable(obj):
elif type(left) == type(right) and (isdatacls(left) or isattrs(left)):
type_fn = (isdatacls, isattrs)
explanation = _compare_eq_cls(left, right, verbose, type_fn)
elif verbose:
elif verbose > 0:
explanation = _compare_eq_verbose(left, right)
if isiterable(left) and isiterable(right):
expl = _compare_eq_iterable(left, right, verbose)
Expand All @@ -175,8 +175,8 @@ def isiterable(obj):
return [summary] + explanation


def _diff_text(left, right, verbose=False):
"""Return the explanation for the diff between text or bytes
def _diff_text(left, right, verbose=0):
"""Return the explanation for the diff between text or bytes.
Unless --verbose is used this will skip leading and trailing
characters which are identical to keep the diff minimal.
Expand All @@ -202,7 +202,7 @@ def escape_for_readable_diff(binary_text):
left = escape_for_readable_diff(left)
if isinstance(right, bytes):
right = escape_for_readable_diff(right)
if not verbose:
if verbose < 1:
i = 0 # just in case left or right has zero length
for i in range(min(len(left), len(right))):
if left[i] != right[i]:
Expand Down Expand Up @@ -250,7 +250,7 @@ def _compare_eq_verbose(left, right):
return explanation


def _compare_eq_iterable(left, right, verbose=False):
def _compare_eq_iterable(left, right, verbose=0):
if not verbose:
return [u"Use -v to get the full diff"]
# dynamic import to speedup pytest
Expand All @@ -273,7 +273,7 @@ def _compare_eq_iterable(left, right, verbose=False):
return explanation


def _compare_eq_sequence(left, right, verbose=False):
def _compare_eq_sequence(left, right, verbose=0):
explanation = []
for i in range(min(len(left), len(right))):
if left[i] != right[i]:
Expand All @@ -292,7 +292,7 @@ def _compare_eq_sequence(left, right, verbose=False):
return explanation


def _compare_eq_set(left, right, verbose=False):
def _compare_eq_set(left, right, verbose=0):
explanation = []
diff_left = left - right
diff_right = right - left
Expand All @@ -307,7 +307,7 @@ def _compare_eq_set(left, right, verbose=False):
return explanation


def _compare_eq_dict(left, right, verbose=False):
def _compare_eq_dict(left, right, verbose=0):
explanation = []
common = set(left).intersection(set(right))
same = {k: left[k] for k in common if left[k] == right[k]}
Expand Down Expand Up @@ -368,7 +368,7 @@ def _compare_eq_cls(left, right, verbose, type_fns):
return explanation


def _notin_text(term, text, verbose=False):
def _notin_text(term, text, verbose=0):
index = text.find(term)
head = text[:index]
tail = text[index + len(term) :]
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def cache(request):

def pytest_report_header(config):
"""Display cachedir with --cache-show and if non-default."""
if config.option.verbose or config.getini("cache_dir") != ".pytest_cache":
if config.option.verbose > 0 or config.getini("cache_dir") != ".pytest_cache":
cachedir = config.cache._cachedir
# TODO: evaluate generating upward relative paths
# starting with .., ../.. if sensible
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ def pytestconfig(request):
Example::
def test_foo(pytestconfig):
if pytestconfig.getoption("verbose"):
if pytestconfig.getoption("verbose") > 0:
...
"""
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def __init__(self, config):
self._config = config

# enable verbose output automatically if live logging is enabled
if self._log_cli_enabled() and not config.getoption("verbose"):
if self._log_cli_enabled() and config.getoption("verbose") < 1:
config.option.verbose = 1

self.print_logs = get_option_ini(config, "log_print")
Expand Down
19 changes: 10 additions & 9 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@


class Option(object):
def __init__(self, verbose=False, fulltrace=False):
self.verbose = verbose
def __init__(self, verbosity=0, fulltrace=False):
self.verbosity = verbosity
self.fulltrace = fulltrace

@property
def args(self):
values = []
if self.verbose:
values.append("-v")
values.append("--verbosity=%d" % self.verbosity)
if self.fulltrace:
values.append("--fulltrace")
return values


@pytest.fixture(
params=[
Option(verbose=False),
Option(verbose=True),
Option(verbose=-1),
Option(verbosity=0),
Option(verbosity=1),
Option(verbosity=-1),
Option(fulltrace=True),
],
ids=["default", "verbose", "quiet", "fulltrace"],
Expand Down Expand Up @@ -86,16 +85,18 @@ def test_func():
"""
)
result = testdir.runpytest(*option.args)
if option.verbose:
if option.verbosity > 0:
result.stdout.fnmatch_lines(
[
"*test_pass_skip_fail.py::test_ok PASS*",
"*test_pass_skip_fail.py::test_skip SKIP*",
"*test_pass_skip_fail.py::test_func FAIL*",
]
)
else:
elif option.verbosity == 0:
result.stdout.fnmatch_lines(["*test_pass_skip_fail.py .sF*"])
else:
result.stdout.fnmatch_lines([".sF*"])
result.stdout.fnmatch_lines(
[" def test_func():", "> assert 0", "E assert 0"]
)
Expand Down

0 comments on commit b617532

Please sign in to comment.