Skip to content

Commit

Permalink
Run tests using pytest (#76)
Browse files Browse the repository at this point in the history
* Run tests using pytest

* Bump Postgres to version 12

Django 4.2 requires Postgres >= 12:
https://docs.djangoproject.com/en/4.2/ref/databases/#postgresql-notes

* Review comments
  • Loading branch information
mgax authored Apr 24, 2024
1 parent 4c3cd0b commit 2e10f46
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:

services:
postgres:
image: ${{ matrix.postgres || 'postgres:11' }}
image: ${{ matrix.postgres || 'postgres:12' }}
env:
POSTGRES_PASSWORD: postgres
ports:
Expand Down
10 changes: 8 additions & 2 deletions {{ cookiecutter.__project_name_kebab }}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ dependencies = [
[project.optional-dependencies]
testing = [
"dj-database-url==2.1.0",
"pre-commit==3.4.0"
"pre-commit==3.4.0",
"pytest==8.1.1",
"pytest-cov==5.0.0",
"pytest-django==4.8.0",
]
ci = [
"tox==4.11.3",
"tox-gh-actions==3.1.3"
"tox-gh-actions==3.1.3",
]

[project.urls]
Expand All @@ -61,3 +64,6 @@ exclude = [
"*.ini",
"*.yml"
]

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "{{ cookiecutter.__project_name_snake }}.test.settings"
10 changes: 10 additions & 0 deletions {{ cookiecutter.__project_name_kebab }}/ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,15 @@ lines-after-imports = 2
lines-between-types = 1


[lint.per-file-ignores]
"tests/**/*.py" = [
"S101", # asserts allowed in tests
"ARG", # unused function args (pytest fixtures)
"FBT", # booleans as positional arguments (@pytest.mark.parametrize)
"PLR2004", # magic value used in comparison
"S311", # standard pseudo-random generators are not suitable for cryptographic purposes
]


[format]
docstring-code-format = true
59 changes: 4 additions & 55 deletions {{ cookiecutter.__project_name_kebab }}/testmanage.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,15 @@
#!/usr/bin/env python

import argparse
import os
import shutil
import sys
import warnings

from django.core.management import execute_from_command_line


os.environ["DJANGO_SETTINGS_MODULE"] = "{{ cookiecutter.__project_name_snake }}.test.settings"


def make_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--deprecation",
choices=["all", "pending", "imminent", "none"],
default="imminent",
)
return parser


def parse_args(args=None):
return make_parser().parse_known_args(args)


def runtests():
args, rest = parse_args()

only_wagtail = r"^wagtail(\.|$)"
if args.deprecation == "all":
# Show all deprecation warnings from all packages
warnings.simplefilter("default", DeprecationWarning)
warnings.simplefilter("default", PendingDeprecationWarning)
elif args.deprecation == "pending":
# Show all deprecation warnings from wagtail
warnings.filterwarnings(
"default", category=DeprecationWarning, module=only_wagtail
)
warnings.filterwarnings(
"default", category=PendingDeprecationWarning, module=only_wagtail
)
elif args.deprecation == "imminent":
# Show only imminent deprecation warnings from wagtail
warnings.filterwarnings(
"default", category=DeprecationWarning, module=only_wagtail
)
elif args.deprecation == "none":
# Deprecation warnings are ignored by default
pass

argv = [sys.argv[0]] + rest

try:
execute_from_command_line(argv)
finally:
from wagtail.test.settings import MEDIA_ROOT, STATIC_ROOT

shutil.rmtree(STATIC_ROOT, ignore_errors=True)
shutil.rmtree(MEDIA_ROOT, ignore_errors=True)
def main():
os.environ["DJANGO_SETTINGS_MODULE"] = "{{ cookiecutter.__project_name_snake }}.test.settings"
execute_from_command_line(sys.argv)


if __name__ == "__main__":
runtests()
main()
6 changes: 6 additions & 0 deletions {{ cookiecutter.__project_name_kebab }}/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture(autouse=True)
def temporary_media_dir(settings, tmp_path: pytest.TempdirFactory):
settings.MEDIA_ROOT = tmp_path / "media"
14 changes: 14 additions & 0 deletions {{ cookiecutter.__project_name_kebab }}/tests/test_placeholder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Placeholder test, so that pytest doesn't fail with an empty testsuite. Feel free to
remove this when you start writing tests.
https://github.com/pytest-dev/pytest/issues/2393
"""

import pytest


pytestmark = pytest.mark.django_db


def test_homepage(client):
assert client.get("/").status_code == 200
2 changes: 1 addition & 1 deletion {{ cookiecutter.__project_name_kebab }}/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DB =

[testenv]
install_command = pip install -e ".[testing]" -U {opts} {packages}
commands = coverage run testmanage.py test --deprecation all {posargs: -v 2}
commands = pytest --cov {posargs: -vv}

basepython =
python3.8: python3.8
Expand Down

0 comments on commit 2e10f46

Please sign in to comment.