django 单元测试


Author: yifei / Created: June 18, 2018, 7:39 a.m. / Modified: June 18, 2018, 7:49 a.m. / Edit

和普通的单元测试不同的是,django 单独提供了一个测试模块,所有的 TestCase 需要继承 django.test.TestCase。

简单的测试

from django.test import TestCase
from myapp.models import Animal


class AnimalTestCase(TestCase):
    def setUp(self):
        Animal.objects.create(name="lion", sound="roar")
        Animal.objects.create(name="cat", sound="meow")

    def test_animals_can_speak(self):
        """Animals that can speak are correctly identified"""
        lion = Animal.objects.get(name="lion")
        cat = Animal.objects.get(name="cat")
        self.assertEqual(lion.speak(), 'The lion says "roar"')
        self.assertEqual(cat.speak(), 'The cat says "meow"')

对于需要测试服务器的测试用例,可以使用 django.test.Client

from django.test import TestCase

class SimpleTest(TestCase):
    def test_details(self):
        response = self.client.get('/customer/details/')
        self.assertEqual(response.status_code, 200)

    def test_index(self):
        response = self.client.get('/customer/index/')
        self.assertEqual(response.status_code, 200)


有任何问题可以发邮件到 kongyifei (at) gmail.com 讨论