【问题标题】:How can I configure MSTest to fail the unit test run on any warnings?如何配置 MSTest 以使单元测试运行失败?
【发布时间】:2017-10-04 00:13:41
【问题描述】:

我们的单元测试使用“Visual Studio 测试”任务在 Visual Studio Online 构建中执行。开发人员偶尔会更改路径,导致根本无法执行任何测试。

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

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

如果出现警告,我有什么方法可以配置 MSTest 或 VSO 中的“Visual Studio 测试”任务失败?

【问题讨论】:

  • 是否可以添加一个PowerShell任务(在VS测试任务之后)来检测VS测试任务中是否有这样的警告,如果有,那么构建失败?
  • 当然。 Powershell 任务如何引用上一个任务的输出?
  • 我添加了一个使用 powershell 任务获取 vs 测试任务警告的答案。您可以在我的回答中找到详细信息。

标签: unit-testing azure-devops mstest


【解决方案1】:

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

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

2。然后通过Timeline获取Visual Studio Test任务构建信息:

GET https://account.visualstudio.com/DefaultCollection/{project}/_apis/build/builds/{buildID}/timeline?api-version=2.0

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

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

比如rest 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2013-09-14
    • 2023-04-04
    • 1970-01-01
    • 2010-09-26
    • 2011-07-14
    相关资源
    最近更新 更多