Skip to content

Commit 72080ad

Browse files
committed
tests: make it possible to run without setting PYTHONPATH=$(pwd)
This makes just `pytest` run successfully.
1 parent b17b7bc commit 72080ad

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

docs/contributing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ but please don't include them in your pull requests.
163163

164164
After this short initial setup you're ready to run tests::
165165

166-
$ COVERAGE_PROCESS_START=`pwd`/pyproject.toml COVERAGE_FILE=`pwd`/.coverage PYTHONPATH=`pwd` pytest --ds=pytest_django_test.settings_postgres
166+
$ COVERAGE_PROCESS_START=`pwd`/pyproject.toml COVERAGE_FILE=`pwd`/.coverage pytest --ds=pytest_django_test.settings_postgres
167167

168168
You should repeat the above step for sqlite and mysql before the next step.
169169
This step will create a lot of ``.coverage`` files with additional suffixes for

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ addopts = [
7272
# Show extra test summary info for everything.
7373
"-ra",
7474
]
75+
pythonpath = ["."]
7576
DJANGO_SETTINGS_MODULE = "pytest_django_test.settings_sqlite_file"
7677
testpaths = ["tests"]
7778
markers = ["tag1", "tag2", "tag3", "tag4", "tag5"]

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import copy
4+
import os
45
import pathlib
56
import shutil
67
from pathlib import Path
@@ -135,6 +136,11 @@ def django_pytester(
135136
shutil.copytree(str(app_source), str(test_app_path))
136137
tpkg_path.joinpath("the_settings.py").write_text(test_settings)
137138

139+
# For suprocess tests, pytest's `pythonpath` setting doesn't currently
140+
# work, only the envvar does.
141+
pythonpath = os.pathsep.join(filter(None, [str(REPOSITORY_ROOT), os.getenv("PYTHONPATH", "")]))
142+
monkeypatch.setenv("PYTHONPATH", pythonpath)
143+
138144
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.the_settings")
139145

140146
def create_test_module(test_code: str, filename: str = "test_the_test.py") -> Path:

tests/test_urls.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from django.urls import is_valid_path
44
from django.utils.encoding import force_str
55

6+
from .helpers import DjangoPytester
7+
68

79
@pytest.mark.urls("pytest_django_test.urls_overridden")
810
def test_urls() -> None:
@@ -16,19 +18,27 @@ def test_urls_client(client) -> None:
1618
assert force_str(response.content) == "Overridden urlconf works!"
1719

1820

19-
def test_urls_cache_is_cleared(pytester: pytest.Pytester) -> None:
20-
pytester.makepyfile(
21+
@pytest.mark.django_project(
22+
extra_settings="""
23+
ROOT_URLCONF = "empty"
24+
""",
25+
)
26+
def test_urls_cache_is_cleared(django_pytester: DjangoPytester) -> None:
27+
django_pytester.makepyfile(
28+
empty="""
29+
urlpatterns = []
30+
""",
2131
myurls="""
2232
from django.urls import path
2333
2434
def fake_view(request):
2535
pass
2636
2737
urlpatterns = [path('first', fake_view, name='first')]
28-
"""
38+
""",
2939
)
3040

31-
pytester.makepyfile(
41+
django_pytester.create_test_module(
3242
"""
3343
from django.urls import reverse, NoReverseMatch
3444
import pytest
@@ -37,42 +47,47 @@ def fake_view(request):
3747
def test_something():
3848
reverse('first')
3949
40-
4150
def test_something_else():
4251
with pytest.raises(NoReverseMatch):
4352
reverse('first')
44-
45-
"""
53+
""",
4654
)
4755

48-
result = pytester.runpytest_subprocess()
56+
result = django_pytester.runpytest_subprocess()
4957
assert result.ret == 0
5058

5159

52-
def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(pytester: pytest.Pytester) -> None:
53-
pytester.makepyfile(
60+
@pytest.mark.django_project(
61+
extra_settings="""
62+
ROOT_URLCONF = "empty"
63+
""",
64+
)
65+
def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(
66+
django_pytester: DjangoPytester,
67+
) -> None:
68+
django_pytester.makepyfile(
69+
empty="""
70+
urlpatterns = []
71+
""",
5472
myurls="""
5573
from django.urls import path
5674
5775
def fake_view(request):
5876
pass
5977
6078
urlpatterns = [path('first', fake_view, name='first')]
61-
"""
62-
)
63-
64-
pytester.makepyfile(
79+
""",
6580
myurls2="""
6681
from django.urls import path
6782
6883
def fake_view(request):
6984
pass
7085
7186
urlpatterns = [path('second', fake_view, name='second')]
72-
"""
87+
""",
7388
)
7489

75-
pytester.makepyfile(
90+
django_pytester.create_test_module(
7691
"""
7792
from django.urls import reverse, NoReverseMatch
7893
import pytest
@@ -87,8 +102,8 @@ def test_something_else():
87102
reverse('first')
88103
89104
reverse('second')
90-
"""
105+
""",
91106
)
92107

93-
result = pytester.runpytest_subprocess()
108+
result = django_pytester.runpytest_subprocess()
94109
assert result.ret == 0

tox.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ deps =
2929
xdist: pytest-xdist>=1.15
3030

3131
setenv =
32-
PYTHONPATH = {toxinidir}:{env:PYTHONPATH:}
33-
3432
mysql_innodb: DJANGO_SETTINGS_MODULE=pytest_django_test.settings_mysql_innodb
3533
mysql_myisam: DJANGO_SETTINGS_MODULE=pytest_django_test.settings_mysql_myisam
3634
postgres: DJANGO_SETTINGS_MODULE=pytest_django_test.settings_postgres

0 commit comments

Comments
 (0)