Skip to content

Commit

Permalink
Display collect progress only when in a terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Feb 20, 2016
1 parent b76de91 commit 96a331e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@

*

*
* Collection only displays progress ("collecting X items") when in a terminal.
This avoids cluttering the output when using ``--color=yes`` to obtain
colors in CI integrations systems (`#1397`_).

*

Expand Down Expand Up @@ -106,6 +108,7 @@
.. _#1226: https://github.com/pytest-dev/pytest/pull/1226
.. _#1290: https://github.com/pytest-dev/pytest/pull/1290
.. _#1355: https://github.com/pytest-dev/pytest/pull/1355
.. _#1397: https://github.com/pytest-dev/pytest/issues/1397
.. _@biern: https://github.com/biern
.. _@MichaelAquilina: https://github.com/MichaelAquilina
.. _@bukzor: https://github.com/bukzor
Expand Down
7 changes: 4 additions & 3 deletions _pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(self, config, file=None):
self.currentfspath = None
self.reportchars = getreportopt(config)
self.hasmarkup = self._tw.hasmarkup
self.isatty = file.isatty()

def hasopt(self, char):
char = {'xfailed': 'x', 'skipped': 's'}.get(char, char)
Expand Down Expand Up @@ -234,7 +235,7 @@ def pytest_runtest_logreport(self, report):
self.currentfspath = -2

def pytest_collection(self):
if not self.hasmarkup and self.config.option.verbose >= 1:
if not self.isatty and self.config.option.verbose >= 1:
self.write("collecting ... ", bold=True)

def pytest_collectreport(self, report):
Expand All @@ -244,7 +245,7 @@ def pytest_collectreport(self, report):
self.stats.setdefault("skipped", []).append(report)
items = [x for x in report.result if isinstance(x, pytest.Item)]
self._numcollected += len(items)
if self.hasmarkup:
if self.isatty:
#self.write_fspath_result(report.nodeid, 'E')
self.report_collect()

Expand All @@ -263,7 +264,7 @@ def report_collect(self, final=False):
line += " / %d errors" % errors
if skipped:
line += " / %d skipped" % skipped
if self.hasmarkup:
if self.isatty:
if final:
line += " \n"
self.rewrite(line, bold=True)
Expand Down
25 changes: 25 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,37 @@ def test_color_yes(testdir):
assert 'test session starts' in result.stdout.str()
assert '\x1b[1m' in result.stdout.str()


def test_color_no(testdir):
testdir.makepyfile("def test_this(): assert 1")
result = testdir.runpytest('--color=no')
assert 'test session starts' in result.stdout.str()
assert '\x1b[1m' not in result.stdout.str()


@pytest.mark.parametrize('verbose', [True, False])
def test_color_yes_collection_on_non_atty(testdir, verbose):
"""skip collect progress report when working on non-terminals.
#1397
"""
testdir.makepyfile("""
import pytest
@pytest.mark.parametrize('i', range(10))
def test_this(i):
assert 1
""")
args = ['--color=yes']
if verbose:
args.append('-vv')
result = testdir.runpytest(*args)
assert 'test session starts' in result.stdout.str()
assert '\x1b[1m' in result.stdout.str()
assert 'collecting 10 items' not in result.stdout.str()
if verbose:
assert 'collecting ...' in result.stdout.str()
assert 'collected 10 items' in result.stdout.str()


def test_getreportopt():
class config:
class option:
Expand Down

0 comments on commit 96a331e

Please sign in to comment.