And*_*rii 5 django django-rest-framework python-unittest visual-studio-code
我有一个带有一些单元测试的 django 应用程序。我想在 VScode v.1.59.1 中运行\调试它们。
我的 ./vscode/settings.json 看起来像这样:
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./routes/tests",
"-p",
"test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true,
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行\发现测试时,我有以下输出:
ImportError: Failed to import test module: test_utils
...
django.core.exceptions.ImproperlyConfigured: Requested setting REST_FRAMEWORK, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Run Code Online (Sandbox Code Playgroud)
当我尝试使用命令以常规 django 方式运行测试时:
python manage.py test
Run Code Online (Sandbox Code Playgroud)
测试工作正常。
请帮助我设置 VScode 来运行 django 单元测试。
PS:我无法安装 pytest,这应该在不安装任何新模块的情况下完成。
我也为此苦苦挣扎。它还会导致调试面板根本无法发现任何测试。
与使用 ./manage.py test 运行时不同,vscode 在开始测试时尚未初始化您的应用程序。
所以; 我所做的是加载设置并执行 setup(),然后加载其余部分:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.testing")
from django import setup
setup()
from django.contrib.auth import get_user_model
<other imports>
<your tests>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
488 次 |
| 最近记录: |