🍡 Django Tutorial - Unit Testing
Updated at 2018-11-23 18:01
Updated development dependencies:
echo -e "pytest\npytest-django" >> requirements-dev.in
pip-compile --output-file requirements-dev.txt requirements-dev.in
pip install -r requirements-dev.txt
Create setup.cfg
in repository root:
echo -e "[tool:pytest]\nDJANGO_SETTINGS_MODULE=djangor.settings" >> setup.cfg
Create polls/tests/test_dummy.py
:
import pytest
from django.contrib.auth import get_user_model
from polls.models.questions import Question
from polls.models.choices import Choice
User = get_user_model()
@pytest.mark.django_db
def test_dummy(client):
question = Question.objects.create(
author=User.objects.create(),
text="What's up?",
)
question.choices.create(text='The roof')
question.choices.create(text='The sky')
assert User.objects.count() == 1
assert Question.objects.count() == 1
assert Choice.objects.count() == 2
assert len(str(User.objects.first().pk)) == 36
assert len(str(Question.objects.first().pk)) == 36
assert len(str(Choice.objects.first().pk)) == 36
# TODO: parametrize by model
Run tests:
pytest
# if later your tests start to get slow...
pytest --reuse-db