2018-10-25 15:01:29 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
2018-06-17 22:23:44 +08:00
|
|
|
|
2018-10-25 15:01:29 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
2015-09-23 02:49:11 +08:00
|
|
|
import sys
|
2018-08-24 00:06:17 +08:00
|
|
|
import textwrap
|
2018-06-17 22:23:44 +08:00
|
|
|
|
2017-07-07 18:07:33 +08:00
|
|
|
import py
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2015-07-11 16:13:27 +08:00
|
|
|
import pytest
|
2019-04-03 21:18:29 +08:00
|
|
|
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
2015-07-11 16:13:27 +08:00
|
|
|
|
2018-06-21 20:13:10 +08:00
|
|
|
pytest_plugins = ("pytester",)
|
2015-07-11 16:13:27 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestNewAPI(object):
|
2015-07-11 16:13:27 +08:00
|
|
|
def test_config_cache_makedir(self, testdir):
|
|
|
|
testdir.makeini("[pytest]")
|
|
|
|
config = testdir.parseconfigure()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
config.cache.makedir("key/name")
|
|
|
|
|
|
|
|
p = config.cache.makedir("name")
|
|
|
|
assert p.check()
|
|
|
|
|
|
|
|
def test_config_cache_dataerror(self, testdir):
|
|
|
|
testdir.makeini("[pytest]")
|
|
|
|
config = testdir.parseconfigure()
|
|
|
|
cache = config.cache
|
|
|
|
pytest.raises(TypeError, lambda: cache.set("key/name", cache))
|
|
|
|
config.cache.set("key/name", 0)
|
2018-06-17 22:23:44 +08:00
|
|
|
config.cache._getvaluepath("key/name").write_bytes(b"123invalid")
|
2015-07-11 16:13:27 +08:00
|
|
|
val = config.cache.get("key/name", -2)
|
|
|
|
assert val == -2
|
|
|
|
|
2018-09-02 08:58:48 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2015-09-22 22:28:19 +08:00
|
|
|
def test_cache_writefail_cachfile_silent(self, testdir):
|
|
|
|
testdir.makeini("[pytest]")
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join(".pytest_cache").write("gone wrong")
|
2015-09-22 22:28:19 +08:00
|
|
|
config = testdir.parseconfigure()
|
|
|
|
cache = config.cache
|
2018-05-23 22:48:46 +08:00
|
|
|
cache.set("test/broken", [])
|
2015-09-22 22:28:19 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows")
|
2018-09-05 02:06:14 +08:00
|
|
|
@pytest.mark.filterwarnings(
|
|
|
|
"ignore:could not create cache path:pytest.PytestWarning"
|
|
|
|
)
|
2015-09-23 02:24:37 +08:00
|
|
|
def test_cache_writefail_permissions(self, testdir):
|
2015-09-22 22:28:19 +08:00
|
|
|
testdir.makeini("[pytest]")
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0)
|
2015-09-22 22:28:19 +08:00
|
|
|
config = testdir.parseconfigure()
|
|
|
|
cache = config.cache
|
2018-05-23 22:48:46 +08:00
|
|
|
cache.set("test/broken", [])
|
2015-09-22 22:28:19 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows")
|
2018-09-05 01:44:02 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2015-09-23 02:24:37 +08:00
|
|
|
def test_cache_failure_warns(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-16 13:42:04 +08:00
|
|
|
def test_error():
|
|
|
|
raise Exception
|
2015-09-23 02:24:37 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-rw")
|
2016-02-16 13:42:04 +08:00
|
|
|
assert result.ret == 1
|
2018-10-16 03:39:51 +08:00
|
|
|
# warnings from nodeids, lastfailed, and stepwise
|
|
|
|
result.stdout.fnmatch_lines(["*could not create cache path*", "*3 warnings*"])
|
2015-09-22 22:28:19 +08:00
|
|
|
|
2015-07-11 16:13:27 +08:00
|
|
|
def test_config_cache(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2015-07-11 16:13:27 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
# see that we get cache information early on
|
|
|
|
assert hasattr(config, "cache")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-07-11 16:13:27 +08:00
|
|
|
def test_session(pytestconfig):
|
|
|
|
assert hasattr(pytestconfig, "cache")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-11 16:13:27 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2015-07-25 02:32:50 +08:00
|
|
|
def test_cachefuncarg(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-07-11 16:13:27 +08:00
|
|
|
import pytest
|
|
|
|
def test_cachefuncarg(cache):
|
|
|
|
val = cache.get("some/thing", None)
|
|
|
|
assert val is None
|
|
|
|
cache.set("some/thing", [1])
|
|
|
|
pytest.raises(TypeError, lambda: cache.get("some/thing"))
|
|
|
|
val = cache.get("some/thing", [])
|
|
|
|
assert val == [1]
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-11 16:13:27 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2017-07-07 18:07:33 +08:00
|
|
|
def test_custom_rel_cache_dir(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
rel_cache_dir = os.path.join("custom_cache_dir", "subdir")
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2017-07-07 18:07:33 +08:00
|
|
|
[pytest]
|
|
|
|
cache_dir = {cache_dir}
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
cache_dir=rel_cache_dir
|
|
|
|
)
|
|
|
|
)
|
|
|
|
testdir.makepyfile(test_errored="def test_error():\n assert False")
|
2017-07-07 18:07:33 +08:00
|
|
|
testdir.runpytest()
|
|
|
|
assert testdir.tmpdir.join(rel_cache_dir).isdir()
|
|
|
|
|
|
|
|
def test_custom_abs_cache_dir(self, testdir, tmpdir_factory):
|
2018-05-23 22:48:46 +08:00
|
|
|
tmp = str(tmpdir_factory.mktemp("tmp"))
|
|
|
|
abs_cache_dir = os.path.join(tmp, "custom_cache_dir")
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2017-07-07 18:07:33 +08:00
|
|
|
[pytest]
|
|
|
|
cache_dir = {cache_dir}
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
cache_dir=abs_cache_dir
|
|
|
|
)
|
|
|
|
)
|
|
|
|
testdir.makepyfile(test_errored="def test_error():\n assert False")
|
2017-07-07 18:07:33 +08:00
|
|
|
testdir.runpytest()
|
|
|
|
assert py.path.local(abs_cache_dir).isdir()
|
|
|
|
|
|
|
|
def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch):
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setenv("env_var", "custom_cache_dir")
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2017-07-07 18:07:33 +08:00
|
|
|
[pytest]
|
|
|
|
cache_dir = {cache_dir}
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
cache_dir="$env_var"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
testdir.makepyfile(test_errored="def test_error():\n assert False")
|
2017-07-07 18:07:33 +08:00
|
|
|
testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert testdir.tmpdir.join("custom_cache_dir").isdir()
|
2015-07-11 16:13:27 +08:00
|
|
|
|
2017-07-18 08:16:14 +08:00
|
|
|
|
2018-10-31 05:35:09 +08:00
|
|
|
@pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir")))
|
|
|
|
def test_cache_reportheader(env, testdir, monkeypatch):
|
|
|
|
testdir.makepyfile("""def test_foo(): pass""")
|
|
|
|
if env:
|
|
|
|
monkeypatch.setenv(*env)
|
|
|
|
expected = os.path.join(env[1], ".pytest_cache")
|
|
|
|
else:
|
|
|
|
monkeypatch.delenv("TOX_ENV_DIR", raising=False)
|
|
|
|
expected = ".pytest_cache"
|
2015-07-11 16:13:27 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2018-10-31 05:35:09 +08:00
|
|
|
result.stdout.fnmatch_lines(["cachedir: %s" % expected])
|
2015-07-11 16:13:27 +08:00
|
|
|
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2018-08-01 14:25:37 +08:00
|
|
|
def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):
|
|
|
|
external_cache = tmpdir_factory.mktemp(
|
|
|
|
"test_cache_reportheader_external_abspath_abs"
|
|
|
|
)
|
|
|
|
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
cache_dir = {abscache}
|
|
|
|
""".format(
|
|
|
|
abscache=external_cache
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["cachedir: {abscache}".format(abscache=external_cache)]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-11 16:13:27 +08:00
|
|
|
def test_cache_show(testdir):
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--cache-show")
|
2015-07-11 16:13:27 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*cache is empty*"])
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2015-07-11 16:13:27 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
config.cache.set("my/name", [1,2,3])
|
|
|
|
config.cache.set("other/some", {1:2})
|
|
|
|
dp = config.cache.makedir("mydb")
|
|
|
|
dp.ensure("hello")
|
|
|
|
dp.ensure("world")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-11 16:13:27 +08:00
|
|
|
result = testdir.runpytest()
|
2015-08-28 01:22:22 +08:00
|
|
|
assert result.ret == 5 # no tests executed
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--cache-show")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines_random(
|
|
|
|
[
|
|
|
|
"*cachedir:*",
|
|
|
|
"-*cache values*-",
|
|
|
|
"*my/name contains:",
|
|
|
|
" [1, 2, 3]",
|
|
|
|
"*other/some contains*",
|
|
|
|
" {*1*: 2}",
|
|
|
|
"-*cache directories*-",
|
|
|
|
"*mydb/hello*length 0*",
|
|
|
|
"*mydb/world*length 0*",
|
|
|
|
]
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestLastFailed(object):
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_lastfailed_usecase(self, testdir, monkeypatch):
|
2018-10-02 05:48:41 +08:00
|
|
|
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_1():
|
|
|
|
assert 0
|
|
|
|
def test_2():
|
|
|
|
assert 0
|
|
|
|
def test_3():
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
|
|
|
p.write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_1():
|
|
|
|
assert 1
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_2():
|
|
|
|
assert 1
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_3():
|
|
|
|
assert 0
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*1 desel*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf")
|
2019-04-03 21:18:29 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 3 items",
|
|
|
|
"run-last-failure: no previously failed tests, not deselecting items.",
|
|
|
|
"*1 failed*2 passed*",
|
|
|
|
]
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf", "--cache-clear")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
# Run this again to make sure clear-cache is robust
|
2018-05-23 22:48:46 +08:00
|
|
|
if os.path.isdir(".pytest_cache"):
|
|
|
|
shutil.rmtree(".pytest_cache")
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf", "--cache-clear")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
def test_failedfirst_order(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("test_a.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_always_passes():
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
testdir.tmpdir.join("test_b.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_always_fails():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
# Test order will be collection order; alphabetical
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"])
|
2017-04-10 16:42:19 +08:00
|
|
|
result = testdir.runpytest("--ff")
|
2015-09-17 02:41:22 +08:00
|
|
|
# Test order will be failing tests firs
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["test_b.py*", "test_a.py*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2017-04-10 16:42:19 +08:00
|
|
|
def test_lastfailed_failedfirst_order(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
2018-08-24 00:06:17 +08:00
|
|
|
"test_a.py": """\
|
2017-04-12 04:55:55 +08:00
|
|
|
def test_always_passes():
|
|
|
|
assert 1
|
2018-08-24 00:06:17 +08:00
|
|
|
""",
|
|
|
|
"test_b.py": """\
|
2017-04-12 04:55:55 +08:00
|
|
|
def test_always_fails():
|
|
|
|
assert 0
|
2018-08-24 00:06:17 +08:00
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
}
|
|
|
|
)
|
2017-04-10 16:42:19 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
# Test order will be collection order; alphabetical
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"])
|
2017-04-10 16:42:19 +08:00
|
|
|
result = testdir.runpytest("--lf", "--ff")
|
|
|
|
# Test order will be failing tests firs
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["test_b.py*"])
|
|
|
|
assert "test_a.py" not in result.stdout.str()
|
2017-04-10 16:42:19 +08:00
|
|
|
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_lastfailed_difference_invocations(self, testdir, monkeypatch):
|
2018-10-02 05:48:41 +08:00
|
|
|
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
test_a="""\
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_a1():
|
|
|
|
assert 0
|
|
|
|
def test_a2():
|
|
|
|
assert 1
|
2018-08-24 00:06:17 +08:00
|
|
|
""",
|
|
|
|
test_b="""\
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_b1():
|
|
|
|
assert 0
|
2018-08-24 00:06:17 +08:00
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
p = testdir.tmpdir.join("test_a.py")
|
|
|
|
p2 = testdir.tmpdir.join("test_b.py")
|
|
|
|
|
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf", p2)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*"])
|
|
|
|
p2.write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_b1():
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf", p2)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf", p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*1 desel*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
def test_lastfailed_usecase_splice(self, testdir, monkeypatch):
|
2018-10-02 05:48:41 +08:00
|
|
|
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_1():
|
|
|
|
assert 0
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
p2 = testdir.tmpdir.join("test_something.py")
|
2018-05-23 22:48:46 +08:00
|
|
|
p2.write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_2():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf", p2)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
result = testdir.runpytest("--lf")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
def test_lastfailed_xpass(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.inline_runsource(
|
|
|
|
"""
|
2015-09-17 02:41:22 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_hello():
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
config = testdir.parseconfigure()
|
|
|
|
lastfailed = config.cache.get("cache/lastfailed", -1)
|
2016-02-16 13:42:04 +08:00
|
|
|
assert lastfailed == -1
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2015-09-21 13:39:34 +08:00
|
|
|
def test_non_serializable_parametrize(self, testdir):
|
|
|
|
"""Test that failed parametrized tests with unmarshable parameters
|
|
|
|
don't break pytest-cache.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
r"""
|
2015-09-21 13:39:34 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('val', [
|
|
|
|
b'\xac\x10\x02G',
|
|
|
|
])
|
|
|
|
def test_fail(val):
|
|
|
|
assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-21 13:39:34 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed in*"])
|
2015-09-21 13:39:34 +08:00
|
|
|
|
2017-07-28 02:06:35 +08:00
|
|
|
def test_terminal_report_lastfailed(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
test_a = testdir.makepyfile(
|
|
|
|
test_a="""
|
2017-07-28 02:06:35 +08:00
|
|
|
def test_a1():
|
|
|
|
pass
|
|
|
|
def test_a2():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
test_b = testdir.makepyfile(
|
|
|
|
test_b="""
|
2017-07-28 02:06:35 +08:00
|
|
|
def test_b1():
|
|
|
|
assert 0
|
|
|
|
def test_b2():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-28 02:06:35 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 4 items", "*2 failed, 2 passed in*"])
|
|
|
|
|
|
|
|
result = testdir.runpytest("--lf")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2019-01-24 07:08:43 +08:00
|
|
|
"collected 4 items / 2 deselected / 2 selected",
|
2018-05-23 22:48:46 +08:00
|
|
|
"run-last-failure: rerun previous 2 failures",
|
|
|
|
"*2 failed, 2 deselected in*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
result = testdir.runpytest(test_a, "--lf")
|
2019-04-03 21:18:29 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 2 items",
|
|
|
|
"run-last-failure: 2 known failures not in selected tests",
|
|
|
|
"*2 passed in*",
|
|
|
|
]
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
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*",
|
|
|
|
]
|
|
|
|
)
|
2017-07-28 02:06:35 +08:00
|
|
|
|
|
|
|
def test_terminal_report_failedfirst(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_a="""
|
2017-07-28 02:06:35 +08:00
|
|
|
def test_a1():
|
|
|
|
assert 0
|
|
|
|
def test_a2():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-28 02:06:35 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
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*",
|
|
|
|
]
|
|
|
|
)
|
2017-07-28 02:06:35 +08:00
|
|
|
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_lastfailed_collectfailure(self, testdir, monkeypatch):
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_maybe="""
|
2017-12-27 11:47:26 +08:00
|
|
|
import os
|
|
|
|
env = os.environ
|
2015-09-17 02:41:22 +08:00
|
|
|
if '1' == env['FAILIMPORT']:
|
|
|
|
raise ImportError('fail')
|
|
|
|
def test_hello():
|
|
|
|
assert '0' == env['FAILTEST']
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
def rlf(fail_import, fail_run):
|
2018-10-02 05:48:41 +08:00
|
|
|
monkeypatch.setenv("FAILIMPORT", str(fail_import))
|
|
|
|
monkeypatch.setenv("FAILTEST", str(fail_run))
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.runpytest("-q")
|
2015-09-17 02:41:22 +08:00
|
|
|
config = testdir.parseconfigure()
|
|
|
|
lastfailed = config.cache.get("cache/lastfailed", -1)
|
|
|
|
return lastfailed
|
|
|
|
|
|
|
|
lastfailed = rlf(fail_import=0, fail_run=0)
|
2016-02-16 13:42:04 +08:00
|
|
|
assert lastfailed == -1
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
lastfailed = rlf(fail_import=1, fail_run=0)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert list(lastfailed) == ["test_maybe.py"]
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
lastfailed = rlf(fail_import=0, fail_run=1)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert list(lastfailed) == ["test_maybe.py::test_hello"]
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
def test_lastfailed_failure_subset(self, testdir, monkeypatch):
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_maybe="""
|
2017-12-27 11:47:26 +08:00
|
|
|
import os
|
|
|
|
env = os.environ
|
2015-09-17 02:41:22 +08:00
|
|
|
if '1' == env['FAILIMPORT']:
|
|
|
|
raise ImportError('fail')
|
|
|
|
def test_hello():
|
|
|
|
assert '0' == env['FAILTEST']
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_maybe2="""
|
2017-12-27 11:47:26 +08:00
|
|
|
import os
|
|
|
|
env = os.environ
|
2015-09-17 02:41:22 +08:00
|
|
|
if '1' == env['FAILIMPORT']:
|
|
|
|
raise ImportError('fail')
|
|
|
|
def test_hello():
|
|
|
|
assert '0' == env['FAILTEST']
|
|
|
|
|
|
|
|
def test_pass():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
def rlf(fail_import, fail_run, args=()):
|
2018-10-02 05:48:41 +08:00
|
|
|
monkeypatch.setenv("FAILIMPORT", str(fail_import))
|
|
|
|
monkeypatch.setenv("FAILTEST", str(fail_run))
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("-q", "--lf", *args)
|
2015-09-17 02:41:22 +08:00
|
|
|
config = testdir.parseconfigure()
|
|
|
|
lastfailed = config.cache.get("cache/lastfailed", -1)
|
|
|
|
return result, lastfailed
|
|
|
|
|
|
|
|
result, lastfailed = rlf(fail_import=0, fail_run=0)
|
2016-02-16 13:42:04 +08:00
|
|
|
assert lastfailed == -1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
result, lastfailed = rlf(fail_import=1, fail_run=0)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert sorted(list(lastfailed)) == ["test_maybe.py", "test_maybe2.py"]
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",))
|
|
|
|
assert list(lastfailed) == ["test_maybe.py"]
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
# edge case of test selection - even if we remember failures
|
|
|
|
# from other tests we still need to run all tests if no test
|
|
|
|
# matches the failures
|
2018-05-23 22:48:46 +08:00
|
|
|
result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",))
|
|
|
|
assert list(lastfailed) == ["test_maybe.py"]
|
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2016-02-16 13:42:04 +08:00
|
|
|
|
|
|
|
def test_lastfailed_creates_cache_when_needed(self, testdir):
|
|
|
|
# Issue #1342
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(test_empty="")
|
|
|
|
testdir.runpytest("-q", "--lf")
|
|
|
|
assert not os.path.exists(".pytest_cache/v/cache/lastfailed")
|
2016-02-16 13:42:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(test_successful="def test_success():\n assert True")
|
|
|
|
testdir.runpytest("-q", "--lf")
|
|
|
|
assert not os.path.exists(".pytest_cache/v/cache/lastfailed")
|
2016-02-16 13:42:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(test_errored="def test_error():\n assert False")
|
|
|
|
testdir.runpytest("-q", "--lf")
|
|
|
|
assert os.path.exists(".pytest_cache/v/cache/lastfailed")
|
2017-07-27 08:06:08 +08:00
|
|
|
|
2017-07-27 21:56:04 +08:00
|
|
|
def test_xfail_not_considered_failure(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-07-27 21:56:04 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-27 21:56:04 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 xfailed*"])
|
2017-07-27 21:56:04 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == []
|
|
|
|
|
|
|
|
def test_xfail_strict_considered_failure(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-07-27 21:56:04 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail(strict=True)
|
|
|
|
def test():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-27 21:56:04 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*"])
|
2018-06-26 21:35:27 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == [
|
|
|
|
"test_xfail_strict_considered_failure.py::test"
|
|
|
|
]
|
2017-07-27 21:56:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("mark", ["mark.xfail", "mark.skip"])
|
2017-07-28 05:43:04 +08:00
|
|
|
def test_failed_changed_to_xfail_or_skip(self, testdir, mark):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-07-28 05:43:04 +08:00
|
|
|
import pytest
|
|
|
|
def test():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-28 05:43:04 +08:00
|
|
|
result = testdir.runpytest()
|
2018-06-26 21:35:27 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == [
|
|
|
|
"test_failed_changed_to_xfail_or_skip.py::test"
|
|
|
|
]
|
2017-07-28 05:43:04 +08:00
|
|
|
assert result.ret == 1
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-07-28 05:43:04 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.{mark}
|
|
|
|
def test():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
mark=mark
|
|
|
|
)
|
|
|
|
)
|
2017-07-28 05:43:04 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
assert self.get_cached_last_failed(testdir) == []
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-08-30 18:31:42 +08:00
|
|
|
@pytest.mark.parametrize("quiet", [True, False])
|
|
|
|
@pytest.mark.parametrize("opt", ["--ff", "--lf"])
|
|
|
|
def test_lf_and_ff_prints_no_needless_message(self, quiet, opt, testdir):
|
|
|
|
# Issue 3853
|
2018-08-31 19:01:55 +08:00
|
|
|
testdir.makepyfile("def test(): assert 0")
|
2018-08-30 18:31:42 +08:00
|
|
|
args = [opt]
|
|
|
|
if quiet:
|
|
|
|
args.append("-q")
|
|
|
|
result = testdir.runpytest(*args)
|
|
|
|
assert "run all" not in result.stdout.str()
|
|
|
|
|
2018-08-31 19:01:55 +08:00
|
|
|
result = testdir.runpytest(*args)
|
|
|
|
if quiet:
|
|
|
|
assert "run all" not in result.stdout.str()
|
|
|
|
else:
|
|
|
|
assert "rerun previous" in result.stdout.str()
|
|
|
|
|
2017-07-27 08:06:08 +08:00
|
|
|
def get_cached_last_failed(self, testdir):
|
|
|
|
config = testdir.parseconfigure()
|
|
|
|
return sorted(config.cache.get("cache/lastfailed", {}))
|
|
|
|
|
|
|
|
def test_cache_cumulative(self, testdir):
|
|
|
|
"""
|
|
|
|
Test workflow where user fixes errors gradually file by file using --lf.
|
|
|
|
"""
|
|
|
|
# 1. initial run
|
2018-05-23 22:48:46 +08:00
|
|
|
test_bar = testdir.makepyfile(
|
|
|
|
test_bar="""
|
2017-07-27 08:06:08 +08:00
|
|
|
def test_bar_1():
|
|
|
|
pass
|
|
|
|
def test_bar_2():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
test_foo = testdir.makepyfile(
|
|
|
|
test_foo="""
|
2017-07-27 08:06:08 +08:00
|
|
|
def test_foo_3():
|
|
|
|
pass
|
|
|
|
def test_foo_4():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-27 08:06:08 +08:00
|
|
|
testdir.runpytest()
|
2018-06-26 21:35:27 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == [
|
|
|
|
"test_bar.py::test_bar_2",
|
|
|
|
"test_foo.py::test_foo_4",
|
|
|
|
]
|
2017-07-27 08:06:08 +08:00
|
|
|
|
|
|
|
# 2. fix test_bar_2, run only test_bar.py
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_bar="""
|
2017-07-27 08:06:08 +08:00
|
|
|
def test_bar_1():
|
|
|
|
pass
|
|
|
|
def test_bar_2():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-27 08:06:08 +08:00
|
|
|
result = testdir.runpytest(test_bar)
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2017-07-27 08:06:08 +08:00
|
|
|
# ensure cache does not forget that test_foo_4 failed once before
|
2018-05-23 22:48:46 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"]
|
2017-07-27 08:06:08 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("--last-failed")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed, 3 deselected*"])
|
2018-05-23 22:48:46 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"]
|
2017-07-27 08:06:08 +08:00
|
|
|
|
|
|
|
# 3. fix test_foo_4, run only test_foo.py
|
2018-05-23 22:48:46 +08:00
|
|
|
test_foo = testdir.makepyfile(
|
|
|
|
test_foo="""
|
2017-07-27 08:06:08 +08:00
|
|
|
def test_foo_3():
|
|
|
|
pass
|
|
|
|
def test_foo_4():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest(test_foo, "--last-failed")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed, 1 deselected*"])
|
2017-07-27 08:06:08 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == []
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("--last-failed")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*4 passed*"])
|
2017-07-27 08:06:08 +08:00
|
|
|
assert self.get_cached_last_failed(testdir) == []
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2018-03-11 04:45:45 +08:00
|
|
|
def test_lastfailed_no_failures_behavior_all_passed(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2018-03-11 04:45:45 +08:00
|
|
|
def test_1():
|
|
|
|
assert True
|
|
|
|
def test_2():
|
|
|
|
assert True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-03-11 04:45:45 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
|
|
|
result = testdir.runpytest("--lf")
|
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
|
|
|
result = testdir.runpytest("--lf", "--lfnf", "all")
|
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
|
|
|
result = testdir.runpytest("--lf", "--lfnf", "none")
|
2019-04-03 21:18:29 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 2 items / 2 deselected",
|
|
|
|
"run-last-failure: no previously failed tests, deselecting all items.",
|
|
|
|
"* 2 deselected in *",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2018-03-11 04:45:45 +08:00
|
|
|
|
|
|
|
def test_lastfailed_no_failures_behavior_empty_cache(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2018-03-11 04:45:45 +08:00
|
|
|
def test_1():
|
|
|
|
assert True
|
|
|
|
def test_2():
|
|
|
|
assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-03-11 04:45:45 +08:00
|
|
|
result = testdir.runpytest("--lf", "--cache-clear")
|
|
|
|
result.stdout.fnmatch_lines(["*1 failed*1 passed*"])
|
|
|
|
result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "all")
|
|
|
|
result.stdout.fnmatch_lines(["*1 failed*1 passed*"])
|
|
|
|
result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "none")
|
|
|
|
result.stdout.fnmatch_lines(["*2 desel*"])
|
|
|
|
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
class TestNewFirst(object):
|
|
|
|
def test_newfirst_usecase(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"test_1/test_1.py": """
|
2018-02-24 03:49:17 +08:00
|
|
|
def test_1(): assert 1
|
|
|
|
def test_2(): assert 1
|
|
|
|
def test_3(): assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
"test_2/test_2.py": """
|
2018-02-24 03:49:17 +08:00
|
|
|
def test_1(): assert 1
|
|
|
|
def test_2(): assert 1
|
|
|
|
def test_3(): assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("test_1/test_1.py").setmtime(1)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_1/test_1.py::test_1 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_2 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_3 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_1 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_2 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_3 PASSED*",
|
|
|
|
]
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("-v", "--nf")
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_2/test_2.py::test_1 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_2 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_3 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_1 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_2 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_3 PASSED*",
|
|
|
|
]
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2018-02-24 03:49:17 +08:00
|
|
|
testdir.tmpdir.join("test_1/test_1.py").write(
|
2018-02-14 03:49:28 +08:00
|
|
|
"def test_1(): assert 1\n"
|
|
|
|
"def test_2(): assert 1\n"
|
|
|
|
"def test_3(): assert 1\n"
|
|
|
|
"def test_4(): assert 1\n"
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("test_1/test_1.py").setmtime(1)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("-v", "--nf")
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_1/test_1.py::test_4 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_1 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_2 PASSED*",
|
|
|
|
"*test_2/test_2.py::test_3 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_1 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_2 PASSED*",
|
|
|
|
"*test_1/test_1.py::test_3 PASSED*",
|
|
|
|
]
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
def test_newfirst_parametrize(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"test_1/test_1.py": """
|
2018-02-24 03:49:17 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('num', [1, 2])
|
|
|
|
def test_1(num): assert num
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
"test_2/test_2.py": """
|
2018-02-24 03:49:17 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('num', [1, 2])
|
|
|
|
def test_1(num): assert num
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("test_1/test_1.py").setmtime(1)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_1/test_1.py::test_1[1*",
|
|
|
|
"*test_1/test_1.py::test_1[2*",
|
|
|
|
"*test_2/test_2.py::test_1[1*",
|
|
|
|
"*test_2/test_2.py::test_1[2*",
|
|
|
|
]
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("-v", "--nf")
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_2/test_2.py::test_1[1*",
|
|
|
|
"*test_2/test_2.py::test_1[2*",
|
|
|
|
"*test_1/test_1.py::test_1[1*",
|
|
|
|
"*test_1/test_1.py::test_1[2*",
|
|
|
|
]
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2018-02-24 03:49:17 +08:00
|
|
|
testdir.tmpdir.join("test_1/test_1.py").write(
|
2018-02-14 03:49:28 +08:00
|
|
|
"import pytest\n"
|
|
|
|
"@pytest.mark.parametrize('num', [1, 2, 3])\n"
|
|
|
|
"def test_1(num): assert num\n"
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("test_1/test_1.py").setmtime(1)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("-v", "--nf")
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_1/test_1.py::test_1[3*",
|
|
|
|
"*test_2/test_2.py::test_1[1*",
|
|
|
|
"*test_2/test_2.py::test_1[2*",
|
|
|
|
"*test_1/test_1.py::test_1[1*",
|
|
|
|
"*test_1/test_1.py::test_1[2*",
|
|
|
|
]
|
|
|
|
)
|
2018-06-21 20:13:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestReadme(object):
|
|
|
|
def check_readme(self, testdir):
|
|
|
|
config = testdir.parseconfigure()
|
2018-06-23 06:03:10 +08:00
|
|
|
readme = config.cache._cachedir.joinpath("README.md")
|
|
|
|
return readme.is_file()
|
2018-06-21 20:13:10 +08:00
|
|
|
|
|
|
|
def test_readme_passed(self, testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
def test_always_passes():
|
|
|
|
assert 1
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.runpytest()
|
|
|
|
assert self.check_readme(testdir) is True
|
|
|
|
|
|
|
|
def test_readme_failed(self, testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2019-03-04 23:42:51 +08:00
|
|
|
def test_always_fails():
|
2018-06-21 20:13:10 +08:00
|
|
|
assert 0
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.runpytest()
|
|
|
|
assert self.check_readme(testdir) is True
|
2018-09-15 02:15:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_gitignore(testdir):
|
|
|
|
"""Ensure we automatically create .gitignore file in the pytest_cache directory (#3286)."""
|
|
|
|
from _pytest.cacheprovider import Cache
|
|
|
|
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
cache = Cache.for_config(config)
|
|
|
|
cache.set("foo", "bar")
|
2018-11-15 04:55:37 +08:00
|
|
|
msg = "# Created by pytest automatically.\n*"
|
|
|
|
gitignore_path = cache._cachedir.joinpath(".gitignore")
|
|
|
|
assert gitignore_path.read_text(encoding="UTF-8") == msg
|
|
|
|
|
|
|
|
# Does not overwrite existing/custom one.
|
2018-11-23 07:38:33 +08:00
|
|
|
gitignore_path.write_text(u"custom")
|
2018-11-15 04:55:37 +08:00
|
|
|
cache.set("something", "else")
|
2018-11-23 07:38:33 +08:00
|
|
|
assert gitignore_path.read_text(encoding="UTF-8") == "custom"
|
2018-11-15 04:55:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_does_not_create_boilerplate_in_existing_dirs(testdir):
|
|
|
|
from _pytest.cacheprovider import Cache
|
|
|
|
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
cache_dir = .
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
cache = Cache.for_config(config)
|
|
|
|
cache.set("foo", "bar")
|
|
|
|
|
2018-11-23 07:38:33 +08:00
|
|
|
assert os.path.isdir("v") # cache contents
|
2018-11-15 04:55:37 +08:00
|
|
|
assert not os.path.exists(".gitignore")
|
|
|
|
assert not os.path.exists("README.md")
|
2018-12-04 20:49:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_cachedir_tag(testdir):
|
|
|
|
"""Ensure we automatically create CACHEDIR.TAG file in the pytest_cache directory (#4278)."""
|
|
|
|
from _pytest.cacheprovider import Cache
|
|
|
|
from _pytest.cacheprovider import CACHEDIR_TAG_CONTENT
|
|
|
|
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
cache = Cache.for_config(config)
|
|
|
|
cache.set("foo", "bar")
|
|
|
|
cachedir_tag_path = cache._cachedir.joinpath("CACHEDIR.TAG")
|
|
|
|
assert cachedir_tag_path.read_bytes() == CACHEDIR_TAG_CONTENT
|