2024-04-08 22:02:31 +08:00
|
|
|
from enum import auto
|
|
|
|
from enum import Enum
|
2018-10-25 15:01:29 +08:00
|
|
|
import os
|
2020-12-15 19:02:32 +08:00
|
|
|
from pathlib import Path
|
2018-10-25 15:01:29 +08:00
|
|
|
import shutil
|
2024-04-08 21:59:51 +08:00
|
|
|
from typing import Any
|
2021-06-24 02:28:09 +08:00
|
|
|
from typing import Generator
|
2020-12-15 19:02:32 +08:00
|
|
|
from typing import List
|
2024-04-08 21:59:51 +08:00
|
|
|
from typing import Sequence
|
|
|
|
from typing import Tuple
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2024-04-08 22:02:31 +08:00
|
|
|
from _pytest.compat import assert_never
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2020-10-19 23:34:03 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2020-12-19 21:16:01 +08:00
|
|
|
from _pytest.tmpdir import TempPathFactory
|
2015-07-11 16:13:27 +08:00
|
|
|
import pytest
|
2024-02-01 04:12:33 +08:00
|
|
|
|
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:
|
2021-03-15 03:58:37 +08:00
|
|
|
def test_config_cache_mkdir(self, pytester: Pytester) -> None:
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini("[pytest]")
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.cache is not None
|
2015-07-11 16:13:27 +08:00
|
|
|
with pytest.raises(ValueError):
|
2021-03-15 03:58:37 +08:00
|
|
|
config.cache.mkdir("key/name")
|
2015-07-11 16:13:27 +08:00
|
|
|
|
2021-03-15 03:58:37 +08:00
|
|
|
p = config.cache.mkdir("name")
|
|
|
|
assert p.is_dir()
|
2015-07-11 16:13:27 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_config_cache_dataerror(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini("[pytest]")
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.cache is not None
|
2015-07-11 16:13:27 +08:00
|
|
|
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
|
|
|
|
|
2020-04-29 03:46:44 +08:00
|
|
|
@pytest.mark.filterwarnings("ignore:could not create cache path")
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cache_writefail_cachfile_silent(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini("[pytest]")
|
2023-06-20 19:55:40 +08:00
|
|
|
pytester.path.joinpath(".pytest_cache").write_text(
|
|
|
|
"gone wrong", encoding="utf-8"
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
config = pytester.parseconfigure()
|
2015-09-22 22:28:19 +08:00
|
|
|
cache = config.cache
|
2020-12-15 19:02:32 +08:00
|
|
|
assert cache is not None
|
2015-09-22 22:28:19 +08:00
|
|
|
cache.set("test/broken", [])
|
|
|
|
|
2021-06-24 02:28:09 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def unwritable_cache_dir(self, pytester: Pytester) -> Generator[Path, None, None]:
|
2020-12-15 19:02:32 +08:00
|
|
|
cache_dir = pytester.path.joinpath(".pytest_cache")
|
|
|
|
cache_dir.mkdir()
|
|
|
|
mode = cache_dir.stat().st_mode
|
|
|
|
cache_dir.chmod(0)
|
2021-06-24 02:28:09 +08:00
|
|
|
if os.access(cache_dir, os.W_OK):
|
|
|
|
pytest.skip("Failed to make cache dir unwritable")
|
|
|
|
|
|
|
|
yield cache_dir
|
|
|
|
cache_dir.chmod(mode)
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings(
|
|
|
|
"ignore:could not create cache path:pytest.PytestWarning"
|
|
|
|
)
|
|
|
|
def test_cache_writefail_permissions(
|
|
|
|
self, unwritable_cache_dir: Path, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makeini("[pytest]")
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
cache = config.cache
|
|
|
|
assert cache is not None
|
|
|
|
cache.set("test/broken", [])
|
2015-09-22 22:28:19 +08:00
|
|
|
|
2020-02-11 20:10:51 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cache_failure_warns(
|
2021-06-24 02:28:09 +08:00
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
unwritable_cache_dir: Path,
|
2020-12-15 19:02:32 +08:00
|
|
|
) -> None:
|
2019-11-16 06:43:54 +08:00
|
|
|
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
|
2021-06-24 02:28:09 +08:00
|
|
|
|
|
|
|
pytester.makepyfile("def test_error(): raise Exception")
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret == 1
|
|
|
|
# warnings from nodeids, lastfailed, and stepwise
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
# Validate location/stacklevel of warning from cacheprovider.
|
|
|
|
"*= warnings summary =*",
|
|
|
|
"*/cacheprovider.py:*",
|
|
|
|
" */cacheprovider.py:*: PytestCacheWarning: could not create cache path "
|
2023-05-18 21:11:47 +08:00
|
|
|
f"{unwritable_cache_dir}/v/cache/nodeids: *",
|
2021-06-24 02:28:09 +08:00
|
|
|
' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))',
|
|
|
|
"*1 failed, 3 warnings in*",
|
|
|
|
]
|
|
|
|
)
|
2015-09-22 22:28:19 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_config_cache(self, pytester: Pytester) -> None:
|
|
|
|
pytester.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")
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2015-07-11 16:13:27 +08:00
|
|
|
"""
|
|
|
|
def test_session(pytestconfig):
|
|
|
|
assert hasattr(pytestconfig, "cache")
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2015-07-11 16:13:27 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cachefuncarg(self, pytester: Pytester) -> None:
|
|
|
|
pytester.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]
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2015-07-11 16:13:27 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_custom_rel_cache_dir(self, pytester: Pytester) -> None:
|
2017-07-07 18:07:33 +08:00
|
|
|
rel_cache_dir = os.path.join("custom_cache_dir", "subdir")
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini(
|
2017-07-07 18:07:33 +08:00
|
|
|
f"""
|
|
|
|
[pytest]
|
|
|
|
cache_dir = {rel_cache_dir}
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(test_errored="def test_error():\n assert False")
|
|
|
|
pytester.runpytest()
|
|
|
|
assert pytester.path.joinpath(rel_cache_dir).is_dir()
|
2017-07-07 18:07:33 +08:00
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_custom_abs_cache_dir(
|
|
|
|
self, pytester: Pytester, tmp_path_factory: TempPathFactory
|
|
|
|
) -> None:
|
|
|
|
tmp = tmp_path_factory.mktemp("tmp")
|
|
|
|
abs_cache_dir = tmp / "custom_cache_dir"
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini(
|
2017-07-07 18:07:33 +08:00
|
|
|
f"""
|
|
|
|
[pytest]
|
|
|
|
cache_dir = {abs_cache_dir}
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(test_errored="def test_error():\n assert False")
|
|
|
|
pytester.runpytest()
|
2020-12-19 21:16:01 +08:00
|
|
|
assert abs_cache_dir.is_dir()
|
2017-07-07 18:07:33 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_custom_cache_dir_with_env_var(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2017-07-07 18:07:33 +08:00
|
|
|
monkeypatch.setenv("env_var", "custom_cache_dir")
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini(
|
2017-07-07 18:07:33 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
cache_dir = {cache_dir}
|
|
|
|
""".format(cache_dir="$env_var")
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(test_errored="def test_error():\n assert False")
|
|
|
|
pytester.runpytest()
|
|
|
|
assert pytester.path.joinpath("custom_cache_dir").is_dir()
|
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")))
|
2024-04-08 21:59:51 +08:00
|
|
|
def test_cache_reportheader(
|
|
|
|
env: Sequence[str], pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile("""def test_foo(): pass""")
|
2018-10-31 05:35:09 +08:00
|
|
|
if env:
|
|
|
|
monkeypatch.setenv(*env)
|
|
|
|
expected = os.path.join(env[1], ".pytest_cache")
|
|
|
|
else:
|
|
|
|
monkeypatch.delenv("TOX_ENV_DIR", raising=False)
|
|
|
|
expected = ".pytest_cache"
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cache_reportheader_external_abspath(
|
2020-12-19 21:16:01 +08:00
|
|
|
pytester: Pytester, tmp_path_factory: TempPathFactory
|
2020-12-15 19:02:32 +08:00
|
|
|
) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
external_cache = tmp_path_factory.mktemp(
|
2018-08-01 14:25:37 +08:00
|
|
|
"test_cache_reportheader_external_abspath_abs"
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile("def test_hello(): pass")
|
|
|
|
pytester.makeini(
|
2018-08-01 14:25:37 +08:00
|
|
|
f"""
|
|
|
|
[pytest]
|
|
|
|
cache_dir = {external_cache}
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2020-10-03 04:16:22 +08:00
|
|
|
result.stdout.fnmatch_lines([f"cachedir: {external_cache}"])
|
2018-08-01 14:25:37 +08:00
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cache_show(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--cache-show")
|
2015-07-11 16:13:27 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*cache is empty*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeconftest(
|
2015-07-24 16:05:54 +08:00
|
|
|
"""
|
2015-07-11 16:13:27 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
config.cache.set("my/name", [1,2,3])
|
2019-04-03 21:56:42 +08:00
|
|
|
config.cache.set("my/hello", "world")
|
2015-07-11 16:13:27 +08:00
|
|
|
config.cache.set("other/some", {1:2})
|
2021-03-15 03:58:37 +08:00
|
|
|
dp = config.cache.mkdir("mydb")
|
|
|
|
dp.joinpath("hello").touch()
|
|
|
|
dp.joinpath("world").touch()
|
2015-07-11 16:13:27 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2015-08-28 01:22:22 +08:00
|
|
|
assert result.ret == 5 # no tests executed
|
2019-04-03 21:56:42 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--cache-show")
|
2019-04-03 21:56:42 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2015-07-11 16:13:27 +08:00
|
|
|
[
|
|
|
|
"*cachedir:*",
|
2019-04-03 21:56:42 +08:00
|
|
|
"*- cache values for '[*]' -*",
|
|
|
|
"cache/nodeids contains:",
|
|
|
|
"my/name contains:",
|
2015-07-11 16:13:27 +08:00
|
|
|
" [1, 2, 3]",
|
2019-04-03 21:56:42 +08:00
|
|
|
"other/some contains:",
|
|
|
|
" {*'1': 2}",
|
|
|
|
"*- cache directories for '[*]' -*",
|
2015-07-11 16:13:27 +08:00
|
|
|
"*mydb/hello*length 0*",
|
|
|
|
"*mydb/world*length 0*",
|
|
|
|
]
|
|
|
|
)
|
2019-04-03 21:56:42 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--cache-show", "*/hello")
|
2019-04-03 21:56:42 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*cachedir:*",
|
|
|
|
"*- cache values for '[*]/hello' -*",
|
|
|
|
"my/hello contains:",
|
|
|
|
" *'world'",
|
|
|
|
"*- cache directories for '[*]/hello' -*",
|
|
|
|
"d/mydb/hello*length 0*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
stdout = result.stdout.str()
|
|
|
|
assert "other/some" not in stdout
|
|
|
|
assert "d/mydb/world" not in stdout
|
|
|
|
assert result.ret == 0
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestLastFailed:
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_usecase(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2019-11-16 06:12:25 +08:00
|
|
|
monkeypatch.setattr("sys.dont_write_bytecode", True)
|
2020-12-15 19:02:32 +08:00
|
|
|
p = pytester.makepyfile(
|
2015-09-17 02:41:22 +08:00
|
|
|
"""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_1(): assert 0
|
|
|
|
def test_2(): assert 0
|
|
|
|
def test_3(): assert 1
|
|
|
|
"""
|
2015-09-17 02:41:22 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(str(p))
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
p = pytester.makepyfile(
|
2019-11-16 06:41:12 +08:00
|
|
|
"""
|
|
|
|
def test_1(): assert 1
|
|
|
|
def test_2(): assert 1
|
|
|
|
def test_3(): assert 0
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(str(p), "--lf")
|
2020-02-20 04:33:03 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-03-15 20:26:04 +08:00
|
|
|
"collected 3 items / 1 deselected / 2 selected",
|
2020-02-20 04:33:03 +08:00
|
|
|
"run-last-failure: rerun previous 2 failures",
|
2020-03-15 20:26:04 +08:00
|
|
|
"*= 2 passed, 1 deselected in *",
|
2020-02-20 04:33:03 +08:00
|
|
|
]
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(str(p), "--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*",
|
|
|
|
]
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.path.joinpath(".pytest_cache", ".git").mkdir(parents=True)
|
|
|
|
result = pytester.runpytest(str(p), "--lf", "--cache-clear")
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
assert pytester.path.joinpath(".pytest_cache", "README.md").is_file()
|
|
|
|
assert pytester.path.joinpath(".pytest_cache", ".git").is_dir()
|
2015-09-17 02:41:22 +08:00
|
|
|
|
|
|
|
# Run this again to make sure clear-cache is robust
|
2018-01-25 21:49:58 +08:00
|
|
|
if os.path.isdir(".pytest_cache"):
|
|
|
|
shutil.rmtree(".pytest_cache")
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--cache-clear")
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_failedfirst_order(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-11-16 06:41:12 +08:00
|
|
|
test_a="def test_always_passes(): pass",
|
|
|
|
test_b="def test_always_fails(): assert 0",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2015-09-17 02:41:22 +08:00
|
|
|
# Test order will be collection order; alphabetical
|
|
|
|
result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--ff")
|
2020-02-20 04:33:03 +08:00
|
|
|
# Test order will be failing tests first
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 2 items",
|
|
|
|
"run-last-failure: rerun previous 1 failure first",
|
|
|
|
"test_b.py*",
|
|
|
|
"test_a.py*",
|
|
|
|
]
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_failedfirst_order(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-11-16 06:41:12 +08:00
|
|
|
test_a="def test_always_passes(): assert 1",
|
|
|
|
test_b="def test_always_fails(): assert 0",
|
2017-04-12 04:55:55 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2017-04-10 16:42:19 +08:00
|
|
|
# Test order will be collection order; alphabetical
|
|
|
|
result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--ff")
|
2020-02-20 04:33:03 +08:00
|
|
|
# Test order will be failing tests first
|
2017-04-10 16:42:19 +08:00
|
|
|
result.stdout.fnmatch_lines(["test_b.py*"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*test_a.py*")
|
2017-04-10 16:42:19 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_difference_invocations(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2019-11-16 06:12:25 +08:00
|
|
|
monkeypatch.setattr("sys.dont_write_bytecode", True)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-11-16 06:41:12 +08:00
|
|
|
test_a="""
|
|
|
|
def test_a1(): assert 0
|
|
|
|
def test_a2(): assert 1
|
2018-08-24 00:06:17 +08:00
|
|
|
""",
|
2019-11-16 06:41:12 +08:00
|
|
|
test_b="def test_b1(): assert 0",
|
2015-09-17 02:41:22 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
p = pytester.path.joinpath("test_a.py")
|
|
|
|
p2 = pytester.path.joinpath("test_b.py")
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", p2)
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*"])
|
2019-11-16 06:41:12 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(test_b="def test_b1(): assert 1")
|
|
|
|
result = pytester.runpytest("--lf", p2)
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", p)
|
2020-03-15 20:26:04 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 2 items / 1 deselected / 1 selected",
|
|
|
|
"run-last-failure: rerun previous 1 failure",
|
|
|
|
"*= 1 failed, 1 deselected in *",
|
|
|
|
]
|
|
|
|
)
|
2015-09-17 02:41:22 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_usecase_splice(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2019-11-16 06:12:25 +08:00
|
|
|
monkeypatch.setattr("sys.dont_write_bytecode", True)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-11-16 06:41:12 +08:00
|
|
|
"def test_1(): assert 0", test_something="def test_2(): assert 0"
|
2015-09-17 02:41:22 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
p2 = pytester.path.joinpath("test_something.py")
|
|
|
|
result = pytester.runpytest()
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", p2)
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf")
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_xpass(self, pytester: Pytester) -> None:
|
|
|
|
pytester.inline_runsource(
|
2015-09-17 02:41:22 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_hello():
|
|
|
|
assert 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.cache is not None
|
2015-09-17 02:41:22 +08:00
|
|
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_non_serializable_parametrize(self, pytester: Pytester) -> None:
|
2015-09-21 13:39:34 +08:00
|
|
|
"""Test that failed parametrized tests with unmarshable parameters
|
|
|
|
don't break pytest-cache.
|
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2015-09-21 13:39:34 +08:00
|
|
|
r"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('val', [
|
|
|
|
b'\xac\x10\x02G',
|
|
|
|
])
|
|
|
|
def test_fail(val):
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed in*"])
|
2015-09-21 13:39:34 +08:00
|
|
|
|
2023-06-02 21:03:39 +08:00
|
|
|
@pytest.mark.parametrize("parent", ("directory", "package"))
|
2023-05-30 03:38:36 +08:00
|
|
|
def test_terminal_report_lastfailed(self, pytester: Pytester, parent: str) -> None:
|
|
|
|
if parent == "package":
|
|
|
|
pytester.makepyfile(
|
|
|
|
__init__="",
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
test_a = pytester.makepyfile(
|
2017-07-28 02:06:35 +08:00
|
|
|
test_a="""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_a1(): pass
|
|
|
|
def test_a2(): pass
|
2017-07-28 02:06:35 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
test_b = pytester.makepyfile(
|
2017-07-28 02:06:35 +08:00
|
|
|
test_b="""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_b1(): assert 0
|
|
|
|
def test_b2(): assert 0
|
2017-07-28 02:06:35 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2017-07-28 02:06:35 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 4 items", "*2 failed, 2 passed in*"])
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf")
|
2017-07-28 02:06:35 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2019-04-29 03:23:38 +08:00
|
|
|
"collected 2 items",
|
|
|
|
"run-last-failure: rerun previous 2 failures (skipped 1 file)",
|
|
|
|
"*2 failed in*",
|
2017-07-28 02:06:35 +08:00
|
|
|
]
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(test_b, "--lf")
|
2017-07-28 02:06:35 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 2 items",
|
|
|
|
"run-last-failure: rerun previous 2 failures",
|
|
|
|
"*2 failed in*",
|
|
|
|
]
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("test_b.py::test_b1", "--lf")
|
2017-07-28 02:06:35 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: rerun previous 1 failure",
|
|
|
|
"*1 failed in*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_terminal_report_failedfirst(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2017-07-28 02:06:35 +08:00
|
|
|
test_a="""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_a1(): assert 0
|
|
|
|
def test_a2(): pass
|
2017-07-28 02:06:35 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2017-07-28 02:06:35 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 2 items", "*1 failed, 1 passed in*"])
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--ff")
|
2017-07-28 02:06:35 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 2 items",
|
|
|
|
"run-last-failure: rerun previous 1 failure first",
|
|
|
|
"*1 failed, 1 passed in*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_collectfailure(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2015-09-17 02:41:22 +08:00
|
|
|
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']
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2024-04-08 21:59:51 +08:00
|
|
|
def rlf(fail_import: int, fail_run: int) -> Any:
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.runpytest("-q")
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.cache is not None
|
2015-09-17 02:41:22 +08:00
|
|
|
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)
|
|
|
|
assert list(lastfailed) == ["test_maybe.py"]
|
|
|
|
|
|
|
|
lastfailed = rlf(fail_import=0, fail_run=1)
|
|
|
|
assert list(lastfailed) == ["test_maybe.py::test_hello"]
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_failure_subset(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2015-09-17 02:41:22 +08:00
|
|
|
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']
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2015-09-17 02:41:22 +08:00
|
|
|
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')
|
2019-11-16 06:41:12 +08:00
|
|
|
|
2015-09-17 02:41:22 +08:00
|
|
|
def test_hello():
|
|
|
|
assert '0' == env['FAILTEST']
|
|
|
|
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2024-04-08 21:59:51 +08:00
|
|
|
def rlf(
|
|
|
|
fail_import: int, fail_run: int, args: Sequence[str] = ()
|
|
|
|
) -> Tuple[Any, Any]:
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-q", "--lf", *args)
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.cache is not None
|
2015-09-17 02:41:22 +08:00
|
|
|
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
|
2015-09-17 02:41:22 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
|
|
|
result, lastfailed = rlf(fail_import=1, fail_run=0)
|
|
|
|
assert sorted(list(lastfailed)) == ["test_maybe.py", "test_maybe2.py"]
|
|
|
|
|
|
|
|
result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",))
|
|
|
|
assert list(lastfailed) == ["test_maybe.py"]
|
|
|
|
|
|
|
|
# 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
|
|
|
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_creates_cache_when_needed(self, pytester: Pytester) -> None:
|
2016-02-16 13:42:04 +08:00
|
|
|
# Issue #1342
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(test_empty="")
|
|
|
|
pytester.runpytest("-q", "--lf")
|
2018-02-14 03:49:28 +08:00
|
|
|
assert not os.path.exists(".pytest_cache/v/cache/lastfailed")
|
2016-02-16 13:42:04 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(test_successful="def test_success():\n assert True")
|
|
|
|
pytester.runpytest("-q", "--lf")
|
2018-02-14 03:49:28 +08:00
|
|
|
assert not os.path.exists(".pytest_cache/v/cache/lastfailed")
|
2016-02-16 13:42:04 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(test_errored="def test_error():\n assert False")
|
|
|
|
pytester.runpytest("-q", "--lf")
|
2018-02-14 03:49:28 +08:00
|
|
|
assert os.path.exists(".pytest_cache/v/cache/lastfailed")
|
2017-07-27 08:06:08 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_xfail_not_considered_failure(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-27 21:56:04 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail
|
2019-11-16 06:41:12 +08:00
|
|
|
def test(): assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-27 21:56:04 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 xfailed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
assert self.get_cached_last_failed(pytester) == []
|
2017-07-27 21:56:04 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_xfail_strict_considered_failure(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-27 21:56:04 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail(strict=True)
|
2019-11-16 06:41:12 +08:00
|
|
|
def test(): pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-27 21:56:04 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
assert self.get_cached_last_failed(pytester) == [
|
2018-06-26 21:35:27 +08:00
|
|
|
"test_xfail_strict_considered_failure.py::test"
|
|
|
|
]
|
2017-07-27 21:56:04 +08:00
|
|
|
|
2017-07-28 05:43:04 +08:00
|
|
|
@pytest.mark.parametrize("mark", ["mark.xfail", "mark.skip"])
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_failed_changed_to_xfail_or_skip(
|
|
|
|
self, pytester: Pytester, mark: str
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-28 05:43:04 +08:00
|
|
|
import pytest
|
2019-11-16 06:41:12 +08:00
|
|
|
def test(): assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-28 05:43:04 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
|
|
|
assert self.get_cached_last_failed(pytester) == [
|
2018-06-26 21:35:27 +08:00
|
|
|
"test_failed_changed_to_xfail_or_skip.py::test"
|
|
|
|
]
|
2017-07-28 05:43:04 +08:00
|
|
|
assert result.ret == 1
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
f"""
|
2017-07-28 05:43:04 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.{mark}
|
2019-11-16 06:41:12 +08:00
|
|
|
def test(): assert 0
|
2017-07-28 05:43:04 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2017-07-28 05:43:04 +08:00
|
|
|
assert result.ret == 0
|
2020-12-15 19:02:32 +08:00
|
|
|
assert self.get_cached_last_failed(pytester) == []
|
2017-07-28 05:43:04 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-08-30 18:31:42 +08:00
|
|
|
@pytest.mark.parametrize("quiet", [True, False])
|
|
|
|
@pytest.mark.parametrize("opt", ["--ff", "--lf"])
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lf_and_ff_prints_no_needless_message(
|
|
|
|
self, quiet: bool, opt: str, pytester: Pytester
|
|
|
|
) -> None:
|
2018-08-30 18:31:42 +08:00
|
|
|
# Issue 3853
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile("def test(): assert 0")
|
2018-08-30 18:31:42 +08:00
|
|
|
args = [opt]
|
|
|
|
if quiet:
|
|
|
|
args.append("-q")
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(*args)
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*run all*")
|
2018-08-30 18:31:42 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(*args)
|
2018-08-31 19:01:55 +08:00
|
|
|
if quiet:
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*run all*")
|
2018-08-31 19:01:55 +08:00
|
|
|
else:
|
|
|
|
assert "rerun previous" in result.stdout.str()
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def get_cached_last_failed(self, pytester: Pytester) -> List[str]:
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.cache is not None
|
2017-07-27 08:06:08 +08:00
|
|
|
return sorted(config.cache.get("cache/lastfailed", {}))
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cache_cumulative(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test workflow where user fixes errors gradually file by file using --lf."""
|
2017-07-27 08:06:08 +08:00
|
|
|
# 1. initial run
|
2020-12-15 19:02:32 +08:00
|
|
|
test_bar = pytester.makepyfile(
|
2017-07-27 08:06:08 +08:00
|
|
|
test_bar="""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_bar_1(): pass
|
|
|
|
def test_bar_2(): assert 0
|
2017-07-27 08:06:08 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
test_foo = pytester.makepyfile(
|
2017-07-27 08:06:08 +08:00
|
|
|
test_foo="""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_foo_3(): pass
|
|
|
|
def test_foo_4(): assert 0
|
2017-07-27 08:06:08 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.runpytest()
|
|
|
|
assert self.get_cached_last_failed(pytester) == [
|
2018-06-26 21:35:27 +08:00
|
|
|
"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
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2017-07-27 08:06:08 +08:00
|
|
|
test_bar="""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_bar_1(): pass
|
|
|
|
def test_bar_2(): pass
|
2017-07-27 08:06:08 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.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
|
2020-12-15 19:02:32 +08:00
|
|
|
assert self.get_cached_last_failed(pytester) == ["test_foo.py::test_foo_4"]
|
2017-07-27 08:06:08 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--last-failed")
|
2020-02-20 04:33:03 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: rerun previous 1 failure (skipped 1 file)",
|
|
|
|
"*= 1 failed in *",
|
|
|
|
]
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
assert self.get_cached_last_failed(pytester) == ["test_foo.py::test_foo_4"]
|
2017-07-27 08:06:08 +08:00
|
|
|
|
|
|
|
# 3. fix test_foo_4, run only test_foo.py
|
2020-12-15 19:02:32 +08:00
|
|
|
test_foo = pytester.makepyfile(
|
2017-07-27 08:06:08 +08:00
|
|
|
test_foo="""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_foo_3(): pass
|
|
|
|
def test_foo_4(): pass
|
2017-07-27 08:06:08 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(test_foo, "--last-failed")
|
2020-02-20 04:33:03 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-03-15 20:26:04 +08:00
|
|
|
"collected 2 items / 1 deselected / 1 selected",
|
2020-02-20 04:33:03 +08:00
|
|
|
"run-last-failure: rerun previous 1 failure",
|
2020-03-15 20:26:04 +08:00
|
|
|
"*= 1 passed, 1 deselected in *",
|
2020-02-20 04:33:03 +08:00
|
|
|
]
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
assert self.get_cached_last_failed(pytester) == []
|
2017-07-27 08:06:08 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--last-failed")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*4 passed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
assert self.get_cached_last_failed(pytester) == []
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_no_failures_behavior_all_passed(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-03-11 04:45:45 +08:00
|
|
|
"""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_1(): pass
|
|
|
|
def test_2(): pass
|
2018-03-11 04:45:45 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2018-03-11 04:45:45 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf")
|
2018-03-11 04:45:45 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--lfnf", "all")
|
2018-03-11 04:45:45 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2020-01-07 02:37:01 +08:00
|
|
|
|
|
|
|
# Ensure the list passed to pytest_deselected is a copy,
|
|
|
|
# and not a reference which is cleared right after.
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeconftest(
|
2020-01-07 02:37:01 +08:00
|
|
|
"""
|
|
|
|
deselected = []
|
|
|
|
|
|
|
|
def pytest_deselected(items):
|
|
|
|
global deselected
|
|
|
|
deselected = items
|
|
|
|
|
|
|
|
def pytest_sessionfinish():
|
|
|
|
print("\\ndeselected={}".format(len(deselected)))
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--lfnf", "none")
|
2019-04-03 21:18:29 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2022-02-15 19:17:34 +08:00
|
|
|
"collected 2 items / 2 deselected / 0 selected",
|
2019-04-03 21:18:29 +08:00
|
|
|
"run-last-failure: no previously failed tests, deselecting all items.",
|
2020-01-07 02:37:01 +08:00
|
|
|
"deselected=2",
|
2019-04-03 21:18:29 +08:00
|
|
|
"* 2 deselected in *",
|
|
|
|
]
|
|
|
|
)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2018-03-11 04:45:45 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_no_failures_behavior_empty_cache(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-03-11 04:45:45 +08:00
|
|
|
"""
|
2019-11-16 06:41:12 +08:00
|
|
|
def test_1(): pass
|
|
|
|
def test_2(): assert 0
|
2018-03-11 04:45:45 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--cache-clear")
|
2018-03-11 04:45:45 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*1 passed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--cache-clear", "--lfnf", "all")
|
2018-03-11 04:45:45 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*1 passed*"])
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--cache-clear", "--lfnf", "none")
|
2018-03-11 04:45:45 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 desel*"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_skip_collection(self, pytester: Pytester) -> None:
|
2019-04-29 03:23:38 +08:00
|
|
|
"""
|
|
|
|
Test --lf behavior regarding skipping collection of files that are not marked as
|
|
|
|
failed in the cache (#5172).
|
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-04-29 03:23:38 +08:00
|
|
|
**{
|
|
|
|
"pkg1/test_1.py": """
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('i', range(3))
|
|
|
|
def test_1(i): pass
|
|
|
|
""",
|
|
|
|
"pkg2/test_2.py": """
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('i', range(5))
|
|
|
|
def test_1(i):
|
|
|
|
assert i not in (1, 3)
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
# first run: collects 8 items (test_1: 3, test_2: 5)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-04-29 03:23:38 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 8 items", "*2 failed*6 passed*"])
|
|
|
|
# second run: collects only 5 items from test_2, because all tests from test_1 have passed
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf")
|
2019-04-29 03:23:38 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-02-20 04:33:03 +08:00
|
|
|
"collected 2 items",
|
2019-04-29 03:23:38 +08:00
|
|
|
"run-last-failure: rerun previous 2 failures (skipped 1 file)",
|
2020-02-20 04:33:03 +08:00
|
|
|
"*= 2 failed in *",
|
2019-04-29 03:23:38 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# add another file and check if message is correct when skipping more than 1 file
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-04-29 03:23:38 +08:00
|
|
|
**{
|
|
|
|
"pkg1/test_3.py": """
|
|
|
|
def test_3(): pass
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf")
|
2019-04-29 03:23:38 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-02-20 04:33:03 +08:00
|
|
|
"collected 2 items",
|
2019-04-29 03:23:38 +08:00
|
|
|
"run-last-failure: rerun previous 2 failures (skipped 2 files)",
|
2020-02-20 04:33:03 +08:00
|
|
|
"*= 2 failed in *",
|
2019-04-29 03:23:38 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-05-30 14:29:19 +08:00
|
|
|
def test_lastfailed_skip_collection_with_nesting(self, pytester: Pytester) -> None:
|
|
|
|
"""Check that file skipping works even when the file with failures is
|
|
|
|
nested at a different level of the collection tree."""
|
|
|
|
pytester.makepyfile(
|
|
|
|
**{
|
|
|
|
"test_1.py": """
|
|
|
|
def test_1(): pass
|
|
|
|
""",
|
|
|
|
"pkg/__init__.py": "",
|
|
|
|
"pkg/test_2.py": """
|
|
|
|
def test_2(): assert False
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
# first run
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["collected 2 items", "*1 failed*1 passed*"])
|
|
|
|
# second run - test_1.py is skipped.
|
|
|
|
result = pytester.runpytest("--lf")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: rerun previous 1 failure (skipped 1 file)",
|
|
|
|
"*= 1 failed in *",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_with_known_failures_not_being_selected(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-05-30 04:43:24 +08:00
|
|
|
**{
|
|
|
|
"pkg1/test_1.py": """def test_1(): assert 0""",
|
|
|
|
"pkg1/test_2.py": """def test_2(): pass""",
|
|
|
|
}
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-05-30 04:43:24 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
Path("pkg1/test_1.py").unlink()
|
|
|
|
result = pytester.runpytest("--lf")
|
2019-05-30 04:43:24 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: 1 known failures not in selected tests",
|
|
|
|
"* 1 passed in *",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2019-05-30 05:12:45 +08:00
|
|
|
# Recreate file with known failure.
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(**{"pkg1/test_1.py": """def test_1(): assert 0"""})
|
|
|
|
result = pytester.runpytest("--lf")
|
2019-05-30 05:12:45 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: rerun previous 1 failure (skipped 1 file)",
|
|
|
|
"* 1 failed in *",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-03-15 20:26:04 +08:00
|
|
|
# Remove/rename test: collects the file again.
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(**{"pkg1/test_1.py": """def test_renamed(): assert 0"""})
|
|
|
|
result = pytester.runpytest("--lf", "-rf")
|
2019-05-30 05:12:45 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-02-20 04:33:03 +08:00
|
|
|
"collected 2 items",
|
|
|
|
"run-last-failure: 1 known failures not in selected tests",
|
|
|
|
"pkg1/test_1.py F *",
|
|
|
|
"pkg1/test_2.py . *",
|
|
|
|
"FAILED pkg1/test_1.py::test_renamed - assert 0",
|
|
|
|
"* 1 failed, 1 passed in *",
|
2019-05-30 05:12:45 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--co")
|
2020-03-15 20:26:04 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: rerun previous 1 failure (skipped 1 file)",
|
|
|
|
"",
|
2023-06-02 21:03:39 +08:00
|
|
|
"<Dir *>",
|
|
|
|
" <Dir pkg1>",
|
|
|
|
" <Module test_1.py>",
|
|
|
|
" <Function test_renamed>",
|
2020-03-15 20:26:04 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_args_with_deselected(self, pytester: Pytester) -> None:
|
2020-03-15 20:26:04 +08:00
|
|
|
"""Test regression with --lf running into NoMatch error.
|
|
|
|
|
|
|
|
This was caused by it not collecting (non-failed) nodes given as
|
|
|
|
arguments.
|
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-03-15 20:26:04 +08:00
|
|
|
**{
|
|
|
|
"pkg1/test_1.py": """
|
|
|
|
def test_pass(): pass
|
|
|
|
def test_fail(): assert 0
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2020-03-15 20:26:04 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"])
|
|
|
|
assert result.ret == 1
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("pkg1/test_1.py::test_pass", "--lf", "--co")
|
2020-03-15 20:26:04 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*collected 1 item",
|
|
|
|
"run-last-failure: 1 known failures not in selected tests",
|
|
|
|
"",
|
2023-06-02 21:03:39 +08:00
|
|
|
"<Dir *>",
|
|
|
|
" <Dir pkg1>",
|
|
|
|
" <Module test_1.py>",
|
|
|
|
" <Function test_pass>",
|
2020-03-15 20:26:04 +08:00
|
|
|
],
|
|
|
|
consecutive=True,
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(
|
2020-03-15 20:26:04 +08:00
|
|
|
"pkg1/test_1.py::test_pass", "pkg1/test_1.py::test_fail", "--lf", "--co"
|
|
|
|
)
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 2 items / 1 deselected / 1 selected",
|
|
|
|
"run-last-failure: rerun previous 1 failure",
|
|
|
|
"",
|
2023-06-02 21:03:39 +08:00
|
|
|
"<Dir *>",
|
|
|
|
" <Dir pkg1>",
|
|
|
|
" <Module test_1.py>",
|
|
|
|
" <Function test_fail>",
|
2020-11-08 22:45:10 +08:00
|
|
|
"*= 1/2 tests collected (1 deselected) in *",
|
2020-03-15 20:26:04 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_with_class_items(self, pytester: Pytester) -> None:
|
2020-03-15 20:26:04 +08:00
|
|
|
"""Test regression with --lf deselecting whole classes."""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-03-15 20:26:04 +08:00
|
|
|
**{
|
|
|
|
"pkg1/test_1.py": """
|
|
|
|
class TestFoo:
|
|
|
|
def test_pass(self): pass
|
|
|
|
def test_fail(self): assert 0
|
|
|
|
|
|
|
|
def test_other(): assert 0
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2020-03-15 20:26:04 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 3 items", "* 2 failed, 1 passed in *"])
|
|
|
|
assert result.ret == 1
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--co")
|
2020-03-15 20:26:04 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 3 items / 1 deselected / 2 selected",
|
|
|
|
"run-last-failure: rerun previous 2 failures",
|
|
|
|
"",
|
2023-06-02 21:03:39 +08:00
|
|
|
"<Dir *>",
|
|
|
|
" <Dir pkg1>",
|
|
|
|
" <Module test_1.py>",
|
|
|
|
" <Class TestFoo>",
|
|
|
|
" <Function test_fail>",
|
|
|
|
" <Function test_other>",
|
2020-03-15 20:26:04 +08:00
|
|
|
"",
|
2020-11-08 22:45:10 +08:00
|
|
|
"*= 2/3 tests collected (1 deselected) in *",
|
2020-03-15 20:26:04 +08:00
|
|
|
],
|
|
|
|
consecutive=True,
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_lastfailed_with_all_filtered(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-03-15 20:26:04 +08:00
|
|
|
**{
|
|
|
|
"pkg1/test_1.py": """
|
|
|
|
def test_fail(): assert 0
|
|
|
|
def test_pass(): pass
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2020-03-15 20:26:04 +08:00
|
|
|
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"])
|
|
|
|
assert result.ret == 1
|
|
|
|
|
|
|
|
# Remove known failure.
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-03-15 20:26:04 +08:00
|
|
|
**{
|
|
|
|
"pkg1/test_1.py": """
|
|
|
|
def test_pass(): pass
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--lf", "--co")
|
2020-03-15 20:26:04 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: 1 known failures not in selected tests",
|
|
|
|
"",
|
2023-06-02 21:03:39 +08:00
|
|
|
"<Dir *>",
|
|
|
|
" <Dir pkg1>",
|
|
|
|
" <Module test_1.py>",
|
|
|
|
" <Function test_pass>",
|
2020-03-15 20:26:04 +08:00
|
|
|
"",
|
2020-11-08 22:45:10 +08:00
|
|
|
"*= 1 test collected in*",
|
2020-03-15 20:26:04 +08:00
|
|
|
],
|
|
|
|
consecutive=True,
|
|
|
|
)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-10-19 23:34:03 +08:00
|
|
|
def test_packages(self, pytester: Pytester) -> None:
|
|
|
|
"""Regression test for #7758.
|
|
|
|
|
|
|
|
The particular issue here was that Package nodes were included in the
|
|
|
|
filtering, being themselves Modules for the __init__.py, even if they
|
|
|
|
had failed Modules in them.
|
|
|
|
|
|
|
|
The tests includes a test in an __init__.py file just to make sure the
|
|
|
|
fix doesn't somehow regress that, it is not critical for the issue.
|
|
|
|
"""
|
|
|
|
pytester.makepyfile(
|
|
|
|
**{
|
|
|
|
"__init__.py": "",
|
|
|
|
"a/__init__.py": "def test_a_init(): assert False",
|
|
|
|
"a/test_one.py": "def test_1(): assert False",
|
|
|
|
"b/__init__.py": "",
|
|
|
|
"b/test_two.py": "def test_2(): assert False",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
pytester.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
python_files = *.py
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.assert_outcomes(failed=3)
|
|
|
|
result = pytester.runpytest("--lf")
|
|
|
|
result.assert_outcomes(failed=3)
|
|
|
|
|
2023-06-02 16:41:36 +08:00
|
|
|
def test_non_python_file_skipped(
|
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
dummy_yaml_custom_test: None,
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
**{
|
|
|
|
"test_bad.py": """def test_bad(): assert False""",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"])
|
|
|
|
|
|
|
|
result = pytester.runpytest("--lf")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"collected 1 item",
|
|
|
|
"run-last-failure: rerun previous 1 failure (skipped 1 file)",
|
|
|
|
"* 1 failed in *",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2018-02-14 03:49:28 +08:00
|
|
|
|
|
|
|
class TestNewFirst:
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_newfirst_usecase(self, pytester: Pytester) -> None:
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-02-24 03:49:17 +08:00
|
|
|
**{
|
|
|
|
"test_1/test_1.py": """
|
|
|
|
def test_1(): assert 1
|
|
|
|
""",
|
|
|
|
"test_2/test_2.py": """
|
|
|
|
def test_1(): assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
2018-02-24 03:49:17 +08:00
|
|
|
}
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
p1 = pytester.path.joinpath("test_1/test_1.py")
|
|
|
|
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9)))
|
|
|
|
|
|
|
|
result = pytester.runpytest("-v")
|
2018-02-14 03:49:28 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2020-03-15 20:26:04 +08:00
|
|
|
["*test_1/test_1.py::test_1 PASSED*", "*test_2/test_2.py::test_1 PASSED*"]
|
2018-02-14 03:49:28 +08:00
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-v", "--nf")
|
2018-02-14 03:49:28 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2020-03-15 20:26:04 +08:00
|
|
|
["*test_2/test_2.py::test_1 PASSED*", "*test_1/test_1.py::test_1 PASSED*"]
|
2018-02-14 03:49:28 +08:00
|
|
|
)
|
|
|
|
|
2023-06-20 19:55:40 +08:00
|
|
|
p1.write_text(
|
|
|
|
"def test_1(): assert 1\n" "def test_2(): assert 1\n", encoding="utf-8"
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9)))
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--nf", "--collect-only", "-q")
|
2020-03-15 20:26:04 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"test_1/test_1.py::test_2",
|
|
|
|
"test_2/test_2.py::test_1",
|
|
|
|
"test_1/test_1.py::test_1",
|
|
|
|
]
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2020-03-15 20:26:04 +08:00
|
|
|
# Newest first with (plugin) pytest_collection_modifyitems hook.
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-03-15 20:26:04 +08:00
|
|
|
myplugin="""
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
|
|
items[:] = sorted(items, key=lambda item: item.nodeid)
|
|
|
|
print("new_items:", [x.nodeid for x in items])
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
result = pytester.runpytest("--nf", "-p", "myplugin", "--collect-only", "-q")
|
2018-02-14 03:49:28 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-03-15 20:26:04 +08:00
|
|
|
"new_items: *test_1.py*test_1.py*test_2.py*",
|
|
|
|
"test_1/test_1.py::test_2",
|
|
|
|
"test_2/test_2.py::test_1",
|
|
|
|
"test_1/test_1.py::test_1",
|
2018-02-14 03:49:28 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_newfirst_parametrize(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-02-24 03:49:17 +08:00
|
|
|
**{
|
|
|
|
"test_1/test_1.py": """
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('num', [1, 2])
|
|
|
|
def test_1(num): assert num
|
|
|
|
""",
|
|
|
|
"test_2/test_2.py": """
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('num', [1, 2])
|
|
|
|
def test_1(num): assert num
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
2018-02-24 03:49:17 +08:00
|
|
|
}
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
p1 = pytester.path.joinpath("test_1/test_1.py")
|
|
|
|
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9)))
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-v")
|
2018-02-14 03:49:28 +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*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-v", "--nf")
|
2018-02-14 03:49:28 +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*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
p1.write_text(
|
2018-02-14 03:49:28 +08:00
|
|
|
"import pytest\n"
|
|
|
|
"@pytest.mark.parametrize('num', [1, 2, 3])\n"
|
2023-06-20 19:55:40 +08:00
|
|
|
"def test_1(num): assert num\n",
|
|
|
|
encoding="utf-8",
|
2018-02-14 03:49:28 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9)))
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2019-10-19 10:44:22 +08:00
|
|
|
# Running only a subset does not forget about existing ones.
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-v", "--nf", "test_2/test_2.py")
|
2019-10-19 10:44:22 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*test_2/test_2.py::test_1[1*", "*test_2/test_2.py::test_1[2*"]
|
|
|
|
)
|
2018-02-14 03:49:28 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-v", "--nf")
|
2018-02-14 03:49:28 +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:
|
2020-12-15 19:02:32 +08:00
|
|
|
def check_readme(self, pytester: Pytester) -> bool:
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.cache is not None
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_readme_passed(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile("def test_always_passes(): pass")
|
|
|
|
pytester.runpytest()
|
|
|
|
assert self.check_readme(pytester) is True
|
2018-06-21 20:13:10 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_readme_failed(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile("def test_always_fails(): assert 0")
|
|
|
|
pytester.runpytest()
|
|
|
|
assert self.check_readme(pytester) is True
|
2018-09-15 02:15:12 +08:00
|
|
|
|
|
|
|
|
2024-04-08 22:02:31 +08:00
|
|
|
class Action(Enum):
|
|
|
|
"""Action to perform on the cache directory."""
|
|
|
|
|
|
|
|
MKDIR = auto()
|
|
|
|
SET = auto()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("action", list(Action))
|
|
|
|
def test_gitignore(
|
|
|
|
pytester: Pytester,
|
|
|
|
action: Action,
|
|
|
|
) -> None:
|
2018-09-15 02:15:12 +08:00
|
|
|
"""Ensure we automatically create .gitignore file in the pytest_cache directory (#3286)."""
|
|
|
|
from _pytest.cacheprovider import Cache
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
config = pytester.parseconfig()
|
2020-09-28 03:20:31 +08:00
|
|
|
cache = Cache.for_config(config, _ispytest=True)
|
2024-04-08 22:02:31 +08:00
|
|
|
if action == Action.MKDIR:
|
|
|
|
cache.mkdir("foo")
|
|
|
|
elif action == Action.SET:
|
|
|
|
cache.set("foo", "bar")
|
|
|
|
else:
|
|
|
|
assert_never(action)
|
2019-10-20 04:45:15 +08:00
|
|
|
msg = "# Created by pytest automatically.\n*\n"
|
2018-11-15 04:55:37 +08:00
|
|
|
gitignore_path = cache._cachedir.joinpath(".gitignore")
|
|
|
|
assert gitignore_path.read_text(encoding="UTF-8") == msg
|
|
|
|
|
|
|
|
# Does not overwrite existing/custom one.
|
2023-06-20 19:55:40 +08:00
|
|
|
gitignore_path.write_text("custom", encoding="utf-8")
|
2024-04-08 22:02:31 +08:00
|
|
|
if action == Action.MKDIR:
|
|
|
|
cache.mkdir("something")
|
|
|
|
elif action == Action.SET:
|
|
|
|
cache.set("something", "else")
|
|
|
|
else:
|
|
|
|
assert_never(action)
|
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
|
|
|
|
|
|
|
|
2021-10-19 06:15:34 +08:00
|
|
|
def test_preserve_keys_order(pytester: Pytester) -> None:
|
|
|
|
"""Ensure keys order is preserved when saving dicts (#9205)."""
|
|
|
|
from _pytest.cacheprovider import Cache
|
|
|
|
|
|
|
|
config = pytester.parseconfig()
|
|
|
|
cache = Cache.for_config(config, _ispytest=True)
|
|
|
|
cache.set("foo", {"z": 1, "b": 2, "a": 3, "d": 10})
|
|
|
|
read_back = cache.get("foo", None)
|
|
|
|
assert list(read_back.items()) == [("z", 1), ("b", 2), ("a", 3), ("d", 10)]
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_does_not_create_boilerplate_in_existing_dirs(pytester: Pytester) -> None:
|
2018-11-15 04:55:37 +08:00
|
|
|
from _pytest.cacheprovider import Cache
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini(
|
2018-11-15 04:55:37 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
cache_dir = .
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
config = pytester.parseconfig()
|
2020-09-28 03:20:31 +08:00
|
|
|
cache = Cache.for_config(config, _ispytest=True)
|
2018-11-15 04:55:37 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cachedir_tag(pytester: Pytester) -> None:
|
2018-12-04 20:49:08 +08:00
|
|
|
"""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
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
config = pytester.parseconfig()
|
2020-09-28 03:20:31 +08:00
|
|
|
cache = Cache.for_config(config, _ispytest=True)
|
2018-12-04 20:49:08 +08:00
|
|
|
cache.set("foo", "bar")
|
|
|
|
cachedir_tag_path = cache._cachedir.joinpath("CACHEDIR.TAG")
|
|
|
|
assert cachedir_tag_path.read_bytes() == CACHEDIR_TAG_CONTENT
|
2023-01-19 20:44:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_clioption_with_cacheshow_and_help(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--cache-show", "--help")
|
|
|
|
assert result.ret == 0
|