-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathtest_initialization.py
More file actions
101 lines (86 loc) · 3.15 KB
/
Copy pathtest_initialization.py
File metadata and controls
101 lines (86 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from textwrap import dedent
import pytest
from .helpers import DjangoPytester
def test_django_setup_order_and_uniqueness(django_pytester: DjangoPytester) -> None:
"""
The django.setup() function shall not be called multiple times by
pytest-django, since it resets logging conf each time.
"""
django_pytester.makeconftest(
"""
import django.apps
assert django.apps.apps.ready
from tpkg.app.models import Item
print("conftest")
def pytest_configure():
import django
print("pytest_configure: conftest")
django.setup = lambda: SHOULD_NOT_GET_CALLED
"""
)
django_pytester.project_root.joinpath("tpkg", "plugin.py").write_text(
dedent(
"""
import pytest
import django.apps
assert not django.apps.apps.ready
print("plugin")
def pytest_configure():
assert django.apps.apps.ready
from tpkg.app.models import Item
print("pytest_configure: plugin")
@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
print("pytest_load_initial_conftests")
assert not django.apps.apps.ready
"""
)
)
django_pytester.makepyfile(
"""
def test_ds():
pass
"""
)
result = django_pytester.runpytest_subprocess("-s", "-p", "tpkg.plugin")
result.stdout.fnmatch_lines(
[
"plugin",
"pytest_load_initial_conftests",
"conftest",
"pytest_configure: conftest",
"pytest_configure: plugin",
"* 1 passed*",
]
)
assert result.ret == 0
@pytest.mark.parametrize("option", ["--help", "--version"])
def test_django_setup_with_help_and_version(
django_pytester: DjangoPytester,
option: str,
) -> None:
"""Django must be set up before conftest files are imported, even for
``--help``/``--version``, so a top-level Django model import in a
``conftest.py`` does not fail with ``AppRegistryNotReady``.
Regression test for https://github.com/pytest-dev/pytest-django/issues/1152
"""
django_pytester.makeconftest(
"""
import sys
from tpkg.app.models import Item # noqa: F401
# Only reached if the model import above succeeds (i.e. Django is set
# up). With --version, pytest records but never prints the conftest
# load failure, so we assert on this positive marker instead.
print("conftest-imported-models", file=sys.stderr)
"""
)
# A single `--version`/`-V` is short-circuited before pytest loads any
# conftests (pytest #13574), so it wouldn't exercise this path; passing
# it twice forces full startup, which imports the initial conftests.
args = [option, option] if option == "--version" else [option]
result = django_pytester.runpytest_subprocess(*args)
output = result.stdout.str() + result.stderr.str()
assert result.ret == 0
assert "conftest-imported-models" in output
assert "AppRegistryNotReady" not in output
assert "could not load initial conftests" not in output