Django - 在TestCase

时间:2017-04-14 16:47:58

标签: python django

我正在打包一个我想重用的Django应用程序(希望可以被其他人使用),我想创建一个测试用例,当app在不同的上下文中使用时,它会模仿定义子类。 / p>

我发现this solution在Python测试用例中做了类似的事情,但是,我的Django测试用例出现以下错误。

RuntimeError: Model class tests.test_unit_health.Heart doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

下面类似于我设置的产生此错误的内容:

models.py

from django.db import models


class HealthTest(models.Model):
    # some attributes...
    # some methods...
    def get_score(self, **kwargs):
        score = self.score(**kwargs)
        # more logic

test_unit_health.py

from django.test import TestCase
from health.models import HealthTest


class HealthTestCase(TestCase):
    def setUp(self):
        class Heart(HealthTest):
            def score(self, heartrate):
                if heartrate > 90:
                    return 2
                else:
                    return 1

完整错误如下所示:

======================================================================
ERROR: tests.test_unit_health (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_unit_health
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
  File ".../test_unit_health.py", line 8, in <module>
    class Heart(HealthTest):
  File ".../env/lib/python2.7/site-packages/django/db/models/base.py", line 118, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class tests.test_unit_health.Heart doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

非常感谢任何有助于此工作的帮助。谢谢!