Код: Выделить всё
from pathlib import Path
import shutil
from pytest import CaptureFixture
import pytest
from .tests import testutils
FIXTURES_ROOT = Path(__file__).parent / "fixtures"
INBOX = Path(__file__).parent / "inbox"
CONVERTED = Path(__file__).parent / "converted"
class TestItem:
converted_dir: Path
def __init__(self, inbox_dir: Path):
self.inbox_dir = inbox_dir
self.converted_dir = CONVERTED / inbox_dir.name
def load_test_fixture(
name: str,
*,
exclusive: bool = False,
override_name: str | None = None,
match_filter: str | None = None,
cleanup_inbox: bool = False,
):
src = FIXTURES_ROOT / name
if not src.exists():
raise FileNotFoundError(
f"Fixture {name} not found. Does it exist in {FIXTURES_ROOT}?"
)
dst = INBOX / (override_name or name)
dst.mkdir(parents=True, exist_ok=True)
for f in src.glob("**/*"):
dst_f = dst / f.relative_to(src)
if f.is_file() and not dst_f.exists():
dst_f.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(f, dst_f)
# if any files in dst are not in src, delete them
for f in dst.glob("**/*"):
src_f = src / f.relative_to(dst)
if f.is_file() and not src_f.exists():
f.unlink()
if exclusive or match_filter is not None:
testutils.set_match_filter(match_filter or name)
converted_dir = CONVERTED / (override_name or name)
shutil.rmtree(converted_dir, ignore_errors=True)
return TestItem(dst)
Код: Выделить всё
@pytest.fixture(scope="function")
def basic_fixture():
return load_test_fixture("basic_fixture", exclusive=True)
def test_converted_dir_exists(
basic_fixture: TestItem, capfd: CaptureFixture[str]
):
assert basic_fixture.converted_dir.exists()
Код: Выделить всё
def load_test_fixtures(
*names: str,
exclusive: bool = False,
override_names: list[str] | None = None,
match_filter: str | None = None,
):
if exclusive:
match_filter = match_filter or rf"^({'|'.join(override_names or names)})"
return [
load_test_fixture(name, override_name=override, match_filter=match_filter)
for (name, override) in zip(names, override_names or names)
]
Код: Выделить всё
def load_test_fixture(
name: str,
*,
exclusive: bool = False,
override_name: str | None = None,
match_filter: str | None = None,
cleanup_inbox: bool = False,
):
# ...
# instead of returning, yield the item
yield TestItem(dst)
if cleanup_inbox:
shutil.rmtree(dst, ignore_errors=True)
@pytest.fixture(scope="function")
def basic_fixture():
yield from load_test_fixture("basic_fixture", exclusive=True)
Код: Выделить всё
def load_test_fixtures(
*names: str,
exclusive: bool = False,
override_names: list[str] | None = None,
match_filter: str | None = None,
):
if exclusive:
match_filter = match_filter or rf"^({'|'.join(override_names or names)})"
yield from (
load_test_fixture(name, match_filter=match_filter, override_name=override)
for name, override in zip(names, override_names or names)
)
# tried all combinations of list, tuple, and yield here, as well as
# yield (next(load_test_fixture(name, match_filter=match_filter, override_name=override))
# for name, override in zip(names, override_names or names)
Код: Выделить всё
fixtures = []
for f in [
"basic_fixture",
"fancy_fixture",
"tasty_fixture",
"smart_fixture",
]:
fixtures.extend(
load_test_fixture(f, match_filter=match_filter, cleanup_inbox=True)
)
yield fixtures
Подробнее здесь: https://stackoverflow.com/questions/783 ... t-fixtures