Skip to main content
 首页 » 编程设计

python之Django 。测试用例未运行

2025年05月04日33telwanggs

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

这是我的tests.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:

from django.db import models 
 
class UserProfile(models.Model): 
    street = models.CharField(max_length=250) 
    tel = models.CharField(max_length=64, default='', blank=True) 
    postcode = models.CharField(max_length=250) 
 
    def __unicode__(self): 
        return self.tel 

我的工厂.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' 

请您参考如下方法:

您的个人测试功能应以“测试”一词开头。

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

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

我可能有点迟钝,但我在 main django testing docs 中看不到任何东西说明函数必须以单词 test 开头。无论如何,this 中有对要求的引用(官方)教程。