Use py.test for testing

This commit is contained in:
Aarni Koskela 2023-12-12 13:05:41 +02:00
parent 9d674e4793
commit 30fad7b763
7 changed files with 74 additions and 95 deletions

View File

@ -13,7 +13,7 @@ We require features to be backed by a unit test.
This way, we can test *django-polymorphic* against new Django versions.
To run the included test suite, execute::
./runtests.py
py.test
To test support for multiple Python and Django versions, run tox from the repository root::

9
manage.py Normal file
View File

@ -0,0 +1,9 @@
# This helps pytest-django locate the project.
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "polymorphic_test_settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

View File

@ -1,4 +1,4 @@
from unittest import TestCase
from django.test import TestCase
from polymorphic.contrib.guardian import get_polymorphic_base_content_type
from polymorphic.tests.models import Model2D, PlainC

View File

@ -0,0 +1,56 @@
import dj_database_url
DEBUG = False
DATABASES = {
"default": dj_database_url.config(
env="PRIMARY_DATABASE",
default="sqlite://:memory:",
),
"secondary": dj_database_url.config(
env="SECONDARY_DATABASE",
default="sqlite://:memory:",
),
}
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
INSTALLED_APPS = (
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.messages",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.admin",
"polymorphic",
"polymorphic.tests",
)
MIDDLEWARE = (
"django.middleware.common.CommonMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
)
SITE_ID = 3
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": (),
"OPTIONS": {
"loaders": (
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
),
"context_processors": (
"django.template.context_processors.debug",
"django.template.context_processors.i18n",
"django.template.context_processors.media",
"django.template.context_processors.request",
"django.template.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.contrib.auth.context_processors.auth",
),
},
}
]
POLYMORPHIC_TEST_SWAPPABLE = "polymorphic.swappedmodel"
ROOT_URLCONF = None
SECRET_KEY = "supersecret"

View File

@ -33,3 +33,6 @@ select = [
"F841",
"I",
]
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "polymorphic_test_settings"

View File

@ -1,91 +0,0 @@
#!/usr/bin/env python -Wd
import sys
import warnings
from os.path import abspath, dirname
import dj_database_url
import django
from django.conf import settings
from django.core.management import execute_from_command_line
# python -Wd, or run via coverage:
warnings.simplefilter("always", DeprecationWarning)
# Give feedback on used versions
sys.stderr.write(f"Using Python version {sys.version[:5]} from {sys.executable}\n")
sys.stderr.write(
"Using Django version {} from {}\n".format(
django.get_version(), dirname(abspath(django.__file__))
)
)
if not settings.configured:
settings.configure(
DEBUG=False,
DATABASES={
"default": dj_database_url.config(env="PRIMARY_DATABASE", default="sqlite://:memory:"),
"secondary": dj_database_url.config(
env="SECONDARY_DATABASE", default="sqlite://:memory:"
),
},
DEFAULT_AUTO_FIELD="django.db.models.AutoField",
TEST_RUNNER="django.test.runner.DiscoverRunner",
INSTALLED_APPS=(
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.messages",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.admin",
"polymorphic",
"polymorphic.tests",
),
MIDDLEWARE=(
"django.middleware.common.CommonMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
),
SITE_ID=3,
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": (),
"OPTIONS": {
"loaders": (
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
),
"context_processors": (
"django.template.context_processors.debug",
"django.template.context_processors.i18n",
"django.template.context_processors.media",
"django.template.context_processors.request",
"django.template.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.contrib.auth.context_processors.auth",
),
},
}
],
POLYMORPHIC_TEST_SWAPPABLE="polymorphic.swappedmodel",
ROOT_URLCONF=None,
SECRET_KEY="supersecret",
)
DEFAULT_TEST_APPS = ["polymorphic"]
def runtests():
other_args = list(filter(lambda arg: arg.startswith("-"), sys.argv[1:]))
test_apps = (
list(filter(lambda arg: not arg.startswith("-"), sys.argv[1:])) or DEFAULT_TEST_APPS
)
argv = sys.argv[:1] + ["test", "--traceback"] + other_args + test_apps
execute_from_command_line(argv)
if __name__ == "__main__":
runtests()

View File

@ -12,7 +12,9 @@ setenv =
postgres: DEFAULT_DATABASE = postgres:///default
postgres: SECONDARY_DATABASE = postgres:///secondary
deps =
coverage
pytest
pytest-cov
pytest-django
dj-database-url
django32: Django ~= 3.2
django40: Django ~= 4.0
@ -22,7 +24,7 @@ deps =
djangomain: https://github.com/django/django/archive/main.tar.gz
postgres: psycopg2
commands =
coverage run --source polymorphic runtests.py
py.test --cov --cov-report=term-missing --cov-report=xml .
[testenv:docs]
deps =