如何配置 MSTest 以在出现任何警告时使单元测试 运行 失败?

How can I configure MSTest to fail the unit test run on any warnings?

我们的单元测试使用 "Visual Studio Test" 任务在 Visual Studio 在线构建中执行。有时开发人员会更改路径,这会导致根本无法执行任何测试。

我们在 Visual Studio 联机日志中收到警告,但构建仍然成功,因此很难发现。

2017-10-02T16:33:15.7470931Z ##[warning]No test assemblies found matching the pattern: **\DBX.Tests*.dll;-:**\obj\**.

有什么方法可以将 MSTest 或 VSO 中的 "Visual Studio Test" 任务配置为在出现警告时失败?

当visual studio测试任务出现此类警告时,您可以使用其他方法使构建结果失败。详细步骤如下:

1.在 Visual Studio 测试任务后添加一个 PowerShell 任务。

2。然后通过Timeline:

获取Visual Studio测试任务构建信息
GET https://account.visualstudio.com/DefaultCollection/{project}/_apis/build/builds/{buildID}/timeline?api-version=2.0

对于buildID,您可以通过预定义变量$(Build.BuildId)获取。

3。按任务名称或任务显示名称搜索 Visual Studio 测试任务信息。

如其余api的部分回复如下:

{
  "id": "59863cf3-dc46-4e1b-914a-6e903e1aa924",
  "parentId": "95930a52-e560-42d6-b9d2-14e0eef98758",
  "type": "Task",
  "name": "VsTest - testAssemblies",
  "startTime": "2017-10-03T15:54:36.35Z",
  "finishTime": "2017-10-03T15:54:37.897Z",
  "currentOperation": null,
  "percentComplete": null,
  "state": "completed",
  "result": "succeeded",
  "resultCode": null,
  "changeId": 16,
  "lastModified": "0001-01-01T00:00:00",
  "workerName": "Hosted Agent",
  "order": 5,
  "details": null,
  "errorCount": 0,
  "warningCount": 2,
  "url": null,
  "log": {
    "id": 6,
    "type": "Container",
    "url": "https://maaccount.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/1583/logs/6"
  },
  "task": {
    "id": "ef087383-ee5e-42c7-9a53-ab56c98420f9",
    "name": "VSTest",
    "version": "2.1.12"
  },
  "issues": [
    {
      "type": "warning",
      "category": "General",
      "message": "",
      "data": {
        "type": "warning",
        "code": "002004"
      }
    },
    {
      "type": "warning",
      "category": "General",
      "message": "No test assemblies found matching the pattern: **\*test*.dll,!**\obj\**.",
      "data": {
        "type": "warning"
      }
    }
  ]

}

您可以按任务名称 VSTest 或任务显示名称 VsTest - testAssemblies 进行搜索。

对于 issues 对象,您可以获得所需的警告。

4.如果 VS 测试有您指定的警告,则构建结果失败。

如果有您指定的警告(例如警告No test assemblies found matching the pattern: **\DBX.Tests*.dll;-:**\obj\**),您可以通过

使PowerShell任务失败
exit 1