Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: make it possible to run without setting PYTHONPATH=$(pwd) #1098

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 8 additions & 2 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 All @@ -15,7 +16,7 @@

pytest_plugins = "pytester"

REPOSITORY_ROOT = pathlib.Path(__file__).parent
REPOSITORY_ROOT = pathlib.Path(__file__).parent.parent


def pytest_configure(config: pytest.Config) -> None:
Expand Down Expand Up @@ -128,13 +129,18 @@ def django_pytester(

tpkg_path.joinpath("__init__.py").touch()

app_source = REPOSITORY_ROOT / "../pytest_django_test/app"
app_source = REPOSITORY_ROOT / "pytest_django_test/app"
test_app_path = tpkg_path / "app"

# Copy the test app to make it available in the new test run
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