首页 > 解决方案 > 在 VSCode 测试资源管理器中运行 Django 测试?

问题描述

我正在尝试在 VSCode 测试资源管理器中运行 Django 单元测试,此外,我希望 CodeLens 的“运行测试”按钮出现在每个测试上方。 在此处输入图像描述 但是,在测试资源管理器中,当我按下“播放”按钮时,会显示错误:“没有运行测试”没有运行测试

我的目录结构是:

我正在使用单元测试框架。我的 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 的替代品,如何轻松设置它作为替代品?

标签: djangounit-testingvisual-studio-codevirtualenvpipenv

解决方案


请考虑以下检查:

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

推荐阅读