Django 单元测试没有找到任何测试

时间:2021-04-30 11:07:10

标签: django unit-testing django-tests django-3.2

我想对 test_models.py 运行单元测试,文件内容如下:

from django.test import TestCase
from django.contrib.auth import get_user_model


class ModelTest(TestCase):
    def test_create_user_with_email_successful(self):
        email = 'superuser@super.com'
        password = '9876543210'
        user = get_user_model().objects.create_user(
            email=email,
            password=password
        )

        self.assertEqual(user.email, email)
        self.assertTrue(user.check_password(password))

在我运行 python manage.py test 后,我得到了这个:

System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

我项目的文件结构如图 the file structure of my project

1 个答案:

答案 0 :(得分:1)

好的。我发现错误。文件夹 tests.py 不包含任何 __init__.py 文件。这就是 django 找不到测试文件的原因。所以它不会运行测试。

最后在 test 文件夹中添加 __init__.py 解决了问题