VSCode中Django测试调试未使用launch.json指定参数的问题
问题描述
我在VSCode中配置launch.json用于Django测试调试(默认unittest不会自动创建测试数据库),但通过测试面板调试时,并未使用launch.json中指定的参数。
配置情况
我的launch.json配置如下:
{ "name": "Django Testing", "type": "python", "request": "launch", "purpose": ["debug-test"], "program": "${workspaceFolder}\\manage.py", "console": "integratedTerminal", "args": [ "test", "api.tests" ], "django": true, "justMyCode": true }
我配置了"purpose": ["debug-test"],期望点击调试测试时终端执行manage.py test api.tests。
两种调试方式的差异
- 运行和调试工具栏启动:符合预期,终端执行命令包含
manage.py test api.tests:
C:\Users\61403\Desktop\liner> c: && cd c:\Users\61403\Desktop\liner && cmd /C "c:\Users\61403\Desktop\liner\venv\Scripts\python.exe c:\Users\61403\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy\adapter/../../debugpy/launcher 62791 -- C:\Users\61403\Desktop\liner\manage.py test api.tests "
- 命令面板执行
Test: Debug All Tests:不符合预期,VSCode未执行manage.py test api.tests,而是逐个运行测试用例:
C:\Users\61403\Desktop\liner> c: && cd c:\Users\61403\Desktop\liner && cmd /C "c:\Users\61403\Desktop\liner\venv\Scripts\python.exe c:\Users\61403\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy\adapter/../../debugpy/launcher 62817 -- c:\Users\61403\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\visualstudio_py_testlauncher.py --us=. --up=test*.py --uvInt=2 --result-port=62814 -tapi.tests.CompanyTestCase.test_creating_company_creates_queue -tapi.tests.QueueTestCase.test_join_queue_endpoint -tapi.tests.QueueTestCase.test_position_in_queue -tapi.tests.UserTestCase.test_vscode_test -tapi.tests.CompanyTestCase.test_creating_company_creates_queue -tapi.tests.QueueTestCase.test_join_queue_endpoint -tapi.tests.QueueTestCase.test_position_in_queue -tapi.tests.UserTestCase.test_vscode_test --testFile=c:\Users\61403\Desktop\liner\api\tests.py "
最小复现示例
- 创建全新Django项目
testing,添加应用aif - 编写
aif/tests.py:
from django.test import TestCase from django import setup import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings") setup() from aif.models import SomeModel class Test_My_Case(TestCase): def test_some_test(self): SomeModel.objects.create() assert len(SomeModel.objects.all()) == 1
- 编写
aif/models.py:
class SomeModel(models.Model): pass
- 执行
py manage.py makemigrations和py manage.py migrate - 配置VSCode测试器:
Configure Tests > unittest > test*.py - 添加
launch.json配置:
{ "name": "Django Tests", "type": "python", "request": "launch", "purpose": ["debug-test"], "program": "${workspaceFolder}\\manage.py", "args": [ "test", "aif.tests" ], "django": true, "justMyCode": true },
- 测试结果:
- 运行和调试工具栏启动:终端执行
manage.py test aif.tests - 测试面板调试:使用
visualstudio_py_testlauncher.py逐个运行测试
- 运行和调试工具栏启动:终端执行
解决方案
问题根源是VSCode测试探索器默认用unittest原生方式运行测试,而非调用Django的manage.py test命令(后者才会自动创建测试数据库)。解决步骤如下:
- 修改
.vscode/settings.json:
添加配置指定测试运行命令为Django的manage.py test:
{ "python.testing.unittestArgs": [ "-v", "--tb=short", "--no-header", "--no-summary", "-s", ".", "-p", "test*.py" ], "python.testing.pytestEnabled": false, "python.testing.unittestEnabled": true, // 关键配置:让测试探索器使用manage.py test运行测试 "python.testing.unittestCommand": "${workspaceFolder}/manage.py test" }
Windows环境路径改为${workspaceFolder}\\manage.py test。
- 重新加载窗口:
按Ctrl+Shift+P执行Reload Window,使配置生效。
设置完成后,测试探索器的Debug All Tests会调用manage.py test命令,自动创建测试数据库并按指定参数运行测试。
内容的提问来源于stack exchange,提问作者meg hidey
相关产品推荐
相关产品推荐

