Skip to content

Commit

Permalink
Merge pull request #2624 from nicoddemus/lf-report-after-collection
Browse files Browse the repository at this point in the history
Fix --last-failed reported items in terminal
  • Loading branch information
nicoddemus committed Jul 28, 2017
2 parents 1712196 + 7a12acb commit 768edde
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
13 changes: 9 additions & 4 deletions _pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ def __init__(self, config):
active_keys = 'lf', 'failedfirst'
self.active = any(config.getvalue(key) for key in active_keys)
self.lastfailed = config.cache.get("cache/lastfailed", {})
self._previously_failed_count = None

def pytest_report_header(self):
def pytest_report_collectionfinish(self):
if self.active:
if not self.lastfailed:
if not self._previously_failed_count:
mode = "run all (no recorded failures)"
else:
mode = "rerun previous failures%s" % (
" first" if self.config.getvalue("failedfirst") else "")
noun = 'failure' if self._previously_failed_count == 1 else 'failures'
suffix = " first" if self.config.getvalue("failedfirst") else ""
mode = "rerun previous {count} {noun}{suffix}".format(
count=self._previously_failed_count, suffix=suffix, noun=noun
)
return "run-last-failure: %s" % mode

def pytest_runtest_logreport(self, report):
Expand Down Expand Up @@ -142,6 +146,7 @@ def pytest_collection_modifyitems(self, session, config, items):
previously_failed.append(item)
else:
previously_passed.append(item)
self._previously_failed_count = len(previously_failed)
if not previously_failed:
# running a subset of all tests with recorded failures outside
# of the set of tests currently executing
Expand Down
5 changes: 4 additions & 1 deletion _pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ def pytest_report_header(config, startdir):


def pytest_report_collectionfinish(config, startdir, items):
""" return a string or list of strings to be displayed after collection has finished successfully.
"""
.. versionadded:: 3.2
return a string or list of strings to be displayed after collection has finished successfully.
This strings will be displayed after the standard "collected X items" message.
Expand Down
67 changes: 67 additions & 0 deletions testing/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,73 @@ def test_fail(val):
result = testdir.runpytest()
result.stdout.fnmatch_lines('*1 failed in*')

def test_terminal_report_lastfailed(self, testdir):
test_a = testdir.makepyfile(test_a="""
def test_a1():
pass
def test_a2():
pass
""")
test_b = testdir.makepyfile(test_b="""
def test_b1():
assert 0
def test_b2():
assert 0
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'collected 4 items',
'*2 failed, 2 passed in*',
])

result = testdir.runpytest('--lf')
result.stdout.fnmatch_lines([
'collected 4 items',
'run-last-failure: rerun previous 2 failures',
'*2 failed, 2 deselected in*',
])

result = testdir.runpytest(test_a, '--lf')
result.stdout.fnmatch_lines([
'collected 2 items',
'run-last-failure: run all (no recorded failures)',
'*2 passed in*',
])

result = testdir.runpytest(test_b, '--lf')
result.stdout.fnmatch_lines([
'collected 2 items',
'run-last-failure: rerun previous 2 failures',
'*2 failed in*',
])

result = testdir.runpytest('test_b.py::test_b1', '--lf')
result.stdout.fnmatch_lines([
'collected 1 item',
'run-last-failure: rerun previous 1 failure',
'*1 failed in*',
])

def test_terminal_report_failedfirst(self, testdir):
testdir.makepyfile(test_a="""
def test_a1():
assert 0
def test_a2():
pass
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'collected 2 items',
'*1 failed, 1 passed in*',
])

result = testdir.runpytest('--ff')
result.stdout.fnmatch_lines([
'collected 2 items',
'run-last-failure: rerun previous 1 failure first',
'*1 failed, 1 passed in*',
])

def test_lastfailed_collectfailure(self, testdir, monkeypatch):

testdir.makepyfile(test_maybe="""
Expand Down

0 comments on commit 768edde

Please sign in to comment.