From e4d361b0935a62b50ff2a134961a9c89db8e7af0 Mon Sep 17 00:00:00 2001 From: Dmitry Malinovsky Date: Tue, 16 Feb 2016 11:42:04 +0600 Subject: [PATCH 1/2] LastFailed now creates .cache only when needed. Fixes #1342 --- _pytest/cacheprovider.py | 4 +++- testing/test_cache.py | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index e5c11a878..b593f1604 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -149,7 +149,9 @@ class LFPlugin: config = self.config if config.getvalue("cacheshow") or hasattr(config, "slaveinput"): return - config.cache.set("cache/lastfailed", self.lastfailed) + prev_failed = config.cache.get("cache/lastfailed", None) is not None + if (session.items and prev_failed) or self.lastfailed: + config.cache.set("cache/lastfailed", self.lastfailed) def pytest_addoption(parser): diff --git a/testing/test_cache.py b/testing/test_cache.py index 75557af38..98053f869 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -46,12 +46,12 @@ class TestNewAPI: def test_cache_failure_warns(self, testdir): testdir.tmpdir.ensure_dir('.cache').chmod(0) testdir.makepyfile(""" - def test_pass(): - pass + def test_error(): + raise Exception """) result = testdir.runpytest('-rw') - assert result.ret == 0 + assert result.ret == 1 result.stdout.fnmatch_lines([ "*could not create cache path*", "*1 pytest-warnings*", @@ -266,7 +266,7 @@ class TestLastFailed: """) config = testdir.parseconfigure() lastfailed = config.cache.get("cache/lastfailed", -1) - assert not lastfailed + assert lastfailed == -1 def test_non_serializable_parametrize(self, testdir): """Test that failed parametrized tests with unmarshable parameters @@ -305,7 +305,7 @@ class TestLastFailed: return lastfailed lastfailed = rlf(fail_import=0, fail_run=0) - assert not lastfailed + assert lastfailed == -1 lastfailed = rlf(fail_import=1, fail_run=0) assert list(lastfailed) == ['test_maybe.py'] @@ -347,7 +347,7 @@ class TestLastFailed: return result, lastfailed result, lastfailed = rlf(fail_import=0, fail_run=0) - assert not lastfailed + assert lastfailed == -1 result.stdout.fnmatch_lines([ '*3 passed*', ]) @@ -370,3 +370,17 @@ class TestLastFailed: result.stdout.fnmatch_lines([ '*2 passed*', ]) + + def test_lastfailed_creates_cache_when_needed(self, testdir): + # Issue #1342 + testdir.makepyfile(test_empty='') + testdir.runpytest('-q', '--lf') + assert not os.path.exists('.cache') + + testdir.makepyfile(test_successful='def test_success():\n assert True') + testdir.runpytest('-q', '--lf') + assert not os.path.exists('.cache') + + testdir.makepyfile(test_errored='def test_error():\n assert False') + testdir.runpytest('-q', '--lf') + assert os.path.exists('.cache') From 52a5acda922e8b2dbc8de91397a472d9acc293c9 Mon Sep 17 00:00:00 2001 From: Dmitry Malinovsky Date: Fri, 11 Mar 2016 14:48:17 +0600 Subject: [PATCH 2/2] Use testscollected to make xdist happy --- _pytest/cacheprovider.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index b593f1604..0657001f2 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -150,7 +150,7 @@ class LFPlugin: if config.getvalue("cacheshow") or hasattr(config, "slaveinput"): return prev_failed = config.cache.get("cache/lastfailed", None) is not None - if (session.items and prev_failed) or self.lastfailed: + if (session.testscollected and prev_failed) or self.lastfailed: config.cache.set("cache/lastfailed", self.lastfailed)