如果导入了类,则PyCharm中的单元测试失败

rjzwgtxy  于 2023-03-18  发布在  PyCharm
关注(0)|答案(1)|浏览(139)

使用PyCharm,我们尝试为概念验证类PocFunctional创建一个单元测试文件tests/test_poc.py,并且我们已经将DJANGO_SETTINGS_MODULE=applicationproject.settings添加到“运行/调试配置”的“Python测试”部分。
然而,如果我们在下面的示例源代码中取消注解from app.poc import PocFunctional以导入测试中的类,单元测试将得到一个运行时错误django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.。尽管如果没有导入PocFunctional类,文件运行正常。
我们是PyCharm环境中Python单元测试的新手,因此我们非常感谢任何提示和建议。

技术详情:
部分单元测试源代码:

from unittest import TestCase

# from app.poc import PocFunctional

class TestPocEav(TestCase):
    def test_get_transaction(self):
        # self. Fail()
        return

    # ... other test methods

例外情况:

/data/app-py3/venv3.7/bin/python /var/lib/snapd/snap/pycharm-professional/316/plugins/python/helpers/pycharm/_jb_unittest_runner.py --target test_poc.TestPocFunctional 
Testing started at 5:03 PM ...
Launching unittests with arguments python -m unittest test_poc.TestPocFunctional in /data/app-py3/APPLICATION/tests

Traceback (most recent call last):
  File "/var/lib/snapd/snap/pycharm-professional/316/plugins/python/helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
    sys.exit(main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING))
  File "/usr/local/lib/python3.7/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
  File "/usr/local/lib/python3.7/unittest/main.py", line 147, in parseArgs
    self.createTests()
  File "/usr/local/lib/python3.7/unittest/main.py", line 159, in createTests
    self.module)
  File "/usr/local/lib/python3.7/unittest/loader.py", line 220, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/lib/python3.7/unittest/loader.py", line 220, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/data/app-py3/APPLICATION/tests/test_poc.py", line 3, in <module>
    from applicationapp.poc import PocFunctional
  File "/data/app-py3/APPLICATION/applicationapp/poc.py", line 2, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/contrib/contenttypes/models.py", line 133, in <module>
    class ContentType(models.Model):
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/data/app-py3/venv3.7/lib/python3.7/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Process finished with exit code 1

Empty suite
qojgxg4l

qojgxg4l1#

看起来你的Django应用程序没有完全加载。
在尝试导入PocFunctional类之前,应尝试使用django.setup()
例如:

import django
from unittest import TestCase

class TestPocFunctional(TestCase):
    @classmethod
    def setUpClass(cls):
        django.setup() 
        from app.poc import PocFunctional
        cls.poc = PocFunctional()

    def test_get_transaction(self):

test_get_transaction方法中执行测试事务,它应该工作

相关问题