【问题标题】:Require build to Contain Unit tests VSTS要求构建包含单元测试 VSTS
【发布时间】:2018-05-24 08:41:26
【问题描述】:

我有 1 个问题

我们在公司中使用 VSTS。现在我想要求我的开发人员为每个项目编写单元测试

我的问题是:如果我可以在 Team Service Web Portal 中构建以要求包含单元测试,并且如果项目没有任何单元测试 - 设置构建失败?

谢谢。

【问题讨论】:

  • 每个项目是否只包含单元测试而不包含其他测试?您可以通过检查 VS 测试任务中的警告来检查每个项目是否包含单元测试吗?例如如果 VS Test 任务显示[warning]No test assemblies found matching the pattern: '**\*test*.dll;-:**\obj\**',则构建失败。或者您想使用什么其他标准来检查项目是否包含单元测试?
  • 是的 - [警告]找不到与模式匹配的测试程序集:'**test.dll;-:**\obj**',然后构建失败。对我来说足够了。
  • @MarinaLiu-MSFT 我该怎么做?
  • 这是人的问题,不是技术问题。如果开发人员添加一个测试项目,其中包含一个只执行Assert.IsTrue(true) 的测试怎么办?如果他们在不编写新测试的情况下添加重要的新功能怎么办?如果他们编写了不能验证正确性的糟糕测试怎么办?如果您的开发人员没有在开发过程中注入质量,那么您的要求将无济于事。如果您的开发人员支持,则您的要求是不必要的。
  • @DanielMann - 没错。我们已经和我们的开发者谈过了)

标签: azure-devops azure-pipelines


【解决方案1】:

您可以添加 PowerShell 任务 来检查构建项目是否包含单元测试。详细步骤如下:

在VS测试任务后添加PowerShell任务,通过timeline REST API获取VS测试构建日志。对于 TFS,其余 api 格式应为:

GET http://{tsserver}:8080/tfs/{collection}/{project}/_apis/build/builds/{buildId}/timeline?api-version=3.0

然后在日志中搜索VS测试任务,您可以通过url获取详细的VS测试构建日志。比如下面的例子,VS测试任务构建日志可以在http://wxv-xindo-12r2:8080/tfs/DefaultCollection/5dfb8b33-9949-4187-9d09-474c8fe87238/_apis/build/builds/1477/logs/5找到。

    {
        "id": "c43e4f8f-3db9-4fdc-90c0-1153f165b9a9",
        "parentId": "39d66172-979a-46e8-a04d-fe8e4e77da43",
        "type": "Task",
        "name": "Test Assemblies",
        "startTime": "2018-05-25T02:31:11.177Z",
        "finishTime": "2018-05-25T02:31:17.133Z",
        "currentOperation": null,
        "percentComplete": null,
        "state": "completed",
        "result": "succeeded",
        "resultCode": null,
        "changeId": 17,
        "lastModified": "0001-01-01T00:00:00",
        "workerName": "mypc",
        "order": 5,
        "details": null,
        "errorCount": 0,
        "warningCount": 2,
        "url": null,
        "log": {
            "id": 5,
            "type": "Container",
            "url": "http://tfsserver:8080/tfs/DefaultCollection/5dfb8b33-9949-4187-9d09-474c8fe87238/_apis/build/builds/1477/logs/5"
        },
        "task": {
            "id": "ef087383-ee5e-42c7-9a53-ab56c98420f9",
            "name": "VSTest",
            "version": "2.0.55"
        },
        "issues": [
            {
                "type": "warning",
                "category": "General",
                "message": "",
                "data": {
                    "type": "warning",
                    "code": "002004"
                }
            },
            {
                "type": "warning",
                "category": "General",
                "message": "No test assemblies found matching the pattern: **\\release\\*test*.dll,!**\\obj\\**.",
                "data": {
                    "type": "warning"
                }
            }
        ]
    }

然后检查VS测试任务是否有警告:

[warning]No test assemblies found matching the pattern: '**\*test*.dll;-:**\obj\**'

如果 VS 测试构建日志包含上述警告,则通过 exit 1 使 PowerShell 任务失败。

【讨论】:

  • 我在我的服务器上使用 TFS,可能我的 TFS 实例没有 API
  • Timeline REST API 确实支持 TFS,您只需要使用较低的 api 版本,例如 2.0 或 3.0。并且我在我的回答中用 TFS 更新了 REST API 格式,你可以试一试。
猜你喜欢
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2017-04-10
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多