TP : Tester son application¶
date: | 2012-04-30 19:06 |
---|---|
tags: | django, python |
category: | Django |
author: | Rémy Hubscher |
Récapitulatif¶
Nous avons maintenant une application qui fonctionne.
Cependant nous allons sûrement la faire vivre et en modifiant quelque chose, on risque de créer des bugs.
Pour éviter cela, nous allons tester toutes nos views afin d’être sur qu’elle se comporte correctement.
Notre fichier de test¶
tests.py
Un squelette est créé automatiquement lors de la création de notre app.
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
Tester l’affichage de la liste¶
Pour commencer, nous allons créer une liste de tâche reprenant tous les cas de figures dans la méthode setUp
# -*- coding: utf-8 -*-
"""
Todo : Tests
"""
from django.test import TestCase
from django.core.urlresolvers import reverse
from todo.models import Task
class TodoTest(TestCase):
def setUp(self):
self.task1 = Task.objects.create(content=u'Ma première tâche', is_resolved=True)
task2 = Task.objects.create(content=u'Ma seconde tâche', is_resolved=False)
task3 = Task.objects.create(content=u'Ma troisième tâche', is_resolved=True)
task4 = Task.objects.create(content=u'Ma quatrième tâche', is_resolved=False)
Ensuite nous allons ajouter nos méthodes, une méthode par test
def test_task_list(self):
url = reverse('tasks-list')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context['object_list']), Task.objects.count())
Tester la suppression des taches terminées¶
def test_tasks_clear(self):
url = reverse('tasks-clear')
nb_tasks = Task.objects.count()
response = self.client.post(url)
self.assertEqual(nb_tasks-2, Task.objects.count())
Tester le changement de status de toutes les tâches¶
def test_tasks_toggle(self):
url = reverse('tasks-toggle')
nb_tasks = Task.objects.filter(is_resolved=True).count()
response = self.client.post(url)
self.assertEqual(0, Task.objects.filter(is_resolved=True).count())
response = self.client.post(url)
self.assertEqual(nb_tasks+2, Task.objects.filter(is_resolved=True).count())
Tester la création d’une tâche¶
def test_task_creation(self):
url = reverse('task-create')
nb_tasks = Task.objects.count()
response = self.client.post(url, {'content': u'Ma cinquième tâche'})
self.assertEqual(nb_tasks+1, Task.objects.count())
Tester le changement de statut d’une tâche¶
def test_task_toggle(self):
task_id = self.task1.id
url = reverse('task-toggle', args=[task_id])
status = Task.objects.get(pk=task_id).is_resolved
response = self.client.post(url)
self.assertEqual(status, not Task.objects.get(pk=task_id).is_resolved)
Tester la suppression d’une tâche¶
def test_task_delete(self):
task_id = self.task1.id
url = reverse('task-delete', args=[task_id])
nb_tasks = Task.objects.count()
response = self.client.post(url)
self.assertEqual(nb_tasks-1, Task.objects.count())
response = self.client.post(url)
assertEqual(response.status_code, 404)
Conclusion¶
Rien de bien compliqué, pour lancer nos tests
$ python manage.py test todo
Creating test database for alias 'default'...
......
----------------------------------------------------------------------
Ran 6 tests in 0.206s
OK
Destroying test database for alias 'default'...
Il faudra, au préalable, créer la base test_tuto_django_bdd et donner les droits à l’utilisateur tuto_django_user.