Skip to content

Commit e4d361b

Browse files
committed
LastFailed now creates .cache only when needed. Fixes #1342
1 parent bed56e5 commit e4d361b

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

_pytest/cacheprovider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ def pytest_sessionfinish(self, session):
149149
config = self.config
150150
if config.getvalue("cacheshow") or hasattr(config, "slaveinput"):
151151
return
152-
config.cache.set("cache/lastfailed", self.lastfailed)
152+
prev_failed = config.cache.get("cache/lastfailed", None) is not None
153+
if (session.items and prev_failed) or self.lastfailed:
154+
config.cache.set("cache/lastfailed", self.lastfailed)
153155

154156

155157
def pytest_addoption(parser):

testing/test_cache.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def test_cache_writefail_permissions(self, testdir):
4646
def test_cache_failure_warns(self, testdir):
4747
testdir.tmpdir.ensure_dir('.cache').chmod(0)
4848
testdir.makepyfile("""
49-
def test_pass():
50-
pass
49+
def test_error():
50+
raise Exception
5151
5252
""")
5353
result = testdir.runpytest('-rw')
54-
assert result.ret == 0
54+
assert result.ret == 1
5555
result.stdout.fnmatch_lines([
5656
"*could not create cache path*",
5757
"*1 pytest-warnings*",
@@ -266,7 +266,7 @@ def test_hello():
266266
""")
267267
config = testdir.parseconfigure()
268268
lastfailed = config.cache.get("cache/lastfailed", -1)
269-
assert not lastfailed
269+
assert lastfailed == -1
270270

271271
def test_non_serializable_parametrize(self, testdir):
272272
"""Test that failed parametrized tests with unmarshable parameters
@@ -305,7 +305,7 @@ def rlf(fail_import, fail_run):
305305
return lastfailed
306306

307307
lastfailed = rlf(fail_import=0, fail_run=0)
308-
assert not lastfailed
308+
assert lastfailed == -1
309309

310310
lastfailed = rlf(fail_import=1, fail_run=0)
311311
assert list(lastfailed) == ['test_maybe.py']
@@ -347,7 +347,7 @@ def rlf(fail_import, fail_run, args=()):
347347
return result, lastfailed
348348

349349
result, lastfailed = rlf(fail_import=0, fail_run=0)
350-
assert not lastfailed
350+
assert lastfailed == -1
351351
result.stdout.fnmatch_lines([
352352
'*3 passed*',
353353
])
@@ -370,3 +370,17 @@ def rlf(fail_import, fail_run, args=()):
370370
result.stdout.fnmatch_lines([
371371
'*2 passed*',
372372
])
373+
374+
def test_lastfailed_creates_cache_when_needed(self, testdir):
375+
# Issue #1342
376+
testdir.makepyfile(test_empty='')
377+
testdir.runpytest('-q', '--lf')
378+
assert not os.path.exists('.cache')
379+
380+
testdir.makepyfile(test_successful='def test_success():\n assert True')
381+
testdir.runpytest('-q', '--lf')
382+
assert not os.path.exists('.cache')
383+
384+
testdir.makepyfile(test_errored='def test_error():\n assert False')
385+
testdir.runpytest('-q', '--lf')
386+
assert os.path.exists('.cache')

0 commit comments

Comments
 (0)