单个模块测试:python manage.py test 加项目名.模块名.文件名 不加.py
Django的中的单元测试使用蟒蛇的单元测试模块,这个模块使用基于类的方法来定义测试。类名为django.test.TestCase,继承于蟒蛇的unittest.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"')
执行目录下所有的测试(所有的test * .py文件):运行测试的时候,测试程序会在所有以测试开头的文件中查找所有的测试用例(inittest.TestCase的子类),自动建立测试集然后运行测试。
1 $ python manage.py测试
执行动物项目下的测试包里的测试:
1 $ python manage.py testanimals.tests
执行动物项目里的试验测试:
1 $ python manage.py testanimals
单独执行某个测试用例:
1 $ python manage.py testanimals.tests.AnimalTestCase
单独执行某个测试方法:
1 $ python manage.py testanimals.tests.AnimalTestCase.test_animals_can_speak
为测试文件提供路径:
1 $ python manage.py testanimals /
通配测试文件名:
1 $ python manage.py test-pattern =“tests _ * .py”
启用警告提醒:
1 $ python -Wall manage.py测试