You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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。

两种调试方式的差异

  1. 运行和调试工具栏启动:符合预期,终端执行命令包含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 "
  1. 命令面板执行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 "

最小复现示例

  1. 创建全新Django项目testing,添加应用aif
  2. 编写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
  1. 编写aif/models.py:
class SomeModel(models.Model):
    pass
  1. 执行py manage.py makemigrations和py manage.py migrate
  2. 配置VSCode测试器:Configure Tests > unittest > test*.py
  3. 添加launch.json配置:
{
    "name": "Django Tests",
    "type": "python",
    "request": "launch",
    "purpose": ["debug-test"],
    "program": "${workspaceFolder}\\manage.py",
    "args": [
        "test",
        "aif.tests"
    ],
    "django": true,
    "justMyCode": true
},
  1. 测试结果:
    • 运行和调试工具栏启动:终端执行manage.py test aif.tests
    • 测试面板调试:使用visualstudio_py_testlauncher.py逐个运行测试
解决方案

问题根源是VSCode测试探索器默认用unittest原生方式运行测试,而非调用Django的manage.py test命令(后者才会自动创建测试数据库)。解决步骤如下:

  1. 修改.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。

  1. 重新加载窗口:
    按Ctrl+Shift+P执行Reload Window,使配置生效。

设置完成后,测试探索器的Debug All Tests会调用manage.py test命令,自动创建测试数据库并按指定参数运行测试。

内容的提问来源于stack exchange,提问作者meg hidey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 17:48:18