Skip to content

Commit

Permalink
Use pytest-django and add smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
SupraSummus committed Aug 30, 2024
1 parent 5505320 commit 46262b6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
18 changes: 18 additions & 0 deletions 30-django.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
poetry add 'django=*'
poetry add --group dev 'pytest-django==*'

# Check if the DJANGO_PROJECT_NAME environment variable is set
if [[ -z "$DJANGO_PROJECT_NAME" ]]; then
Expand All @@ -12,6 +13,23 @@ fi
# Run django-admin startproject with the project name
poetry run django-admin startproject "$DJANGO_PROJECT_NAME" .

# create pytest-django configuration
sed -i "/\[tool\.pytest\.ini_options\]/a\\
DJANGO_SETTINGS_MODULE = \"$DJANGO_PROJECT_NAME.settings\"\\
FAIL_INVALID_TEMPLATE_VARS = true" pyproject.toml

# create smoke test
cat > "test_no_smoke.py" <<EOL
import pytest
from django.urls import reverse
@pytest.mark.django_db
def test_admin_root(admin_client):
response = admin_client.get(reverse('admin:index'))
assert response.status_code == 200
EOL

poetry run isort .
git add --all
git commit -m "Initialize Django project"
7 changes: 4 additions & 3 deletions example/commits.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
example/urls.py | 23 ++++++++++
example/wsgi.py | 17 +++++++
manage.py | 22 ++++++++++
poetry.lock | 62 +++++++++++++++++++++++++-
pyproject.toml | 1 +
8 files changed, 265 insertions(+), 1 deletion(-)
poetry.lock | 80 ++++++++++++++++++++++++++++++++-
pyproject.toml | 4 ++
test_no_smoke.py | 8 ++++
9 files changed, 294 insertions(+), 1 deletion(-)

--- Configure Django settings with django-environ

Expand Down
20 changes: 19 additions & 1 deletion example/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions example/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ flake8 = "*"
flake8-pyproject = "*"
flake8-isort = "*"
pytest = "*"
pytest-django = "*"

[build-system]
requires = ["poetry-core"]
Expand All @@ -42,4 +43,6 @@ multi_line_output = 5
include_trailing_comma = true

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "example.settings"
FAIL_INVALID_TEMPLATE_VARS = true
python_files = ["tests.py", "test_*.py", "*_tests.py"]
8 changes: 8 additions & 0 deletions example/test_no_smoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest
from django.urls import reverse


@pytest.mark.django_db
def test_admin_root(admin_client):
response = admin_client.get(reverse('admin:index'))
assert response.status_code == 200
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ success=$?
poetry run flake8
cp example.env .env
poetry run python manage.py collectstatic --no-input
poetry run pytest || true # allow it to fail
poetry run pytest

# remove the virtual env, it may be stored outside the temporary directory
poetry env remove --all
Expand Down

0 comments on commit 46262b6

Please sign in to comment.