Skip to content

Commit

Permalink
tests: make it possible to run without setting PYTHONPATH=$(pwd)
Browse files Browse the repository at this point in the history
This makes just `pytest` run successfully.
  • Loading branch information
bluetech committed Nov 11, 2023
1 parent b17b7bc commit 72080ad
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ but please don't include them in your pull requests.

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

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

You should repeat the above step for sqlite and mysql before the next step.
This step will create a lot of ``.coverage`` files with additional suffixes for
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ addopts = [
# Show extra test summary info for everything.
"-ra",
]
pythonpath = ["."]
DJANGO_SETTINGS_MODULE = "pytest_django_test.settings_sqlite_file"
testpaths = ["tests"]
markers = ["tag1", "tag2", "tag3", "tag4", "tag5"]
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import copy
import os
import pathlib
import shutil
from pathlib import Path
Expand Down Expand Up @@ -135,6 +136,11 @@ def django_pytester(
shutil.copytree(str(app_source), str(test_app_path))
tpkg_path.joinpath("the_settings.py").write_text(test_settings)

# For suprocess tests, pytest's `pythonpath` setting doesn't currently
# work, only the envvar does.
pythonpath = os.pathsep.join(filter(None, [str(REPOSITORY_ROOT), os.getenv("PYTHONPATH", "")]))
monkeypatch.setenv("PYTHONPATH", pythonpath)

monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.the_settings")

def create_test_module(test_code: str, filename: str = "test_the_test.py") -> Path:
Expand Down
51 changes: 33 additions & 18 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.urls import is_valid_path
from django.utils.encoding import force_str

from .helpers import DjangoPytester


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


def test_urls_cache_is_cleared(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
@pytest.mark.django_project(
extra_settings="""
ROOT_URLCONF = "empty"
""",
)
def test_urls_cache_is_cleared(django_pytester: DjangoPytester) -> None:
django_pytester.makepyfile(
empty="""
urlpatterns = []
""",
myurls="""
from django.urls import path
def fake_view(request):
pass
urlpatterns = [path('first', fake_view, name='first')]
"""
""",
)

pytester.makepyfile(
django_pytester.create_test_module(
"""
from django.urls import reverse, NoReverseMatch
import pytest
Expand All @@ -37,42 +47,47 @@ def fake_view(request):
def test_something():
reverse('first')
def test_something_else():
with pytest.raises(NoReverseMatch):
reverse('first')
"""
""",
)

result = pytester.runpytest_subprocess()
result = django_pytester.runpytest_subprocess()
assert result.ret == 0


def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
@pytest.mark.django_project(
extra_settings="""
ROOT_URLCONF = "empty"
""",
)
def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(
django_pytester: DjangoPytester,
) -> None:
django_pytester.makepyfile(
empty="""
urlpatterns = []
""",
myurls="""
from django.urls import path
def fake_view(request):
pass
urlpatterns = [path('first', fake_view, name='first')]
"""
)

pytester.makepyfile(
""",
myurls2="""
from django.urls import path
def fake_view(request):
pass
urlpatterns = [path('second', fake_view, name='second')]
"""
""",
)

pytester.makepyfile(
django_pytester.create_test_module(
"""
from django.urls import reverse, NoReverseMatch
import pytest
Expand All @@ -87,8 +102,8 @@ def test_something_else():
reverse('first')
reverse('second')
"""
""",
)

result = pytester.runpytest_subprocess()
result = django_pytester.runpytest_subprocess()
assert result.ret == 0
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ deps =
xdist: pytest-xdist>=1.15

setenv =
PYTHONPATH = {toxinidir}:{env:PYTHONPATH:}

mysql_innodb: DJANGO_SETTINGS_MODULE=pytest_django_test.settings_mysql_innodb
mysql_myisam: DJANGO_SETTINGS_MODULE=pytest_django_test.settings_mysql_myisam
postgres: DJANGO_SETTINGS_MODULE=pytest_django_test.settings_postgres
Expand Down

0 comments on commit 72080ad

Please sign in to comment.