python - Django 。测试用例未运行

标签 python django

为什么如果我在我的终端中运行 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 中有对要求的引用(官方)教程。

关于python - Django 。测试用例未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14853176/

相关文章:

python - Django - 关于外键的 AutoField

Python、Raspberry pi,每10毫秒精确调用一次任务

python - 如何完全改变 tkinter.ttk Treeview 的背景颜色

python - 导入错误 : No module named 'multiprocessing.forking'

python - 从 Django 查询集中获取特定属性的更好方法?

python - 使用 Django-storages 删除 Amazon S3 中的文件

python - Django 管理列表过滤器删除所有选项

python - 我无法在 TreeView 中插入分隔符或笔记本

python - 使用全局,但在Python函数中仍然无法识别

python - 使用 Python 的 3 个矩阵的 Kronecker 乘积