Skip to content

Commit a2fa70d

Browse files
committed
Display collect progress only when in a terminal
Fix #1397
1 parent b76de91 commit a2fa70d

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272

7373
*
7474

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

7779
*
7880

@@ -106,6 +108,7 @@
106108
.. _#1226: https://github.com/pytest-dev/pytest/pull/1226
107109
.. _#1290: https://github.com/pytest-dev/pytest/pull/1290
108110
.. _#1355: https://github.com/pytest-dev/pytest/pull/1355
111+
.. _#1397: https://github.com/pytest-dev/pytest/issues/1397
109112
.. _@biern: https://github.com/biern
110113
.. _@MichaelAquilina: https://github.com/MichaelAquilina
111114
.. _@bukzor: https://github.com/bukzor

_pytest/terminal.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def __init__(self, config, file=None):
112112
self.currentfspath = None
113113
self.reportchars = getreportopt(config)
114114
self.hasmarkup = self._tw.hasmarkup
115+
self.isatty = file.isatty()
115116

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

236237
def pytest_collection(self):
237-
if not self.hasmarkup and self.config.option.verbose >= 1:
238+
if not self.isatty and self.config.option.verbose >= 1:
238239
self.write("collecting ... ", bold=True)
239240

240241
def pytest_collectreport(self, report):
@@ -244,7 +245,7 @@ def pytest_collectreport(self, report):
244245
self.stats.setdefault("skipped", []).append(report)
245246
items = [x for x in report.result if isinstance(x, pytest.Item)]
246247
self._numcollected += len(items)
247-
if self.hasmarkup:
248+
if self.isatty:
248249
#self.write_fspath_result(report.nodeid, 'E')
249250
self.report_collect()
250251

@@ -263,7 +264,7 @@ def report_collect(self, final=False):
263264
line += " / %d errors" % errors
264265
if skipped:
265266
line += " / %d skipped" % skipped
266-
if self.hasmarkup:
267+
if self.isatty:
267268
if final:
268269
line += " \n"
269270
self.rewrite(line, bold=True)

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ install:
2222
build: false # Not a C# project, build stuff at the test step instead.
2323

2424
test_script:
25-
- C:\Python35\python -m tox
25+
- C:\Python35\python -m tox -- --color=yes
2626
# coveralls is not in tox's envlist, plus for PRs the secure variable
2727
# is not defined so we have to check for it
2828
- if defined COVERALLS_REPO_TOKEN C:\Python35\python -m tox -e coveralls

testing/test_terminal.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,37 @@ def test_color_yes(testdir):
561561
assert 'test session starts' in result.stdout.str()
562562
assert '\x1b[1m' in result.stdout.str()
563563

564+
564565
def test_color_no(testdir):
565566
testdir.makepyfile("def test_this(): assert 1")
566567
result = testdir.runpytest('--color=no')
567568
assert 'test session starts' in result.stdout.str()
568569
assert '\x1b[1m' not in result.stdout.str()
569570

571+
572+
@pytest.mark.parametrize('verbose', [True, False])
573+
def test_color_yes_collection_on_non_atty(testdir, verbose):
574+
"""skip collect progress report when working on non-terminals.
575+
#1397
576+
"""
577+
testdir.makepyfile("""
578+
import pytest
579+
@pytest.mark.parametrize('i', range(10))
580+
def test_this(i):
581+
assert 1
582+
""")
583+
args = ['--color=yes']
584+
if verbose:
585+
args.append('-vv')
586+
result = testdir.runpytest(*args)
587+
assert 'test session starts' in result.stdout.str()
588+
assert '\x1b[1m' in result.stdout.str()
589+
assert 'collecting 10 items' not in result.stdout.str()
590+
if verbose:
591+
assert 'collecting ...' in result.stdout.str()
592+
assert 'collected 10 items' in result.stdout.str()
593+
594+
570595
def test_getreportopt():
571596
class config:
572597
class option:

0 commit comments

Comments
 (0)