为什么我的Django Unittest无法加载并显示“ ImportError:无法导入测试模块:测试”

时间:2018-12-09 02:23:05

标签: python django pycharm

我无法弄清楚自己在做什么。

我添加了这个简单的单元测试:

from django.test import TestCase
from blog.myapp.models import *


class AllTests(TestCase):

    def FooBarTest(self):
        foo1 = Foo.objects.create(name='foo1')
        bar1 = Bar.objects.create(name='bar1')
        FooBar.objects.create(foo=foo1,bar=bar1)
        foo_from_db = Foo.objects.filter(bar__id=bar1)
        self.assertEqual(foo1.id,foo_from_db.id)

这是我的项目布局:

.
├── blog
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
├── myapp
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   └── 0001_initial.py
│   ├── models.py
│   ├── serializers.py
│   ├── templates
│   │   ├── myapp
│   │   │   ├── index.html
│   │   │   └── post_detail.html
│   │   └── post_list.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py

我正在使用PyCharm,这是配置测试的方式:

PyCharm Edit Configuration Dialog Box

现在,当我运行测试时,我得到以下输出:

Testing started at 6:21 PM ...
C:\Users\plankton\PycharmProjects\blog\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2\helpers\pycharm\_jb_unittest_runner.py" --target tests.AllTests
Launching unittests with arguments python -m unittest tests.AllTests in C:\Users\plankton\PycharmProjects\blog\blog\myapp

Error
Traceback (most recent call last):
  File "C:\Users\plankton\AppData\Local\Programs\Python\Python37-32\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Users\plankton\AppData\Local\Programs\Python\Python37-32\lib\unittest\case.py", line 615, in run
    testMethod()
  File "C:\Users\plankton\AppData\Local\Programs\Python\Python37-32\lib\unittest\loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: tests
Traceback (most recent call last):
  File "C:\Users\plankton\AppData\Local\Programs\Python\Python37-32\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "C:\Users\plankton\PycharmProjects\blog\blog\myapp\tests.py", line 2, in <module>
    from blog.myapp.models import *
  File "C:\Users\plankton\PycharmProjects\blog\blog\myapp\models.py", line 4, in <module>
    class Author(models.Model):
  File "C:\Users\plankton\PycharmProjects\blog\venv\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\plankton\PycharmProjects\blog\venv\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\plankton\PycharmProjects\blog\venv\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Users\plankton\PycharmProjects\blog\venv\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:\Users\plankton\PycharmProjects\blog\venv\lib\site-packages\django\conf\__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\plankton\PycharmProjects\blog\venv\lib\site-packages\django\conf\__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\plankton\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'blog.settings'




Ran 1 test in 0.002s

FAILED (errors=1)

Process finished with exit code 1

在克劳斯D的建议下,我已经像这样更新了我的跑步配置……

enter image description here

…现在看起来像这样……

enter image description here

但是我得到同样的错误。

我可以在终端上进行测试:

(venv) C:\Users\plankton\PycharmProjects\blog\blog>python manage.py test myapp.tests.AllTests.FooBarTest
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Destroying test database for alias 'default'...

但是我不得不将myapp / tests.py中的import语句更改为:

from .models import *

1 个答案:

答案 0 :(得分:0)

问题原来是免费版本的PyCharm不支持​​Django单元测试。我在PyCharm中定义的测试只是一个python测试,而PyCharm运行以下代码:

python -m unittest blog.myapp.tests.AllTests 

这不是运行Django单元测试的方式。