python - 如何在 VSCode 中启动 django unittest?

标签 python django python-3.x visual-studio-code django-unittest

我正在尝试使用 VSCode 而不是终端运行 django unittest。

我的项目树是这样的:

├── db.sqlite3
├── hero
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── admin.cpython-37.pyc
│   │   ├── apps.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── tests.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_hero_age.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-37.pyc
│   │       ├── 0002_hero_age.cpython-37.pyc
│   │       └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
└── toh
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-37.pyc
    │   ├── settings.cpython-37.pyc
    │   ├── urls.cpython-37.pyc
    │   └── wsgi.cpython-37.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

我在 hero 目录下创建了 tests.py 文件。

我的 tests.py 代码如下所示:

from django.test import TestCase, Client
from .models import Hero
# Create your tests here.
class HeroTestCase(TestCase):
    def setUp(self):
        Hero.objects.create(name='Superman', age=10)
        Hero.objects.create(name='Batman', age=1)
        Hero.objects.create(name='Ironman', age=30)

    def test_hero_count(self):
        self.assertEqual(Hero.objects.all().count(), 3)
    def test_hero_id(self):
        client=Client()
        response=client.get('/hero/1/')

        self.assertEqual(response.status_code, 200)
        self.assertIn('1', response.content.decode())
    def test_hero_visit_count(self):
        client = Client()
        response = client.get('/hero/hello')
        self.assertEqual(response.status_code, 200)
        self.assertIn('1', response.content.decode())
        response = client.get('/hero/hello')
        self.assertEqual(response.status_code, 200)
        self.assertIn('2', response.content.decode())

我的 .vscode/settings.json 看起来像这样:

{
    "python.pythonPath": "/anaconda3/bin/python",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./hero",
        "-p",
        "*test*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}

但是当我通过 VSCode 运行测试时,这个错误不断出现。

======================================================================
ERROR: tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/anaconda3/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "toh/hero/tests.py", line 2, in <module>
    from .models import Hero
ImportError: attempted relative import with no known parent package

或者:

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

所以我检查了我的测试代码是否有问题,但是当我运行 python manage.py test 时,我的测试代码通过了!我该如何解决这个问题?

最佳答案

相对导入问题是因为您将 -p 设置为 hero ,这会将顶级目录更改为该目录,因此它不再像 Python 的包。

配置问题是因为 unittest 没有运行 manage.py。你可以去https://github.com/microsoft/vscode-python/issues/73和 👍 投票决定优先考虑的问题。

关于python - 如何在 VSCode 中启动 django unittest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424616/

相关文章:

python - 将行转置为列 python

python - 尝试将项目从列移动到行中,但不转置,也不从长到宽

Python 3——[s for s in subsets(S)] and yield

python - 检测相似图像

python - 如何在python中从文件中的字典中添加多个值

python - Django DisallowedHost 错误

python - 使用 Gunicorn 部署 Django 应用程序时出错?

excel - 将一个数据框与另一个数据框重叠并仅保留新的或更改的行

python - 在mongodb中保存numpy数组

python - Django , python :AttributeError: 'NoneType' object has no attribute '_meta'