-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathtest_initialization.py
More file actions
99 lines (85 loc) · 2.72 KB
/
test_initialization.py
File metadata and controls
99 lines (85 loc) · 2.72 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
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("flag", ["--help", "--version"])
def test_django_setup_for_help_and_version_with_model_import(
django_pytester: DjangoPytester,
flag: str,
) -> None:
"""Django should be set up before conftest files are loaded, even with
--help/--version, so that top-level model imports in conftest files
don't fail with AppRegistryNotReady.
Regression test for https://github.com/pytest-dev/pytest-django/issues/1152
"""
django_pytester.makeconftest(
"""
from tpkg.app.models import Item
"""
)
django_pytester.makepyfile(
"""
def test_placeholder():
pass
"""
)
args = [flag]
if flag == "--version":
# pytest requires -V passed twice to actually print version info
args.append(flag)
result = django_pytester.runpytest_subprocess(*args)
assert result.ret == 0
result.stderr.no_fnmatch_line("*AppRegistryNotReady*")
result.stderr.no_fnmatch_line("*could not load initial conftests*")