我对Django的测试有疑问。我启动此命令以运行测试:
$ python manage.py test gastrobook
gastrobook是我的应用程序,对于Django的文档可能会很好,但实际上它可以正常运行但是没有运行任何测试但是在tests.py中我写了一些测试!这是终端说:
$ python manage.py test gastrobook/
Creating test database for alias 'default'...
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
这是我的 tests.py
from django.test import TestCase from gastrobook.models import * import datetime from django.utils import timezone # Create your tests here. def create_recipe(title,desc,procedimento,pub_date,hard,time,person): return Recipe.objects.create(title=title,desc=desc,procedimento=procedimento,pub_date=pub_date,hard=hard,time=time,person=person) class RecipeTest(TestCase): def setUp(self): create_recipe("cui","dcnjenj","owcjc",timezone.now(),2,"5 ore",2) def hard_no_more_than(self): recipe = Recipe.objects.all() for rec in recipe: self.assertIn(rec.hard,range(1,10))
答案 0 :(得分:3)
通过继承unittest.TestCase创建测试用例。使用名称以字母
test
开头的方法定义三个单独的测试。此命名约定通知测试运行器哪些方法代表测试。
将测试功能更改为test_hard_no_more_than
,然后测试运行员就能够发现测试用例。
答案 1 :(得分:1)
测试方法需要以" test"开头命名。让测试运动员找到它们。因此,您的方法应该被称为" test_hard_no_more_than"。