【问题标题】:Django not running tests with manage.py testDjango 没有使用 manage.py test 运行测试
【发布时间】:2014-10-05 11:29:32
【问题描述】:

我对 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))

【问题讨论】:

  • 你的 Django 版本是多少?
  • 你可以试试不带斜杠 (/) 吗?您必须指定 Python 模块(以点 (.) 作为分隔符)而不是目录。

标签: python django unit-testing testing


【解决方案1】:

来自unittest documentation

测试用例是通过继承 unittest.TestCase 来创建的。这三个单独的测试是使用名称以字母test 开头的方法定义的。此命名约定告知测试运行程序哪些方法代表测试。

将您的测试函数更改为test_hard_no_more_than,然后测试运行者应该能够发现测试用例。

【讨论】:

    【解决方案2】:

    测试方法需要以“test”开头,以便测试运行器找到它们。所以你的方法应该叫做“test_hard_no_more_than”。

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 2015-02-02
      • 2019-02-19
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多