diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4f6b2d016..e89a40954 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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`_). * @@ -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 diff --git a/_pytest/terminal.py b/_pytest/terminal.py index e17b8dda2..825f553ef 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -112,6 +112,7 @@ class TerminalReporter: 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) @@ -234,7 +235,7 @@ class TerminalReporter: 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): @@ -244,7 +245,7 @@ class TerminalReporter: 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() @@ -263,7 +264,7 @@ class TerminalReporter: 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) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index df9cdc040..b43d6b379 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -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: