Skip to content

Commit 46262b6

Browse files
committed
Use pytest-django and add smoke test
1 parent 5505320 commit 46262b6

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

30-django.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
poetry add 'django=*'
2+
poetry add --group dev 'pytest-django==*'
23

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

16+
# create pytest-django configuration
17+
sed -i "/\[tool\.pytest\.ini_options\]/a\\
18+
DJANGO_SETTINGS_MODULE = \"$DJANGO_PROJECT_NAME.settings\"\\
19+
FAIL_INVALID_TEMPLATE_VARS = true" pyproject.toml
20+
21+
# create smoke test
22+
cat > "test_no_smoke.py" <<EOL
23+
import pytest
24+
from django.urls import reverse
25+
26+
27+
@pytest.mark.django_db
28+
def test_admin_root(admin_client):
29+
response = admin_client.get(reverse('admin:index'))
30+
assert response.status_code == 200
31+
EOL
32+
1533
poetry run isort .
1634
git add --all
1735
git commit -m "Initialize Django project"

example/commits.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
example/urls.py | 23 ++++++++++
3636
example/wsgi.py | 17 +++++++
3737
manage.py | 22 ++++++++++
38-
poetry.lock | 62 +++++++++++++++++++++++++-
39-
pyproject.toml | 1 +
40-
8 files changed, 265 insertions(+), 1 deletion(-)
38+
poetry.lock | 80 ++++++++++++++++++++++++++++++++-
39+
pyproject.toml | 4 ++
40+
test_no_smoke.py | 8 ++++
41+
9 files changed, 294 insertions(+), 1 deletion(-)
4142

4243
--- Configure Django settings with django-environ
4344

example/poetry.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ flake8 = "*"
2020
flake8-pyproject = "*"
2121
flake8-isort = "*"
2222
pytest = "*"
23+
pytest-django = "*"
2324

2425
[build-system]
2526
requires = ["poetry-core"]
@@ -42,4 +43,6 @@ multi_line_output = 5
4243
include_trailing_comma = true
4344

4445
[tool.pytest.ini_options]
46+
DJANGO_SETTINGS_MODULE = "example.settings"
47+
FAIL_INVALID_TEMPLATE_VARS = true
4548
python_files = ["tests.py", "test_*.py", "*_tests.py"]

example/test_no_smoke.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
from django.urls import reverse
3+
4+
5+
@pytest.mark.django_db
6+
def test_admin_root(admin_client):
7+
response = admin_client.get(reverse('admin:index'))
8+
assert response.status_code == 200

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ success=$?
2020
poetry run flake8
2121
cp example.env .env
2222
poetry run python manage.py collectstatic --no-input
23-
poetry run pytest || true # allow it to fail
23+
poetry run pytest
2424

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

0 commit comments

Comments
 (0)