| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Generator |
| 4 |
from collections.abc import Sequence |
| 5 |
from enum import auto |
| 6 |
from enum import Enum |
| 7 |
import os |
| 8 |
from pathlib import Path |
| 9 |
import shutil |
| 10 |
from typing import Any |
| 11 |
|
| 12 |
from _pytest.compat import assert_never |
| 13 |
from _pytest.config import ExitCode |
| 14 |
from _pytest.monkeypatch import MonkeyPatch |
| 15 |
from _pytest.pytester import Pytester |
| 16 |
from _pytest.tmpdir import TempPathFactory |
| 17 |
import pytest |
| 18 |
|
| 19 |
|
| 20 |
pytest_plugins = ("pytester",) |
| 21 |
|
| 22 |
|
| 23 |
class TestNewAPI: |
| 24 |
def test_config_cache_mkdir(self, pytester: Pytester) -> None: |
| 25 |
pytester.makeini("[pytest]") |
| 26 |
config = pytester.parseconfigure() |
| 27 |
assert config.cache is not None |
| 28 |
with pytest.raises(ValueError): |
| 29 |
config.cache.mkdir("key/name") |
| 30 |
|
| 31 |
p = config.cache.mkdir("name") |
| 32 |
assert p.is_dir() |
| 33 |
|
| 34 |
def test_cache_dir_permissions(self, pytester: Pytester) -> None: |
| 35 |
"""The .pytest_cache directory should have world-readable permissions |
| 36 |
(depending on umask). |
| 37 |
|
| 38 |
Regression test for #12308. |
| 39 |
""" |
| 40 |
pytester.makeini("[pytest]") |
| 41 |
config = pytester.parseconfigure() |
| 42 |
assert config.cache is not None |
| 43 |
p = config.cache.mkdir("name") |
| 44 |
assert p.is_dir() |
| 45 |
# Instead of messing with umask, make sure .pytest_cache has the same |
| 46 |
# permissions as the default that `mkdir` gives `p`. |
| 47 |
assert (p.parent.stat().st_mode & 0o777) == (p.stat().st_mode & 0o777) |
| 48 |
|
| 49 |
def test_config_cache_dataerror(self, pytester: Pytester) -> None: |
| 50 |
pytester.makeini("[pytest]") |
| 51 |
config = pytester.parseconfigure() |
| 52 |
assert config.cache is not None |
| 53 |
cache = config.cache |
| 54 |
with pytest.raises(TypeError): |
| 55 |
cache.set("key/name", cache) |
| 56 |
config.cache.set("key/name", 0) |
| 57 |
config.cache._getvaluepath("key/name").write_bytes(b"123invalid") |
| 58 |
val = config.cache.get("key/name", -2) |
| 59 |
assert val == -2 |
| 60 |
|
| 61 |
@pytest.mark.filterwarnings("ignore:could not create cache path") |
| 62 |
def test_cache_writefail_cachefile_silent(self, pytester: Pytester) -> None: |
| 63 |
pytester.makeini("[pytest]") |
| 64 |
pytester.path.joinpath(".pytest_cache").write_text( |
| 65 |
"gone wrong", encoding="utf-8" |
| 66 |
) |
| 67 |
config = pytester.parseconfigure() |
| 68 |
cache = config.cache |
| 69 |
assert cache is not None |
| 70 |
cache.set("test/broken", []) |
| 71 |
|
| 72 |
@pytest.fixture |
| 73 |
def unwritable_cache_dir(self, pytester: Pytester) -> Generator[Path]: |
| 74 |
cache_dir = pytester.path.joinpath(".pytest_cache") |
| 75 |
cache_dir.mkdir() |
| 76 |
mode = cache_dir.stat().st_mode |
| 77 |
cache_dir.chmod(0) |
| 78 |
if os.access(cache_dir, os.W_OK): |
| 79 |
pytest.skip("Failed to make cache dir unwritable") |
| 80 |
|
| 81 |
yield cache_dir |
| 82 |
cache_dir.chmod(mode) |
| 83 |
|
| 84 |
@pytest.mark.filterwarnings( |
| 85 |
"ignore:could not create cache path:pytest.PytestWarning" |
| 86 |
) |
| 87 |
def test_cache_writefail_permissions( |
| 88 |
self, unwritable_cache_dir: Path, pytester: Pytester |
| 89 |
) -> None: |
| 90 |
pytester.makeini("[pytest]") |
| 91 |
config = pytester.parseconfigure() |
| 92 |
cache = config.cache |
| 93 |
assert cache is not None |
| 94 |
cache.set("test/broken", []) |
| 95 |
|
| 96 |
@pytest.mark.filterwarnings("default") |
| 97 |
def test_cache_failure_warns( |
| 98 |
self, |
| 99 |
pytester: Pytester, |
| 100 |
monkeypatch: MonkeyPatch, |
| 101 |
unwritable_cache_dir: Path, |
| 102 |
) -> None: |
| 103 |
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") |
| 104 |
|
| 105 |
pytester.makepyfile("def test_error(): raise Exception") |
| 106 |
result = pytester.runpytest() |
| 107 |
assert result.ret == 1 |
| 108 |
# warnings from nodeids and lastfailed |
| 109 |
result.stdout.fnmatch_lines( |
| 110 |
[ |
| 111 |
# Validate location/stacklevel of warning from cacheprovider. |
| 112 |
"*= warnings summary =*", |
| 113 |
"*/cacheprovider.py:*", |
| 114 |
" */cacheprovider.py:*: PytestCacheWarning: could not create cache path " |
| 115 |
f"{unwritable_cache_dir}/v/cache/nodeids: *", |
| 116 |
' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))', |
| 117 |
"*1 failed, 2 warnings in*", |
| 118 |
] |
| 119 |
) |
| 120 |
|
| 121 |
def test_config_cache(self, pytester: Pytester) -> None: |
| 122 |
pytester.makeconftest( |
| 123 |
""" |
| 124 |
def pytest_configure(config): |
| 125 |
# see that we get cache information early on |
| 126 |
assert hasattr(config, "cache") |
| 127 |
""" |
| 128 |
) |
| 129 |
pytester.makepyfile( |
| 130 |
""" |
| 131 |
def test_session(pytestconfig): |
| 132 |
assert hasattr(pytestconfig, "cache") |
| 133 |
""" |
| 134 |
) |
| 135 |
result = pytester.runpytest() |
| 136 |
assert result.ret == 0 |
| 137 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 138 |
|
| 139 |
def test_cachefuncarg(self, pytester: Pytester) -> None: |
| 140 |
pytester.makepyfile( |
| 141 |
""" |
| 142 |
import pytest |
| 143 |
def test_cachefuncarg(cache): |
| 144 |
val = cache.get("some/thing", None) |
| 145 |
assert val is None |
| 146 |
cache.set("some/thing", [1]) |
| 147 |
with pytest.raises(TypeError): |
| 148 |
cache.get("some/thing") |
| 149 |
val = cache.get("some/thing", []) |
| 150 |
assert val == [1] |
| 151 |
""" |
| 152 |
) |
| 153 |
result = pytester.runpytest() |
| 154 |
assert result.ret == 0 |
| 155 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 156 |
|
| 157 |
def test_custom_rel_cache_dir(self, pytester: Pytester) -> None: |
| 158 |
rel_cache_dir = os.path.join("custom_cache_dir", "subdir") |
| 159 |
pytester.makeini( |
| 160 |
f""" |
| 161 |
[pytest] |
| 162 |
cache_dir = {rel_cache_dir} |
| 163 |
""" |
| 164 |
) |
| 165 |
pytester.makepyfile(test_errored="def test_error():\n assert False") |
| 166 |
pytester.runpytest() |
| 167 |
assert pytester.path.joinpath(rel_cache_dir).is_dir() |
| 168 |
|
| 169 |
def test_custom_abs_cache_dir( |
| 170 |
self, pytester: Pytester, tmp_path_factory: TempPathFactory |
| 171 |
) -> None: |
| 172 |
tmp = tmp_path_factory.mktemp("tmp") |
| 173 |
abs_cache_dir = tmp / "custom_cache_dir" |
| 174 |
pytester.makeini( |
| 175 |
f""" |
| 176 |
[pytest] |
| 177 |
cache_dir = {abs_cache_dir} |
| 178 |
""" |
| 179 |
) |
| 180 |
pytester.makepyfile(test_errored="def test_error():\n assert False") |
| 181 |
pytester.runpytest() |
| 182 |
assert abs_cache_dir.is_dir() |
| 183 |
|
| 184 |
def test_custom_cache_dir_with_env_var( |
| 185 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 186 |
) -> None: |
| 187 |
monkeypatch.setenv("env_var", "custom_cache_dir") |
| 188 |
pytester.makeini( |
| 189 |
""" |
| 190 |
[pytest] |
| 191 |
cache_dir = {cache_dir} |
| 192 |
""".format(cache_dir="$env_var") |
| 193 |
) |
| 194 |
pytester.makepyfile(test_errored="def test_error():\n assert False") |
| 195 |
pytester.runpytest() |
| 196 |
assert pytester.path.joinpath("custom_cache_dir").is_dir() |
| 197 |
|
| 198 |
|
| 199 |
@pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "mydir/tox-env"))) |
| 200 |
def test_cache_reportheader( |
| 201 |
env: Sequence[str], pytester: Pytester, monkeypatch: MonkeyPatch |
| 202 |
) -> None: |
| 203 |
pytester.makepyfile("""def test_foo(): pass""") |
| 204 |
if env: |
| 205 |
monkeypatch.setenv(*env) |
| 206 |
expected = os.path.join(env[1], ".pytest_cache") |
| 207 |
else: |
| 208 |
monkeypatch.delenv("TOX_ENV_DIR", raising=False) |
| 209 |
expected = ".pytest_cache" |
| 210 |
result = pytester.runpytest("-v") |
| 211 |
result.stdout.fnmatch_lines([f"cachedir: {expected}"]) |
| 212 |
|
| 213 |
|
| 214 |
def test_cache_reportheader_external_abspath( |
| 215 |
pytester: Pytester, tmp_path_factory: TempPathFactory |
| 216 |
) -> None: |
| 217 |
external_cache = tmp_path_factory.mktemp( |
| 218 |
"test_cache_reportheader_external_abspath_abs" |
| 219 |
) |
| 220 |
|
| 221 |
pytester.makepyfile("def test_hello(): pass") |
| 222 |
pytester.makeini( |
| 223 |
f""" |
| 224 |
[pytest] |
| 225 |
cache_dir = {external_cache} |
| 226 |
""" |
| 227 |
) |
| 228 |
result = pytester.runpytest("-v") |
| 229 |
result.stdout.fnmatch_lines([f"cachedir: {external_cache}"]) |
| 230 |
|
| 231 |
|
| 232 |
def test_cache_show(pytester: Pytester) -> None: |
| 233 |
result = pytester.runpytest("--cache-show") |
| 234 |
assert result.ret == 0 |
| 235 |
result.stdout.fnmatch_lines(["*cache is empty*"]) |
| 236 |
pytester.makeconftest( |
| 237 |
""" |
| 238 |
def pytest_configure(config): |
| 239 |
config.cache.set("my/name", [1,2,3]) |
| 240 |
config.cache.set("my/hello", "world") |
| 241 |
config.cache.set("other/some", {1:2}) |
| 242 |
dp = config.cache.mkdir("mydb") |
| 243 |
dp.joinpath("hello").touch() |
| 244 |
dp.joinpath("world").touch() |
| 245 |
""" |
| 246 |
) |
| 247 |
result = pytester.runpytest() |
| 248 |
assert result.ret == 5 # no tests executed |
| 249 |
|
| 250 |
result = pytester.runpytest("--cache-show") |
| 251 |
result.stdout.fnmatch_lines( |
| 252 |
[ |
| 253 |
"*cachedir:*", |
| 254 |
"*- cache values for '[*]' -*", |
| 255 |
"cache/nodeids contains:", |
| 256 |
"my/name contains:", |
| 257 |
" [1, 2, 3]", |
| 258 |
"other/some contains:", |
| 259 |
" {*'1': 2}", |
| 260 |
"*- cache directories for '[*]' -*", |
| 261 |
"*mydb/hello*length 0*", |
| 262 |
"*mydb/world*length 0*", |
| 263 |
] |
| 264 |
) |
| 265 |
assert result.ret == 0 |
| 266 |
|
| 267 |
result = pytester.runpytest("--cache-show", "*/hello") |
| 268 |
result.stdout.fnmatch_lines( |
| 269 |
[ |
| 270 |
"*cachedir:*", |
| 271 |
"*- cache values for '[*]/hello' -*", |
| 272 |
"my/hello contains:", |
| 273 |
" *'world'", |
| 274 |
"*- cache directories for '[*]/hello' -*", |
| 275 |
"d/mydb/hello*length 0*", |
| 276 |
] |
| 277 |
) |
| 278 |
stdout = result.stdout.str() |
| 279 |
assert "other/some" not in stdout |
| 280 |
assert "d/mydb/world" not in stdout |
| 281 |
assert result.ret == 0 |
| 282 |
|
| 283 |
|
| 284 |
class TestLastFailed: |
| 285 |
def test_lastfailed_usecase( |
| 286 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 287 |
) -> None: |
| 288 |
monkeypatch.setattr("sys.dont_write_bytecode", True) |
| 289 |
p = pytester.makepyfile( |
| 290 |
""" |
| 291 |
def test_1(): assert 0 |
| 292 |
def test_2(): assert 0 |
| 293 |
def test_3(): assert 1 |
| 294 |
""" |
| 295 |
) |
| 296 |
result = pytester.runpytest(str(p)) |
| 297 |
result.stdout.fnmatch_lines(["*2 failed*"]) |
| 298 |
p = pytester.makepyfile( |
| 299 |
""" |
| 300 |
def test_1(): assert 1 |
| 301 |
def test_2(): assert 1 |
| 302 |
def test_3(): assert 0 |
| 303 |
""" |
| 304 |
) |
| 305 |
result = pytester.runpytest(str(p), "--lf") |
| 306 |
result.stdout.fnmatch_lines( |
| 307 |
[ |
| 308 |
"collected 3 items / 1 deselected / 2 selected", |
| 309 |
"run-last-failure: rerun previous 2 failures", |
| 310 |
"*= 2 passed, 1 deselected in *", |
| 311 |
] |
| 312 |
) |
| 313 |
result = pytester.runpytest(str(p), "--lf") |
| 314 |
result.stdout.fnmatch_lines( |
| 315 |
[ |
| 316 |
"collected 3 items", |
| 317 |
"run-last-failure: no previously failed tests, not deselecting items.", |
| 318 |
"*1 failed*2 passed*", |
| 319 |
] |
| 320 |
) |
| 321 |
pytester.path.joinpath(".pytest_cache", ".git").mkdir(parents=True) |
| 322 |
result = pytester.runpytest(str(p), "--lf", "--cache-clear") |
| 323 |
result.stdout.fnmatch_lines(["*1 failed*2 passed*"]) |
| 324 |
assert pytester.path.joinpath(".pytest_cache", "README.md").is_file() |
| 325 |
assert pytester.path.joinpath(".pytest_cache", ".git").is_dir() |
| 326 |
|
| 327 |
# Run this again to make sure clear-cache is robust |
| 328 |
if os.path.isdir(".pytest_cache"): |
| 329 |
shutil.rmtree(".pytest_cache") |
| 330 |
result = pytester.runpytest("--lf", "--cache-clear") |
| 331 |
result.stdout.fnmatch_lines(["*1 failed*2 passed*"]) |
| 332 |
|
| 333 |
def test_failedfirst_order(self, pytester: Pytester) -> None: |
| 334 |
pytester.makepyfile( |
| 335 |
test_a="def test_always_passes(): pass", |
| 336 |
test_b="def test_always_fails(): assert 0", |
| 337 |
) |
| 338 |
result = pytester.runpytest() |
| 339 |
# Test order will be collection order; alphabetical |
| 340 |
result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"]) |
| 341 |
result = pytester.runpytest("--ff") |
| 342 |
# Test order will be failing tests first |
| 343 |
result.stdout.fnmatch_lines( |
| 344 |
[ |
| 345 |
"collected 2 items", |
| 346 |
"run-last-failure: rerun previous 1 failure first", |
| 347 |
"test_b.py*", |
| 348 |
"test_a.py*", |
| 349 |
] |
| 350 |
) |
| 351 |
|
| 352 |
def test_lastfailed_failedfirst_order(self, pytester: Pytester) -> None: |
| 353 |
pytester.makepyfile( |
| 354 |
test_a="def test_always_passes(): assert 1", |
| 355 |
test_b="def test_always_fails(): assert 0", |
| 356 |
) |
| 357 |
result = pytester.runpytest() |
| 358 |
# Test order will be collection order; alphabetical |
| 359 |
result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"]) |
| 360 |
result = pytester.runpytest("--lf", "--ff") |
| 361 |
# Test order will be failing tests first |
| 362 |
result.stdout.fnmatch_lines(["test_b.py*"]) |
| 363 |
result.stdout.no_fnmatch_line("*test_a.py*") |
| 364 |
|
| 365 |
def test_lastfailed_difference_invocations( |
| 366 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 367 |
) -> None: |
| 368 |
monkeypatch.setattr("sys.dont_write_bytecode", True) |
| 369 |
pytester.makepyfile( |
| 370 |
test_a=""" |
| 371 |
def test_a1(): assert 0 |
| 372 |
def test_a2(): assert 1 |
| 373 |
""", |
| 374 |
test_b="def test_b1(): assert 0", |
| 375 |
) |
| 376 |
p = pytester.path.joinpath("test_a.py") |
| 377 |
p2 = pytester.path.joinpath("test_b.py") |
| 378 |
|
| 379 |
result = pytester.runpytest() |
| 380 |
result.stdout.fnmatch_lines(["*2 failed*"]) |
| 381 |
result = pytester.runpytest("--lf", p2) |
| 382 |
result.stdout.fnmatch_lines(["*1 failed*"]) |
| 383 |
|
| 384 |
pytester.makepyfile(test_b="def test_b1(): assert 1") |
| 385 |
result = pytester.runpytest("--lf", p2) |
| 386 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 387 |
result = pytester.runpytest("--lf", p) |
| 388 |
result.stdout.fnmatch_lines( |
| 389 |
[ |
| 390 |
"collected 2 items / 1 deselected / 1 selected", |
| 391 |
"run-last-failure: rerun previous 1 failure", |
| 392 |
"*= 1 failed, 1 deselected in *", |
| 393 |
] |
| 394 |
) |
| 395 |
|
| 396 |
def test_lastfailed_usecase_splice( |
| 397 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 398 |
) -> None: |
| 399 |
monkeypatch.setattr("sys.dont_write_bytecode", True) |
| 400 |
pytester.makepyfile( |
| 401 |
"def test_1(): assert 0", test_something="def test_2(): assert 0" |
| 402 |
) |
| 403 |
p2 = pytester.path.joinpath("test_something.py") |
| 404 |
result = pytester.runpytest() |
| 405 |
result.stdout.fnmatch_lines(["*2 failed*"]) |
| 406 |
result = pytester.runpytest("--lf", p2) |
| 407 |
result.stdout.fnmatch_lines(["*1 failed*"]) |
| 408 |
result = pytester.runpytest("--lf") |
| 409 |
result.stdout.fnmatch_lines(["*2 failed*"]) |
| 410 |
|
| 411 |
def test_lastfailed_xpass(self, pytester: Pytester) -> None: |
| 412 |
pytester.inline_runsource( |
| 413 |
""" |
| 414 |
import pytest |
| 415 |
@pytest.mark.xfail |
| 416 |
def test_hello(): |
| 417 |
assert 1 |
| 418 |
""" |
| 419 |
) |
| 420 |
config = pytester.parseconfigure() |
| 421 |
assert config.cache is not None |
| 422 |
lastfailed = config.cache.get("cache/lastfailed", -1) |
| 423 |
assert lastfailed == -1 |
| 424 |
|
| 425 |
def test_non_serializable_parametrize(self, pytester: Pytester) -> None: |
| 426 |
"""Test that failed parametrized tests with unmarshable parameters |
| 427 |
don't break pytest-cache. |
| 428 |
""" |
| 429 |
pytester.makepyfile( |
| 430 |
r""" |
| 431 |
import pytest |
| 432 |
|
| 433 |
@pytest.mark.parametrize('val', [ |
| 434 |
b'\xac\x10\x02G', |
| 435 |
]) |
| 436 |
def test_fail(val): |
| 437 |
assert False |
| 438 |
""" |
| 439 |
) |
| 440 |
result = pytester.runpytest() |
| 441 |
result.stdout.fnmatch_lines(["*1 failed in*"]) |
| 442 |
|
| 443 |
@pytest.mark.parametrize("parent", ("directory", "package")) |
| 444 |
def test_terminal_report_lastfailed(self, pytester: Pytester, parent: str) -> None: |
| 445 |
if parent == "package": |
| 446 |
pytester.makepyfile( |
| 447 |
__init__="", |
| 448 |
) |
| 449 |
|
| 450 |
test_a = pytester.makepyfile( |
| 451 |
test_a=""" |
| 452 |
def test_a1(): pass |
| 453 |
def test_a2(): pass |
| 454 |
""" |
| 455 |
) |
| 456 |
test_b = pytester.makepyfile( |
| 457 |
test_b=""" |
| 458 |
def test_b1(): assert 0 |
| 459 |
def test_b2(): assert 0 |
| 460 |
""" |
| 461 |
) |
| 462 |
result = pytester.runpytest() |
| 463 |
result.stdout.fnmatch_lines(["collected 4 items", "*2 failed, 2 passed in*"]) |
| 464 |
|
| 465 |
result = pytester.runpytest("--lf") |
| 466 |
result.stdout.fnmatch_lines( |
| 467 |
[ |
| 468 |
"collected 2 items", |
| 469 |
"run-last-failure: rerun previous 2 failures (skipped 1 file)", |
| 470 |
"*2 failed in*", |
| 471 |
] |
| 472 |
) |
| 473 |
|
| 474 |
result = pytester.runpytest(test_a, "--lf") |
| 475 |
result.stdout.fnmatch_lines( |
| 476 |
[ |
| 477 |
"collected 2 items", |
| 478 |
"run-last-failure: 2 known failures not in selected tests", |
| 479 |
"*2 passed in*", |
| 480 |
] |
| 481 |
) |
| 482 |
|
| 483 |
result = pytester.runpytest(test_b, "--lf") |
| 484 |
result.stdout.fnmatch_lines( |
| 485 |
[ |
| 486 |
"collected 2 items", |
| 487 |
"run-last-failure: rerun previous 2 failures", |
| 488 |
"*2 failed in*", |
| 489 |
] |
| 490 |
) |
| 491 |
|
| 492 |
result = pytester.runpytest("test_b.py::test_b1", "--lf") |
| 493 |
result.stdout.fnmatch_lines( |
| 494 |
[ |
| 495 |
"collected 1 item", |
| 496 |
"run-last-failure: rerun previous 1 failure", |
| 497 |
"*1 failed in*", |
| 498 |
] |
| 499 |
) |
| 500 |
|
| 501 |
def test_terminal_report_failedfirst(self, pytester: Pytester) -> None: |
| 502 |
pytester.makepyfile( |
| 503 |
test_a=""" |
| 504 |
def test_a1(): assert 0 |
| 505 |
def test_a2(): pass |
| 506 |
""" |
| 507 |
) |
| 508 |
result = pytester.runpytest() |
| 509 |
result.stdout.fnmatch_lines(["collected 2 items", "*1 failed, 1 passed in*"]) |
| 510 |
|
| 511 |
result = pytester.runpytest("--ff") |
| 512 |
result.stdout.fnmatch_lines( |
| 513 |
[ |
| 514 |
"collected 2 items", |
| 515 |
"run-last-failure: rerun previous 1 failure first", |
| 516 |
"*1 failed, 1 passed in*", |
| 517 |
] |
| 518 |
) |
| 519 |
|
| 520 |
def test_lastfailed_collectfailure( |
| 521 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 522 |
) -> None: |
| 523 |
pytester.makepyfile( |
| 524 |
test_maybe=""" |
| 525 |
import os |
| 526 |
env = os.environ |
| 527 |
if '1' == env['FAILIMPORT']: |
| 528 |
raise ImportError('fail') |
| 529 |
def test_hello(): |
| 530 |
assert '0' == env['FAILTEST'] |
| 531 |
""" |
| 532 |
) |
| 533 |
|
| 534 |
def rlf(fail_import: int, fail_run: int) -> Any: |
| 535 |
monkeypatch.setenv("FAILIMPORT", str(fail_import)) |
| 536 |
monkeypatch.setenv("FAILTEST", str(fail_run)) |
| 537 |
|
| 538 |
pytester.runpytest("-q") |
| 539 |
config = pytester.parseconfigure() |
| 540 |
assert config.cache is not None |
| 541 |
lastfailed = config.cache.get("cache/lastfailed", -1) |
| 542 |
return lastfailed |
| 543 |
|
| 544 |
lastfailed = rlf(fail_import=0, fail_run=0) |
| 545 |
assert lastfailed == -1 |
| 546 |
|
| 547 |
lastfailed = rlf(fail_import=1, fail_run=0) |
| 548 |
assert list(lastfailed) == ["test_maybe.py"] |
| 549 |
|
| 550 |
lastfailed = rlf(fail_import=0, fail_run=1) |
| 551 |
assert list(lastfailed) == ["test_maybe.py::test_hello"] |
| 552 |
|
| 553 |
def test_lastfailed_failure_subset( |
| 554 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 555 |
) -> None: |
| 556 |
pytester.makepyfile( |
| 557 |
test_maybe=""" |
| 558 |
import os |
| 559 |
env = os.environ |
| 560 |
if '1' == env['FAILIMPORT']: |
| 561 |
raise ImportError('fail') |
| 562 |
def test_hello(): |
| 563 |
assert '0' == env['FAILTEST'] |
| 564 |
""" |
| 565 |
) |
| 566 |
|
| 567 |
pytester.makepyfile( |
| 568 |
test_maybe2=""" |
| 569 |
import os |
| 570 |
env = os.environ |
| 571 |
if '1' == env['FAILIMPORT']: |
| 572 |
raise ImportError('fail') |
| 573 |
|
| 574 |
def test_hello(): |
| 575 |
assert '0' == env['FAILTEST'] |
| 576 |
|
| 577 |
def test_pass(): |
| 578 |
pass |
| 579 |
""" |
| 580 |
) |
| 581 |
|
| 582 |
def rlf( |
| 583 |
fail_import: int, fail_run: int, args: Sequence[str] = () |
| 584 |
) -> tuple[Any, Any]: |
| 585 |
monkeypatch.setenv("FAILIMPORT", str(fail_import)) |
| 586 |
monkeypatch.setenv("FAILTEST", str(fail_run)) |
| 587 |
|
| 588 |
result = pytester.runpytest("-q", "--lf", *args) |
| 589 |
config = pytester.parseconfigure() |
| 590 |
assert config.cache is not None |
| 591 |
lastfailed = config.cache.get("cache/lastfailed", -1) |
| 592 |
return result, lastfailed |
| 593 |
|
| 594 |
result, lastfailed = rlf(fail_import=0, fail_run=0) |
| 595 |
assert lastfailed == -1 |
| 596 |
result.stdout.fnmatch_lines(["*3 passed*"]) |
| 597 |
|
| 598 |
result, lastfailed = rlf(fail_import=1, fail_run=0) |
| 599 |
assert sorted(list(lastfailed)) == ["test_maybe.py", "test_maybe2.py"] |
| 600 |
|
| 601 |
result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",)) |
| 602 |
assert list(lastfailed) == ["test_maybe.py"] |
| 603 |
|
| 604 |
# edge case of test selection - even if we remember failures |
| 605 |
# from other tests we still need to run all tests if no test |
| 606 |
# matches the failures |
| 607 |
result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",)) |
| 608 |
assert list(lastfailed) == ["test_maybe.py"] |
| 609 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 610 |
|
| 611 |
def test_lastfailed_creates_cache_when_needed(self, pytester: Pytester) -> None: |
| 612 |
# Issue #1342 |
| 613 |
pytester.makepyfile(test_empty="") |
| 614 |
pytester.runpytest("-q", "--lf") |
| 615 |
assert not os.path.exists(".pytest_cache/v/cache/lastfailed") |
| 616 |
|
| 617 |
pytester.makepyfile(test_successful="def test_success():\n assert True") |
| 618 |
pytester.runpytest("-q", "--lf") |
| 619 |
assert not os.path.exists(".pytest_cache/v/cache/lastfailed") |
| 620 |
|
| 621 |
pytester.makepyfile(test_errored="def test_error():\n assert False") |
| 622 |
pytester.runpytest("-q", "--lf") |
| 623 |
assert os.path.exists(".pytest_cache/v/cache/lastfailed") |
| 624 |
|
| 625 |
def test_xfail_not_considered_failure(self, pytester: Pytester) -> None: |
| 626 |
pytester.makepyfile( |
| 627 |
""" |
| 628 |
import pytest |
| 629 |
@pytest.mark.xfail |
| 630 |
def test(): assert 0 |
| 631 |
""" |
| 632 |
) |
| 633 |
result = pytester.runpytest() |
| 634 |
result.stdout.fnmatch_lines(["*1 xfailed*"]) |
| 635 |
assert self.get_cached_last_failed(pytester) == [] |
| 636 |
|
| 637 |
def test_xfail_strict_considered_failure(self, pytester: Pytester) -> None: |
| 638 |
pytester.makepyfile( |
| 639 |
""" |
| 640 |
import pytest |
| 641 |
@pytest.mark.xfail(strict=True) |
| 642 |
def test(): pass |
| 643 |
""" |
| 644 |
) |
| 645 |
result = pytester.runpytest() |
| 646 |
result.stdout.fnmatch_lines(["*1 failed*"]) |
| 647 |
assert self.get_cached_last_failed(pytester) == [ |
| 648 |
"test_xfail_strict_considered_failure.py::test" |
| 649 |
] |
| 650 |
|
| 651 |
@pytest.mark.parametrize("mark", ["mark.xfail", "mark.skip"]) |
| 652 |
def test_failed_changed_to_xfail_or_skip( |
| 653 |
self, pytester: Pytester, mark: str |
| 654 |
) -> None: |
| 655 |
pytester.makepyfile( |
| 656 |
""" |
| 657 |
import pytest |
| 658 |
def test(): assert 0 |
| 659 |
""" |
| 660 |
) |
| 661 |
result = pytester.runpytest() |
| 662 |
assert self.get_cached_last_failed(pytester) == [ |
| 663 |
"test_failed_changed_to_xfail_or_skip.py::test" |
| 664 |
] |
| 665 |
assert result.ret == 1 |
| 666 |
|
| 667 |
pytester.makepyfile( |
| 668 |
f""" |
| 669 |
import pytest |
| 670 |
@pytest.{mark} |
| 671 |
def test(): assert 0 |
| 672 |
""" |
| 673 |
) |
| 674 |
result = pytester.runpytest() |
| 675 |
assert result.ret == 0 |
| 676 |
assert self.get_cached_last_failed(pytester) == [] |
| 677 |
assert result.ret == 0 |
| 678 |
|
| 679 |
@pytest.mark.parametrize("quiet", [True, False]) |
| 680 |
@pytest.mark.parametrize("opt", ["--ff", "--lf"]) |
| 681 |
def test_lf_and_ff_prints_no_needless_message( |
| 682 |
self, quiet: bool, opt: str, pytester: Pytester |
| 683 |
) -> None: |
| 684 |
# Issue 3853 |
| 685 |
pytester.makepyfile("def test(): assert 0") |
| 686 |
args = [opt] |
| 687 |
if quiet: |
| 688 |
args.append("-q") |
| 689 |
result = pytester.runpytest(*args) |
| 690 |
result.stdout.no_fnmatch_line("*run all*") |
| 691 |
|
| 692 |
result = pytester.runpytest(*args) |
| 693 |
if quiet: |
| 694 |
result.stdout.no_fnmatch_line("*run all*") |
| 695 |
else: |
| 696 |
assert "rerun previous" in result.stdout.str() |
| 697 |
|
| 698 |
def get_cached_last_failed(self, pytester: Pytester) -> list[str]: |
| 699 |
config = pytester.parseconfigure() |
| 700 |
assert config.cache is not None |
| 701 |
return sorted(config.cache.get("cache/lastfailed", {})) |
| 702 |
|
| 703 |
def test_cache_cumulative(self, pytester: Pytester) -> None: |
| 704 |
"""Test workflow where user fixes errors gradually file by file using --lf.""" |
| 705 |
# 1. initial run |
| 706 |
test_bar = pytester.makepyfile( |
| 707 |
test_bar=""" |
| 708 |
def test_bar_1(): pass |
| 709 |
def test_bar_2(): assert 0 |
| 710 |
""" |
| 711 |
) |
| 712 |
test_foo = pytester.makepyfile( |
| 713 |
test_foo=""" |
| 714 |
def test_foo_3(): pass |
| 715 |
def test_foo_4(): assert 0 |
| 716 |
""" |
| 717 |
) |
| 718 |
pytester.runpytest() |
| 719 |
assert self.get_cached_last_failed(pytester) == [ |
| 720 |
"test_bar.py::test_bar_2", |
| 721 |
"test_foo.py::test_foo_4", |
| 722 |
] |
| 723 |
|
| 724 |
# 2. fix test_bar_2, run only test_bar.py |
| 725 |
pytester.makepyfile( |
| 726 |
test_bar=""" |
| 727 |
def test_bar_1(): pass |
| 728 |
def test_bar_2(): pass |
| 729 |
""" |
| 730 |
) |
| 731 |
result = pytester.runpytest(test_bar) |
| 732 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 733 |
# ensure cache does not forget that test_foo_4 failed once before |
| 734 |
assert self.get_cached_last_failed(pytester) == ["test_foo.py::test_foo_4"] |
| 735 |
|
| 736 |
result = pytester.runpytest("--last-failed") |
| 737 |
result.stdout.fnmatch_lines( |
| 738 |
[ |
| 739 |
"collected 1 item", |
| 740 |
"run-last-failure: rerun previous 1 failure (skipped 1 file)", |
| 741 |
"*= 1 failed in *", |
| 742 |
] |
| 743 |
) |
| 744 |
assert self.get_cached_last_failed(pytester) == ["test_foo.py::test_foo_4"] |
| 745 |
|
| 746 |
# 3. fix test_foo_4, run only test_foo.py |
| 747 |
test_foo = pytester.makepyfile( |
| 748 |
test_foo=""" |
| 749 |
def test_foo_3(): pass |
| 750 |
def test_foo_4(): pass |
| 751 |
""" |
| 752 |
) |
| 753 |
result = pytester.runpytest(test_foo, "--last-failed") |
| 754 |
result.stdout.fnmatch_lines( |
| 755 |
[ |
| 756 |
"collected 2 items / 1 deselected / 1 selected", |
| 757 |
"run-last-failure: rerun previous 1 failure", |
| 758 |
"*= 1 passed, 1 deselected in *", |
| 759 |
] |
| 760 |
) |
| 761 |
assert self.get_cached_last_failed(pytester) == [] |
| 762 |
|
| 763 |
result = pytester.runpytest("--last-failed") |
| 764 |
result.stdout.fnmatch_lines(["*4 passed*"]) |
| 765 |
assert self.get_cached_last_failed(pytester) == [] |
| 766 |
|
| 767 |
def test_lastfailed_no_failures_behavior_all_passed( |
| 768 |
self, pytester: Pytester |
| 769 |
) -> None: |
| 770 |
pytester.makepyfile( |
| 771 |
""" |
| 772 |
def test_1(): pass |
| 773 |
def test_2(): pass |
| 774 |
""" |
| 775 |
) |
| 776 |
result = pytester.runpytest() |
| 777 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 778 |
result = pytester.runpytest("--lf") |
| 779 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 780 |
result = pytester.runpytest("--lf", "--lfnf", "all") |
| 781 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 782 |
|
| 783 |
# Ensure the list passed to pytest_deselected is a copy, |
| 784 |
# and not a reference which is cleared right after. |
| 785 |
pytester.makeconftest( |
| 786 |
""" |
| 787 |
deselected = [] |
| 788 |
|
| 789 |
def pytest_deselected(items): |
| 790 |
global deselected |
| 791 |
deselected = items |
| 792 |
|
| 793 |
def pytest_sessionfinish(): |
| 794 |
print("\\ndeselected={}".format(len(deselected))) |
| 795 |
""" |
| 796 |
) |
| 797 |
|
| 798 |
result = pytester.runpytest("--lf", "--lfnf", "none") |
| 799 |
result.stdout.fnmatch_lines( |
| 800 |
[ |
| 801 |
"collected 2 items / 2 deselected / 0 selected", |
| 802 |
"run-last-failure: no previously failed tests, deselecting all items.", |
| 803 |
"deselected=2", |
| 804 |
"* 2 deselected in *", |
| 805 |
] |
| 806 |
) |
| 807 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 808 |
|
| 809 |
def test_lastfailed_no_failures_behavior_empty_cache( |
| 810 |
self, pytester: Pytester |
| 811 |
) -> None: |
| 812 |
pytester.makepyfile( |
| 813 |
""" |
| 814 |
def test_1(): pass |
| 815 |
def test_2(): assert 0 |
| 816 |
""" |
| 817 |
) |
| 818 |
result = pytester.runpytest("--lf", "--cache-clear") |
| 819 |
result.stdout.fnmatch_lines(["*1 failed*1 passed*"]) |
| 820 |
result = pytester.runpytest("--lf", "--cache-clear", "--lfnf", "all") |
| 821 |
result.stdout.fnmatch_lines(["*1 failed*1 passed*"]) |
| 822 |
result = pytester.runpytest("--lf", "--cache-clear", "--lfnf", "none") |
| 823 |
result.stdout.fnmatch_lines(["*2 desel*"]) |
| 824 |
|
| 825 |
def test_lastfailed_skip_collection(self, pytester: Pytester) -> None: |
| 826 |
""" |
| 827 |
Test --lf behavior regarding skipping collection of files that are not marked as |
| 828 |
failed in the cache (#5172). |
| 829 |
""" |
| 830 |
pytester.makepyfile( |
| 831 |
**{ |
| 832 |
"pkg1/test_1.py": """ |
| 833 |
import pytest |
| 834 |
|
| 835 |
@pytest.mark.parametrize('i', range(3)) |
| 836 |
def test_1(i): pass |
| 837 |
""", |
| 838 |
"pkg2/test_2.py": """ |
| 839 |
import pytest |
| 840 |
|
| 841 |
@pytest.mark.parametrize('i', range(5)) |
| 842 |
def test_1(i): |
| 843 |
assert i not in (1, 3) |
| 844 |
""", |
| 845 |
} |
| 846 |
) |
| 847 |
# first run: collects 8 items (test_1: 3, test_2: 5) |
| 848 |
result = pytester.runpytest() |
| 849 |
result.stdout.fnmatch_lines(["collected 8 items", "*2 failed*6 passed*"]) |
| 850 |
# second run: collects only 5 items from test_2, because all tests from test_1 have passed |
| 851 |
result = pytester.runpytest("--lf") |
| 852 |
result.stdout.fnmatch_lines( |
| 853 |
[ |
| 854 |
"collected 2 items", |
| 855 |
"run-last-failure: rerun previous 2 failures (skipped 1 file)", |
| 856 |
"*= 2 failed in *", |
| 857 |
] |
| 858 |
) |
| 859 |
|
| 860 |
# add another file and check if message is correct when skipping more than 1 file |
| 861 |
pytester.makepyfile( |
| 862 |
**{ |
| 863 |
"pkg1/test_3.py": """ |
| 864 |
def test_3(): pass |
| 865 |
""" |
| 866 |
} |
| 867 |
) |
| 868 |
result = pytester.runpytest("--lf") |
| 869 |
result.stdout.fnmatch_lines( |
| 870 |
[ |
| 871 |
"collected 2 items", |
| 872 |
"run-last-failure: rerun previous 2 failures (skipped 2 files)", |
| 873 |
"*= 2 failed in *", |
| 874 |
] |
| 875 |
) |
| 876 |
|
| 877 |
def test_lastfailed_skip_collection_with_nesting(self, pytester: Pytester) -> None: |
| 878 |
"""Check that file skipping works even when the file with failures is |
| 879 |
nested at a different level of the collection tree.""" |
| 880 |
pytester.makepyfile( |
| 881 |
**{ |
| 882 |
"test_1.py": """ |
| 883 |
def test_1(): pass |
| 884 |
""", |
| 885 |
"pkg/__init__.py": "", |
| 886 |
"pkg/test_2.py": """ |
| 887 |
def test_2(): assert False |
| 888 |
""", |
| 889 |
} |
| 890 |
) |
| 891 |
# first run |
| 892 |
result = pytester.runpytest() |
| 893 |
result.stdout.fnmatch_lines(["collected 2 items", "*1 failed*1 passed*"]) |
| 894 |
# second run - test_1.py is skipped. |
| 895 |
result = pytester.runpytest("--lf") |
| 896 |
result.stdout.fnmatch_lines( |
| 897 |
[ |
| 898 |
"collected 1 item", |
| 899 |
"run-last-failure: rerun previous 1 failure (skipped 1 file)", |
| 900 |
"*= 1 failed in *", |
| 901 |
] |
| 902 |
) |
| 903 |
|
| 904 |
def test_lastfailed_with_known_failures_not_being_selected( |
| 905 |
self, pytester: Pytester |
| 906 |
) -> None: |
| 907 |
pytester.makepyfile( |
| 908 |
**{ |
| 909 |
"pkg1/test_1.py": """def test_1(): assert 0""", |
| 910 |
"pkg1/test_2.py": """def test_2(): pass""", |
| 911 |
} |
| 912 |
) |
| 913 |
result = pytester.runpytest() |
| 914 |
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"]) |
| 915 |
|
| 916 |
Path("pkg1/test_1.py").unlink() |
| 917 |
result = pytester.runpytest("--lf") |
| 918 |
result.stdout.fnmatch_lines( |
| 919 |
[ |
| 920 |
"collected 1 item", |
| 921 |
"run-last-failure: 1 known failures not in selected tests", |
| 922 |
"* 1 passed in *", |
| 923 |
] |
| 924 |
) |
| 925 |
|
| 926 |
# Recreate file with known failure. |
| 927 |
pytester.makepyfile(**{"pkg1/test_1.py": """def test_1(): assert 0"""}) |
| 928 |
result = pytester.runpytest("--lf") |
| 929 |
result.stdout.fnmatch_lines( |
| 930 |
[ |
| 931 |
"collected 1 item", |
| 932 |
"run-last-failure: rerun previous 1 failure (skipped 1 file)", |
| 933 |
"* 1 failed in *", |
| 934 |
] |
| 935 |
) |
| 936 |
|
| 937 |
# Remove/rename test: collects the file again. |
| 938 |
pytester.makepyfile(**{"pkg1/test_1.py": """def test_renamed(): assert 0"""}) |
| 939 |
result = pytester.runpytest("--lf", "-rf") |
| 940 |
result.stdout.fnmatch_lines( |
| 941 |
[ |
| 942 |
"collected 2 items", |
| 943 |
"run-last-failure: 1 known failures not in selected tests", |
| 944 |
"pkg1/test_1.py F *", |
| 945 |
"pkg1/test_2.py . *", |
| 946 |
"FAILED pkg1/test_1.py::test_renamed - assert 0", |
| 947 |
"* 1 failed, 1 passed in *", |
| 948 |
] |
| 949 |
) |
| 950 |
|
| 951 |
result = pytester.runpytest("--lf", "--co") |
| 952 |
result.stdout.fnmatch_lines( |
| 953 |
[ |
| 954 |
"collected 1 item", |
| 955 |
"run-last-failure: rerun previous 1 failure (skipped 1 file)", |
| 956 |
"", |
| 957 |
"<Dir *>", |
| 958 |
" <Dir pkg1>", |
| 959 |
" <Module test_1.py>", |
| 960 |
" <Function test_renamed>", |
| 961 |
] |
| 962 |
) |
| 963 |
|
| 964 |
def test_lastfailed_args_with_deselected(self, pytester: Pytester) -> None: |
| 965 |
"""Test regression with --lf running into NoMatch error. |
| 966 |
|
| 967 |
This was caused by it not collecting (non-failed) nodes given as |
| 968 |
arguments. |
| 969 |
""" |
| 970 |
pytester.makepyfile( |
| 971 |
**{ |
| 972 |
"pkg1/test_1.py": """ |
| 973 |
def test_pass(): pass |
| 974 |
def test_fail(): assert 0 |
| 975 |
""", |
| 976 |
} |
| 977 |
) |
| 978 |
result = pytester.runpytest() |
| 979 |
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"]) |
| 980 |
assert result.ret == 1 |
| 981 |
|
| 982 |
result = pytester.runpytest("pkg1/test_1.py::test_pass", "--lf", "--co") |
| 983 |
assert result.ret == 0 |
| 984 |
result.stdout.fnmatch_lines( |
| 985 |
[ |
| 986 |
"*collected 1 item", |
| 987 |
"run-last-failure: 1 known failures not in selected tests", |
| 988 |
"", |
| 989 |
"<Dir *>", |
| 990 |
" <Dir pkg1>", |
| 991 |
" <Module test_1.py>", |
| 992 |
" <Function test_pass>", |
| 993 |
], |
| 994 |
consecutive=True, |
| 995 |
) |
| 996 |
|
| 997 |
result = pytester.runpytest( |
| 998 |
"pkg1/test_1.py::test_pass", "pkg1/test_1.py::test_fail", "--lf", "--co" |
| 999 |
) |
| 1000 |
assert result.ret == 0 |
| 1001 |
result.stdout.fnmatch_lines( |
| 1002 |
[ |
| 1003 |
"collected 2 items / 1 deselected / 1 selected", |
| 1004 |
"run-last-failure: rerun previous 1 failure", |
| 1005 |
"", |
| 1006 |
"<Dir *>", |
| 1007 |
" <Dir pkg1>", |
| 1008 |
" <Module test_1.py>", |
| 1009 |
" <Function test_fail>", |
| 1010 |
"*= 1/2 tests collected (1 deselected) in *", |
| 1011 |
], |
| 1012 |
) |
| 1013 |
|
| 1014 |
def test_lastfailed_with_class_items(self, pytester: Pytester) -> None: |
| 1015 |
"""Test regression with --lf deselecting whole classes.""" |
| 1016 |
pytester.makepyfile( |
| 1017 |
**{ |
| 1018 |
"pkg1/test_1.py": """ |
| 1019 |
class TestFoo: |
| 1020 |
def test_pass(self): pass |
| 1021 |
def test_fail(self): assert 0 |
| 1022 |
|
| 1023 |
def test_other(): assert 0 |
| 1024 |
""", |
| 1025 |
} |
| 1026 |
) |
| 1027 |
result = pytester.runpytest() |
| 1028 |
result.stdout.fnmatch_lines(["collected 3 items", "* 2 failed, 1 passed in *"]) |
| 1029 |
assert result.ret == 1 |
| 1030 |
|
| 1031 |
result = pytester.runpytest("--lf", "--co") |
| 1032 |
assert result.ret == 0 |
| 1033 |
result.stdout.fnmatch_lines( |
| 1034 |
[ |
| 1035 |
"collected 3 items / 1 deselected / 2 selected", |
| 1036 |
"run-last-failure: rerun previous 2 failures", |
| 1037 |
"", |
| 1038 |
"<Dir *>", |
| 1039 |
" <Dir pkg1>", |
| 1040 |
" <Module test_1.py>", |
| 1041 |
" <Class TestFoo>", |
| 1042 |
" <Function test_fail>", |
| 1043 |
" <Function test_other>", |
| 1044 |
"", |
| 1045 |
"*= 2/3 tests collected (1 deselected) in *", |
| 1046 |
], |
| 1047 |
consecutive=True, |
| 1048 |
) |
| 1049 |
|
| 1050 |
def test_lastfailed_with_all_filtered(self, pytester: Pytester) -> None: |
| 1051 |
pytester.makepyfile( |
| 1052 |
**{ |
| 1053 |
"pkg1/test_1.py": """ |
| 1054 |
def test_fail(): assert 0 |
| 1055 |
def test_pass(): pass |
| 1056 |
""", |
| 1057 |
} |
| 1058 |
) |
| 1059 |
result = pytester.runpytest() |
| 1060 |
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"]) |
| 1061 |
assert result.ret == 1 |
| 1062 |
|
| 1063 |
# Remove known failure. |
| 1064 |
pytester.makepyfile( |
| 1065 |
**{ |
| 1066 |
"pkg1/test_1.py": """ |
| 1067 |
def test_pass(): pass |
| 1068 |
""", |
| 1069 |
} |
| 1070 |
) |
| 1071 |
result = pytester.runpytest("--lf", "--co") |
| 1072 |
result.stdout.fnmatch_lines( |
| 1073 |
[ |
| 1074 |
"collected 1 item", |
| 1075 |
"run-last-failure: 1 known failures not in selected tests", |
| 1076 |
"", |
| 1077 |
"<Dir *>", |
| 1078 |
" <Dir pkg1>", |
| 1079 |
" <Module test_1.py>", |
| 1080 |
" <Function test_pass>", |
| 1081 |
"", |
| 1082 |
"*= 1 test collected in*", |
| 1083 |
], |
| 1084 |
consecutive=True, |
| 1085 |
) |
| 1086 |
assert result.ret == 0 |
| 1087 |
|
| 1088 |
def test_packages(self, pytester: Pytester) -> None: |
| 1089 |
"""Regression test for #7758. |
| 1090 |
|
| 1091 |
The particular issue here was that Package nodes were included in the |
| 1092 |
filtering, being themselves Modules for the __init__.py, even if they |
| 1093 |
had failed Modules in them. |
| 1094 |
|
| 1095 |
The tests includes a test in an __init__.py file just to make sure the |
| 1096 |
fix doesn't somehow regress that, it is not critical for the issue. |
| 1097 |
""" |
| 1098 |
pytester.makepyfile( |
| 1099 |
**{ |
| 1100 |
"__init__.py": "", |
| 1101 |
"a/__init__.py": "def test_a_init(): assert False", |
| 1102 |
"a/test_one.py": "def test_1(): assert False", |
| 1103 |
"b/__init__.py": "", |
| 1104 |
"b/test_two.py": "def test_2(): assert False", |
| 1105 |
}, |
| 1106 |
) |
| 1107 |
pytester.makeini( |
| 1108 |
""" |
| 1109 |
[pytest] |
| 1110 |
python_files = *.py |
| 1111 |
""" |
| 1112 |
) |
| 1113 |
result = pytester.runpytest() |
| 1114 |
result.assert_outcomes(failed=3) |
| 1115 |
result = pytester.runpytest("--lf") |
| 1116 |
result.assert_outcomes(failed=3) |
| 1117 |
|
| 1118 |
def test_non_python_file_skipped( |
| 1119 |
self, |
| 1120 |
pytester: Pytester, |
| 1121 |
dummy_yaml_custom_test: None, |
| 1122 |
) -> None: |
| 1123 |
pytester.makepyfile( |
| 1124 |
**{ |
| 1125 |
"test_bad.py": """def test_bad(): assert False""", |
| 1126 |
}, |
| 1127 |
) |
| 1128 |
result = pytester.runpytest() |
| 1129 |
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"]) |
| 1130 |
|
| 1131 |
result = pytester.runpytest("--lf") |
| 1132 |
result.stdout.fnmatch_lines( |
| 1133 |
[ |
| 1134 |
"collected 1 item", |
| 1135 |
"run-last-failure: rerun previous 1 failure (skipped 1 file)", |
| 1136 |
"* 1 failed in *", |
| 1137 |
] |
| 1138 |
) |
| 1139 |
|
| 1140 |
|
| 1141 |
class TestNewFirst: |
| 1142 |
def test_newfirst_usecase(self, pytester: Pytester) -> None: |
| 1143 |
pytester.makepyfile( |
| 1144 |
**{ |
| 1145 |
"test_1/test_1.py": """ |
| 1146 |
def test_1(): assert 1 |
| 1147 |
""", |
| 1148 |
"test_2/test_2.py": """ |
| 1149 |
def test_1(): assert 1 |
| 1150 |
""", |
| 1151 |
} |
| 1152 |
) |
| 1153 |
|
| 1154 |
p1 = pytester.path.joinpath("test_1/test_1.py") |
| 1155 |
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9))) |
| 1156 |
|
| 1157 |
result = pytester.runpytest("-v") |
| 1158 |
result.stdout.fnmatch_lines( |
| 1159 |
["*test_1/test_1.py::test_1 PASSED*", "*test_2/test_2.py::test_1 PASSED*"] |
| 1160 |
) |
| 1161 |
|
| 1162 |
result = pytester.runpytest("-v", "--nf") |
| 1163 |
result.stdout.fnmatch_lines( |
| 1164 |
["*test_2/test_2.py::test_1 PASSED*", "*test_1/test_1.py::test_1 PASSED*"] |
| 1165 |
) |
| 1166 |
|
| 1167 |
p1.write_text( |
| 1168 |
"def test_1(): assert 1\ndef test_2(): assert 1\n", encoding="utf-8" |
| 1169 |
) |
| 1170 |
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9))) |
| 1171 |
|
| 1172 |
result = pytester.runpytest("--nf", "--collect-only", "-q") |
| 1173 |
result.stdout.fnmatch_lines( |
| 1174 |
[ |
| 1175 |
"test_1/test_1.py::test_2", |
| 1176 |
"test_2/test_2.py::test_1", |
| 1177 |
"test_1/test_1.py::test_1", |
| 1178 |
] |
| 1179 |
) |
| 1180 |
|
| 1181 |
# Newest first with (plugin) pytest_collection_modifyitems hook. |
| 1182 |
pytester.makepyfile( |
| 1183 |
myplugin=""" |
| 1184 |
def pytest_collection_modifyitems(items): |
| 1185 |
items[:] = sorted(items, key=lambda item: item.nodeid) |
| 1186 |
print("new_items:", [x.nodeid for x in items]) |
| 1187 |
""" |
| 1188 |
) |
| 1189 |
pytester.syspathinsert() |
| 1190 |
result = pytester.runpytest("--nf", "-p", "myplugin", "--collect-only", "-q") |
| 1191 |
result.stdout.fnmatch_lines( |
| 1192 |
[ |
| 1193 |
"new_items: *test_1.py*test_1.py*test_2.py*", |
| 1194 |
"test_1/test_1.py::test_2", |
| 1195 |
"test_2/test_2.py::test_1", |
| 1196 |
"test_1/test_1.py::test_1", |
| 1197 |
] |
| 1198 |
) |
| 1199 |
|
| 1200 |
def test_newfirst_parametrize(self, pytester: Pytester) -> None: |
| 1201 |
pytester.makepyfile( |
| 1202 |
**{ |
| 1203 |
"test_1/test_1.py": """ |
| 1204 |
import pytest |
| 1205 |
@pytest.mark.parametrize('num', [1, 2]) |
| 1206 |
def test_1(num): assert num |
| 1207 |
""", |
| 1208 |
"test_2/test_2.py": """ |
| 1209 |
import pytest |
| 1210 |
@pytest.mark.parametrize('num', [1, 2]) |
| 1211 |
def test_1(num): assert num |
| 1212 |
""", |
| 1213 |
} |
| 1214 |
) |
| 1215 |
|
| 1216 |
p1 = pytester.path.joinpath("test_1/test_1.py") |
| 1217 |
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9))) |
| 1218 |
|
| 1219 |
result = pytester.runpytest("-v") |
| 1220 |
result.stdout.fnmatch_lines( |
| 1221 |
[ |
| 1222 |
"*test_1/test_1.py::test_1[1*", |
| 1223 |
"*test_1/test_1.py::test_1[2*", |
| 1224 |
"*test_2/test_2.py::test_1[1*", |
| 1225 |
"*test_2/test_2.py::test_1[2*", |
| 1226 |
] |
| 1227 |
) |
| 1228 |
|
| 1229 |
result = pytester.runpytest("-v", "--nf") |
| 1230 |
result.stdout.fnmatch_lines( |
| 1231 |
[ |
| 1232 |
"*test_2/test_2.py::test_1[1*", |
| 1233 |
"*test_2/test_2.py::test_1[2*", |
| 1234 |
"*test_1/test_1.py::test_1[1*", |
| 1235 |
"*test_1/test_1.py::test_1[2*", |
| 1236 |
] |
| 1237 |
) |
| 1238 |
|
| 1239 |
p1.write_text( |
| 1240 |
"import pytest\n" |
| 1241 |
"@pytest.mark.parametrize('num', [1, 2, 3])\n" |
| 1242 |
"def test_1(num): assert num\n", |
| 1243 |
encoding="utf-8", |
| 1244 |
) |
| 1245 |
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9))) |
| 1246 |
|
| 1247 |
# Running only a subset does not forget about existing ones. |
| 1248 |
result = pytester.runpytest("-v", "--nf", "test_2/test_2.py") |
| 1249 |
result.stdout.fnmatch_lines( |
| 1250 |
["*test_2/test_2.py::test_1[1*", "*test_2/test_2.py::test_1[2*"] |
| 1251 |
) |
| 1252 |
|
| 1253 |
result = pytester.runpytest("-v", "--nf") |
| 1254 |
result.stdout.fnmatch_lines( |
| 1255 |
[ |
| 1256 |
"*test_1/test_1.py::test_1[3*", |
| 1257 |
"*test_2/test_2.py::test_1[1*", |
| 1258 |
"*test_2/test_2.py::test_1[2*", |
| 1259 |
"*test_1/test_1.py::test_1[1*", |
| 1260 |
"*test_1/test_1.py::test_1[2*", |
| 1261 |
] |
| 1262 |
) |
| 1263 |
|
| 1264 |
|
| 1265 |
class TestReadme: |
| 1266 |
def check_readme(self, pytester: Pytester) -> bool: |
| 1267 |
config = pytester.parseconfigure() |
| 1268 |
assert config.cache is not None |
| 1269 |
readme = config.cache._cachedir.joinpath("README.md") |
| 1270 |
return readme.is_file() |
| 1271 |
|
| 1272 |
def test_readme_passed(self, pytester: Pytester) -> None: |
| 1273 |
pytester.makepyfile("def test_always_passes(): pass") |
| 1274 |
pytester.runpytest() |
| 1275 |
assert self.check_readme(pytester) is True |
| 1276 |
|
| 1277 |
def test_readme_failed(self, pytester: Pytester) -> None: |
| 1278 |
pytester.makepyfile("def test_always_fails(): assert 0") |
| 1279 |
pytester.runpytest() |
| 1280 |
assert self.check_readme(pytester) is True |
| 1281 |
|
| 1282 |
|
| 1283 |
class Action(Enum): |
| 1284 |
"""Action to perform on the cache directory.""" |
| 1285 |
|
| 1286 |
MKDIR = auto() |
| 1287 |
SET = auto() |
| 1288 |
|
| 1289 |
|
| 1290 |
@pytest.mark.parametrize("action", list(Action)) |
| 1291 |
def test_gitignore( |
| 1292 |
pytester: Pytester, |
| 1293 |
action: Action, |
| 1294 |
) -> None: |
| 1295 |
"""Ensure we automatically create .gitignore file in the pytest_cache directory (#3286).""" |
| 1296 |
from _pytest.cacheprovider import Cache |
| 1297 |
|
| 1298 |
config = pytester.parseconfig() |
| 1299 |
cache = Cache.for_config(config, _ispytest=True) |
| 1300 |
if action == Action.MKDIR: |
| 1301 |
cache.mkdir("foo") |
| 1302 |
elif action == Action.SET: |
| 1303 |
cache.set("foo", "bar") |
| 1304 |
else: |
| 1305 |
assert_never(action) |
| 1306 |
msg = "# Created by pytest automatically.\n*\n" |
| 1307 |
gitignore_path = cache._cachedir.joinpath(".gitignore") |
| 1308 |
assert gitignore_path.read_text(encoding="UTF-8") == msg |
| 1309 |
|
| 1310 |
# Does not overwrite existing/custom one. |
| 1311 |
gitignore_path.write_text("custom", encoding="utf-8") |
| 1312 |
if action == Action.MKDIR: |
| 1313 |
cache.mkdir("something") |
| 1314 |
elif action == Action.SET: |
| 1315 |
cache.set("something", "else") |
| 1316 |
else: |
| 1317 |
assert_never(action) |
| 1318 |
assert gitignore_path.read_text(encoding="UTF-8") == "custom" |
| 1319 |
|
| 1320 |
|
| 1321 |
def test_preserve_keys_order(pytester: Pytester) -> None: |
| 1322 |
"""Ensure keys order is preserved when saving dicts (#9205).""" |
| 1323 |
from _pytest.cacheprovider import Cache |
| 1324 |
|
| 1325 |
config = pytester.parseconfig() |
| 1326 |
cache = Cache.for_config(config, _ispytest=True) |
| 1327 |
cache.set("foo", {"z": 1, "b": 2, "a": 3, "d": 10}) |
| 1328 |
read_back = cache.get("foo", None) |
| 1329 |
assert list(read_back.items()) == [("z", 1), ("b", 2), ("a", 3), ("d", 10)] |
| 1330 |
|
| 1331 |
|
| 1332 |
def test_does_not_create_boilerplate_in_existing_dirs(pytester: Pytester) -> None: |
| 1333 |
from _pytest.cacheprovider import Cache |
| 1334 |
|
| 1335 |
pytester.makeini( |
| 1336 |
""" |
| 1337 |
[pytest] |
| 1338 |
cache_dir = . |
| 1339 |
""" |
| 1340 |
) |
| 1341 |
config = pytester.parseconfig() |
| 1342 |
cache = Cache.for_config(config, _ispytest=True) |
| 1343 |
cache.set("foo", "bar") |
| 1344 |
|
| 1345 |
assert os.path.isdir("v") # cache contents |
| 1346 |
assert not os.path.exists(".gitignore") |
| 1347 |
assert not os.path.exists("README.md") |
| 1348 |
|
| 1349 |
|
| 1350 |
def test_cachedir_tag(pytester: Pytester) -> None: |
| 1351 |
"""Ensure we automatically create CACHEDIR.TAG file in the pytest_cache directory (#4278).""" |
| 1352 |
from _pytest.cacheprovider import Cache |
| 1353 |
from _pytest.cacheprovider import CACHEDIR_FILES |
| 1354 |
|
| 1355 |
config = pytester.parseconfig() |
| 1356 |
cache = Cache.for_config(config, _ispytest=True) |
| 1357 |
cache.set("foo", "bar") |
| 1358 |
cachedir_tag_path = cache._cachedir.joinpath("CACHEDIR.TAG") |
| 1359 |
assert cachedir_tag_path.read_bytes() == CACHEDIR_FILES["CACHEDIR.TAG"] |
| 1360 |
|
| 1361 |
|
| 1362 |
def test_clioption_with_cacheshow_and_help(pytester: Pytester) -> None: |
| 1363 |
result = pytester.runpytest("--cache-show", "--help") |
| 1364 |
assert result.ret == 0 |
| 1365 |
|
| 1366 |
|
| 1367 |
def test_make_cachedir_cleans_up_on_base_exception( |
| 1368 |
tmp_path: Path, |
| 1369 |
monkeypatch: MonkeyPatch, |
| 1370 |
) -> None: |
| 1371 |
"""Ensure _make_cachedir cleans up the temp directory on BaseException. |
| 1372 |
|
| 1373 |
When a BaseException (like KeyboardInterrupt) is raised during cache |
| 1374 |
directory creation, the temporary directory should be cleaned up before |
| 1375 |
re-raising the exception. |
| 1376 |
""" |
| 1377 |
from _pytest.cacheprovider import _make_cachedir |
| 1378 |
|
| 1379 |
target = tmp_path / ".pytest_cache" |
| 1380 |
|
| 1381 |
def raise_keyboard_interrupt(self: Path, target: Path) -> None: |
| 1382 |
raise KeyboardInterrupt("simulated interrupt") |
| 1383 |
|
| 1384 |
# Patch Path.rename only for the duration of the _make_cachedir call |
| 1385 |
with monkeypatch.context() as m: |
| 1386 |
m.setattr(Path, "rename", raise_keyboard_interrupt) |
| 1387 |
|
| 1388 |
# Verify the exception is re-raised |
| 1389 |
with pytest.raises(KeyboardInterrupt, match="simulated interrupt"): |
| 1390 |
_make_cachedir(target) |
| 1391 |
|
| 1392 |
# Verify no temp directories were left behind |
| 1393 |
temp_dirs = list(tmp_path.glob("pytest-cache-files-*")) |
| 1394 |
assert temp_dirs == [], f"Temp directories not cleaned up: {temp_dirs}" |
| 1395 |
|
| 1396 |
# Verify the target directory was not created |
| 1397 |
assert not target.exists() |