Python Django不';不要运行所有的单元测试
我在tangowithdjango.com上学习关于单元测试的django教程 我的代码如下所示:Python Django不';不要运行所有的单元测试,python,django,unit-testing,Python,Django,Unit Testing,我在tangowithdjango.com上学习关于单元测试的django教程 我的代码如下所示: from django.test import TestCase from rango.models import Category class CategoryMethodTests(TestCase): def test_ensure_views_are_positive(self): """ ensure_views_are_positive
from django.test import TestCase
from rango.models import Category
class CategoryMethodTests(TestCase):
def test_ensure_views_are_positive(self):
"""
ensure_views_are_positive
should results True for
categories where views
are zero or positive
"""
cat = Category(name='test',views=-1, likes=0)
cat.save()
self.assertEqual((cat.views >= 0), True)
def test_slug_line_creation(self):
"""
slug_line_creation checks
to make sure that when we
add a category an appropriate
slug line is created
i.e. "Random Category String"
->
"random-category-string"
"""
cat = Category(name='Random Category String')
cat.save()
self.assertEqual(cat.slug, 'random-category-string')
当我跑的时候
python manage.py test rango
它工作得很好,但它说运行1个测试,而实际上我有两个
另外,当我在第二个测试中编写失败的测试时,它不会抛出错误。我在这里遗漏了什么?如果第一次测试出错,它会失败吗?另外,这是您的实际代码还是教程中的代码?测试文件的名称是什么?默认情况下,Django将在当前工作目录下名为“test*.py”的任何文件中发现测试。我认为类名CategoryMethodTests必须以要发现的测试开头。不知道为什么你会得到一个测试运行否则。如果没有更好的主意,请尝试重命名它。@firelynx,类名不必以“test”开头。但是,类方法会尝试将self.assertTrue(False)放在一个中,将self.assertFalse(True)放在另一个中,以查看哪个没有运行