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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
import pytest
from .helpers import DjangoPytester
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found(django_pytester: DjangoPytester) -> None:
# XXX: Important: Do not chdir() to django_project_root since runpytest_subprocess
# will call "python /path/to/pytest.py", which will implicitly add cwd to
# the path. By instead calling "python /path/to/pytest.py
# django_project_root", we avoid implicitly adding the project to sys.path
# This matches the behaviour when pytest is called directly as an
# executable (cwd is not added to the Python path)
django_pytester.create_test_module(
"""
def test_foobar():
assert 1 + 1 == 2
"""
)
result = django_pytester.runpytest_subprocess("django_project_root")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_with_k(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test that cwd is checked as fallback with non-args via '-k foo'."""
testfile = django_pytester.create_test_module(
"""
def test_foobar():
assert True
""",
"sub/test_in_sub.py",
)
monkeypatch.chdir(testfile.parent)
result = django_pytester.runpytest_subprocess("-k", "test_foobar")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_with_k_and_cwd(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Cover cwd not used as fallback if present already in args."""
testfile = django_pytester.create_test_module(
"""
def test_foobar():
assert True
""",
"sub/test_in_sub.py",
)
monkeypatch.chdir(testfile.parent)
result = django_pytester.runpytest_subprocess(testfile.parent, "-k", "test_foobar")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_absolute(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""This only tests that "." is added as an absolute path (#637)."""
django_pytester.create_test_module(
"""
def test_dot_not_in_syspath():
import sys
assert '.' not in sys.path[:5]
"""
)
monkeypatch.chdir("django_project_root")
# NOTE: the "." here is important to test for an absolute path being used.
result = django_pytester.runpytest_subprocess("-s", ".")
assert result.ret == 0
outcomes = result.parseoutcomes()
assert outcomes["passed"] == 1
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_invalid_settings(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST")
result = django_pytester.runpytest_subprocess("django_project_root")
assert result.ret != 0
result.stderr.fnmatch_lines(["*ImportError:*DOES_NOT_EXIST*"])
result.stderr.fnmatch_lines(["*pytest-django found a Django project*"])
def test_django_project_scan_disabled_invalid_settings(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST")
django_pytester.makeini(
"""
[pytest]
django_find_project = false
"""
)
result = django_pytester.runpytest_subprocess("django_project_root")
assert result.ret != 0
result.stderr.fnmatch_lines(["*ImportError*DOES_NOT_EXIST*"])
result.stderr.fnmatch_lines(["*pytest-django did not search for Django projects*"])
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_found_invalid_settings_version(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Invalid DSM should not cause an error with --help or --version."""
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST")
result = django_pytester.runpytest_subprocess("django_project_root", "--version", "--version")
assert result.ret == 0
result.stdout.fnmatch_lines(["*This is pytest version*"])
result = django_pytester.runpytest_subprocess("django_project_root", "--help")
assert result.ret == 0
result.stdout.fnmatch_lines(["*usage:*"])
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_late_settings_version(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Late configuration should not cause an error with --help or --version."""
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
django_pytester.makepyfile(
t="WAT = 1",
)
django_pytester.makeconftest(
"""
import os
def pytest_configure():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 't')
from django.conf import settings
settings.WAT
"""
)
result = django_pytester.runpytest_subprocess("django_project_root", "--version", "--version")
assert result.ret == 0
result.stdout.fnmatch_lines(["*This is pytest version*"])
result = django_pytester.runpytest_subprocess("django_project_root", "--help")
assert result.ret == 0
result.stdout.fnmatch_lines(["*usage:*"])
@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_runs_without_error_on_long_args(django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
def test_this_is_a_long_message_which_caused_a_bug_when_scanning_for_manage_py_12346712341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234112341234112451234123412341234123412341234123412341234123412341234123412341234123412341234123412341234():
assert 1 + 1 == 2
"""
)
result = django_pytester.runpytest_subprocess(
"-k",
"this_is_a_long_message_which_caused_a_bug_when_scanning_for_manage_py_12346712341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234112341234112451234123412341234123412341234123412341234123412341234123412341234123412341234123412341234",
"django_project_root",
)
assert result.ret == 0
|