django - 在 VSCode 测试资源管理器中运行 Django 测试?
问题描述
我正在尝试在 VSCode 测试资源管理器中运行 Django 单元测试,此外,我希望 CodeLens 的“运行测试”按钮出现在每个测试上方。 在此处输入图像描述 但是,在测试资源管理器中,当我按下“播放”按钮时,会显示错误:“没有运行测试”没有运行测试
我的目录结构是:
- 工作区_文件夹
- 设置.json
- 回购
- python_module_1
- 子模块
- 测试
- test_a.py
- 测试
- 子模块
- python_module_1
我正在使用单元测试框架。我的 Settings.json 看起来像这样:
{
"python.pythonPath": "/Users/nbonilla/.local/share/virtualenvs/koku-iTLe243o/bin/python",
"python.testing.unittestArgs": [
"-v",
"-s",
"${workspaceFolder}/python_module_1/sub_module/"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true,
}
当我按下绿色的“播放”按钮时,测试资源管理器播放按钮 Python 测试日志输出显示消息“ 由 我启动的线程中的未处理异常”我正在使用 pipenv 虚拟环境。如何在 VSCode 测试资源管理器中运行这些 Django 测试?
我看到使用 pyTest 是 unittest 的替代品,如何轻松设置它作为替代品?
解决方案
请考虑以下检查:
1- you should have __init__.py in your test directory
2- in vscode on test configuration use pytest framework
3- use: pip install pytest-django
4- copy pytest.ini in the root with this content:
# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = <your-web-project-name>.settings (like mysite.settings)
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
现在它应该可以按您的意愿工作了。你可以看到这个stackoverflow链接:
https://stackoverflow.com/questions/55837922/vscode-pytest-test-discovery-fails
推荐阅读
- html - 使用 v-for 生成表行,但我想为行 'id' 属性添加一个值
- javascript - 无法读取 Javascript 中未定义的属性“toString”
- python-3.x - 在 Windows 10 上运行 Python 代码后控制台立即关闭
- date - 如何在 Elixir 中获得按日期排序的目录列表?
- git - 如何通过 Git notifyCommit 钩子注入环境变量?
- python - 如何获取满足条件的多个列索引
- c++ - 我的程序不断抛出编译错误,我无法弄清楚为什么会发生错误
- java - 我们可以在 Java 8 中仅根据月份和年份对数据进行分组吗
- java - 为什么在这个例子中使用 Bean - 它看起来像无意义的间接?
- javascript - 当我使用 React JS 按下编辑按钮时,是否可以将响应数据设置为状态?