如何定义测试任务

How to define test task

我正在使用 VS Code 开发一个简单的项目。我已经创建了一些单元测试 (xUnit.net),我想创建一个测试任务来执行它们。我的想法是在我点击 Ctrl+Shift+T 时进行 运行 测试。

但是,我不知道如何定义测试任务。实现该目标的正确方法是什么?

除了命名要执行测试的任务外,您还可以将 isTestCommand 属性 设置为 true。像

{
   ...
   tasks: [{
      "taskName": "myTestTask",
      "isTestCommand": true,
      ...
   }]
}

这也会将 myTestTask 绑定到 Ctrl+Shift+T

看起来他们更改了 Ctrl+Shift+T 键绑定的默认行为在最近的版本中重新打开最后关闭的选项卡(像许多浏览器支持)。要查看您当前的键盘绑定,select 以下菜单选项:

File > Preferences > Keyboard Shortcuts

如果你想将 Ctrl+Shift+T 键绑定改回发布您的默认测试任务,只需更改以下对象中 command 属性 的值:

{ "key": "ctrl+shift+t", "command": "workbench.action.reopenClosedEditor" }

为:workbench.action.tasks.test,或者您可以通过将以下行添加到默认键盘快捷键配置文件来将测试任务分配给不同的键绑定:

{ "key": "<your keybinding here>", "command": "workbench.action.tasks.test" }

看到这个link它是不言自明的

https://code.visualstudio.com/Docs/editor/tasks

在 .vscode

中创建一个 tasks.json 文件
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "MY-COMMAND-FOR-RUNNING-TEST",
    "isShellCommand": true,
    "showOutput": "always"
}

如果你配置了npm test

    {
        "taskName": "build",
        "command": "npm",
        "args": ["test"],
        "isShellCommand": true
    }

如果您已使用 测试任务

配置 gulp
    {
        "taskName": "build",
        "command": "gulp",
        "args": ["test"],
        "isShellCommand": true
    }

这在最新版本的 VS Code (1.47.0) 中已更改

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Run tests",
      "group": "test", // <-- this will add this task to 'Run test task'
      "type": "shell",
      "command": "npm test"
    }
  ]
}