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

如何重置VS Code的测试配置?

如何重置VS Code的测试配置?

别担心,我来帮你搞定VS Code测试配置重置的问题!你之前配置测试时选错了目录,导致Django项目里的pytest测试类和方法没能显示在测试面板里,下面给你两种靠谱的解决方法:

方法一:通过VS Code界面快速重置配置

这是最直观的方式,跟着步骤来就行:

  • 打开VS Code的测试面板——你可以点击侧边栏的测试图标(看起来像个烧杯的那个),或者用快捷键Ctrl+Shift+P(Windows/Linux)/Cmd+Shift+P(Mac)打开命令面板,输入"Testing: Focus on Tests View"跳转过去。
  • 在测试面板的顶部,找到那个齿轮图标(测试配置按钮),点击它之后,选择「Configure Tests」或者「Reset Test Configuration」(不同VS Code版本的文字可能略有区别)。
  • 接下来会重新进入配置向导:先选择测试框架(选pytest),然后指定测试文件所在的项目根目录(就是包含你的manage.py的那个目录),跟着向导完成配置就好。

完成后刷新测试面板,你写的这些测试类和方法应该就能正常显示了:

import pytest
from django.contrib.auth.models import User
from rest_framework import status

@pytest.mark.django_db
class TestCreateCollection:
    
    def test_if_user_annoymous_returns_401(self, api_client):
        response = api_client.post('/store/collections/', {'title':'a'})
        assert response.status_code == status.HTTP_401_UNAUTHORIZED # type: ignore

    def test_if_user_not_admin_returns_403(self, api_client):
        api_client.force_authenticate(user=User(is_staff=False))
        response = api_client.post('/store/collections/', {'title':'a'})
        assert response.status_code == status.HTTP_403_FORBIDDEN # type: ignore

    def test_if_data_is_invalid_returns_400(self, api_client):
        api_client.force_authenticate(user=User(is_staff=True))
        response = api_client.post('/store/collections/', {'title':''})
        assert response.status_code == status.HTTP_400_BAD_REQUEST # type: ignore
        assert response.data['title'] is not None # type: ignore

    def test_if_data_is_valid_returns_201(self, api_client):
        api_client.force_authenticate(user=User(is_staff=True))
        response = api_client.post('/store/collections/', {'title':'a'})
        assert response.status_code == status.HTTP_201_CREATED # type: ignore
        assert response.data['id'] > 0 # type: ignore

方法二:手动删除配置文件(适合界面操作无效的情况)

如果界面重置没起作用,你可以手动清除测试配置:

  • 打开项目目录下的.vscode文件夹(如果看不到这个文件夹,记得在VS Code里开启「显示隐藏文件」的选项)。
  • 找到里面的settings.json或者testing.json文件(测试相关的配置通常存在这里),删掉所有和测试相关的配置项——比如python.testing.pytestArgs、python.testing.pytestEnabled这类字段。
  • 保存文件后重启VS Code,再次打开测试面板时,它会自动提示你重新配置测试,这时候选对目录就行。

另外补充一句:你当前的settings.json里只有以下配置,说明之前的测试配置可能存在工作区的其他配置文件里,所以手动删除.vscode下的相关文件肯定能重置配置:

{
    "window.zoomLevel": 3,
    "workbench.iconTheme": "material-icon-theme",
}

备注:内容来源于stack exchange,提问作者Ravi Yadav

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 18:14:34