要求构建包含单元测试VSTS

时间:2018-05-24 08:41:26

标签: azure-devops azure-pipelines

我有一个问题

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

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

谢谢。

1 个答案:

答案 0 :(得分:-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测试构建日志。如下例所示,可以在http://wxv-xindo-12r2:8080/tfs/DefaultCollection/5dfb8b33-9949-4187-9d09-474c8fe87238/_apis/build/builds/1477/logs/5中找到VS测试任务构建日志。

    {
        "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任务失败。