詹戈。测试用例未运行

2024-06-06 20:15:10 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么我在终端中运行python manage.py test appname是:在0.000s内运行0个测试

这是我的测试.py公司名称:

from django.test import TestCase
import appname.factories

class UserProfileTest(TestCase):
    def sample_data(self):
        for i in range(0, 10):
            user = appname.factories.UserProfileFactory.create()

我的模型.py公司名称:

^{pr2}$

我的工厂.py(工厂男孩):

from appname.models import *
import factory


class UserProfileFactory(factory.Factory):
    FACTORY_FOR = UserProfile

    street = factory.Sequence(lambda n: 'Street' + n)
    tel = factory.Sequence(lambda n: 'Tel' + n)
    password = 'abcdef'

Tags: lambdafrompytestimport名称factory工厂
2条回答

你的个人测试功能应该以单词“test”开头。在

您需要将函数def sample_data(self):更改为def test_sample_data(self):

测试运行程序将在名为tests.py的文件中查找任何类,这些类位于应用程序的根目录中,并扩展unittest.TestCase。然后它将运行该类中以单词test开头的任何函数(加上一个或两个其他函数,如setup()

我可能有点迟钝,但我在main django testing docs中看不到任何关于函数必须以单词test开头的内容。无论如何,在this(官方)教程中有一个对该需求的引用。在

test.py错误,应该是tests.py

docs about writing tests

For a given Django application, the test runner looks for unit tests in two places:

  • The models.py file. The test runner looks for any subclass of unittest.TestCase in this module.
  • A file called tests.py in the application directory – i.e., the directory that holds models.py. Again, the test runner looks for any subclass of unittest.TestCase in this module.

相关问题 更多 >