This repo demos basic TDD for a Django tasks app with CRUD.
The set-up and example tests are from https://www.youtube.com/watch?v=REhBTwubGzo
NB A new virtual environment is recommended.
-
Install the python packages
pip install -r requirements.txt
-
Run the tests in
task/tests.py
using the following command in the terminal:
pytest
to run with pytest, orpython manage.py test
-
Create a new project and install Django
pip install Django
-
Create the Django project
django-admin startproject tddtesting
cd tddtesting
-
Create the app
python manage.py startapp task
-
Create a test in
task/tests.py
from django.test import TestCase
from .models import Task
class TaskModelTest(TestCase):
def test_task_model_exists(self):
tasks = Task.objects.count()
self.assertEqual(tasks, 0)
-
Run tests, they will fail as the model hasn't been created yet
python manage.py test
-
Create the
test
model intask/models.py
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
def __str__(self):
return self.title
-
Make database migrations and migrate
python manage.py makemigrations
python manage.py migrate
-
Run the tests again, they should pass now
python manage.py test
Includes benefit of clearer output
pip install pytest-django
Also recommended for better output:
pip install pytest-sugar pytest-clarity
Create a pytest.ini file in the project root, replacing <app_name> with the name of the app, e.g. tddtesting.
# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = <app_name>.settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
Run tests with command:
pytest -vv
instead of python manage.py test
-vv
gives verbose output