如何在VSCode中启动Django单元测试?

3

我想在VSCode中运行Django单元测试,而不是在终端中运行。

我的项目目录结构如下:

├── 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时,我的测试代码通过了!那么我该如何解决这个问题呢?

1个回答

4

相对导入问题是因为您将 -p 设置为 hero,这会将顶层目录更改为 hero,所以 Python 不再将其视为一个包。

配置问题是因为 unittest 没有运行 manage.py。您可以前往 https://github.com/microsoft/vscode-python/issues/73 并投票支持该问题优先处理。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接