如果启用代码覆盖率,TFS 2017测试任务在VS 2017构建机上运行失败

4

我们正在一个安装了VS 2017 Enterprise的构建机上执行多项测试,其中在Visual Studio测试任务中启用了代码覆盖率。但在TFS 2017控制台出现以下错误:

##[section]Starting: Test Assemblies **\*test*.dll
Preparing task execution handler.
Executing the powershell script: d:\builds\4\_tasks\VSTest_ef087383-ee5e-42c7-9a53-ab56c98420f9\1.0.60\VSTest.ps1
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform
Working folder: d:\builds\4\3\s
Executing C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe "d:\builds\4\3\s\Source\u.a.testassembly.dll"  /Settings:"C:\Users\username\AppData\Local\Temp\tmp9CCF.tmp" /EnableCodeCoverage /logger:trx /TestAdapterPath:"d:\builds\4\3\s"
Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation.  All rights reserved.

##[error]The test source file "/EnableCodeCoverage" provided was not found.

使用Visual Studio 2015企业版,测试已经成功执行并且没有错误。

我们提供了vstest.console.exe文件的自定义路径。

如果我在TFS 2017“Visual Studio Test”任务的复选框中禁用代码覆盖,并使用未指定代码覆盖收集器的runsettings文件,则测试将被执行,但不会报告任何代码覆盖率。

我也尝试过禁用代码覆盖复选框,并使用

/EnableCodeCoverage

测试任务中作为命令行参数提供的参数。这会导致与上述相同的错误。

我发现许多人在VS 2017中存在代码覆盖率问题,但这似乎主要与.NET Core项目有关?

谢谢你的帮助! :-)

更新1

如果我尝试通过开发人员命令提示符运行vstest.console.exe,则无法成功指定/EnableCodeCoverage选项。它会提供以下错误消息:

Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation.  All rights reserved.

The test source file "/EnableCodeCoverage" provided was not found.

如果我在不使用上述开关的情况下运行完全相同的命令,则不会出现此错误消息。

更新2

如果我使用Runsettings文件,其中指定了数据收集器,则会出现与JSON相关的错误:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) 
into type 'System.Collections.ObjectModel.Collection`1 Microsoft.VisualStudio.TestPlatform.ObjectModel.AttachmentSet]' 
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

如果我使用 /Diag 开关,将在日志文件中得到以下堆栈跟踪:
Stack Trace:
ved Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
ved Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
ved Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
ved Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
ved Newtonsoft.Json.Linq.JToken.ToObject[T](JsonSerializer jsonSerializer)
ved Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.JsonDataSerializer.DeserializePayload[T](Message message)
ved Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.<>c__DisplayClass6_0.<AfterTestRunEnd>b__0()
ved Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.InvokeDataCollectionServiceAction(Action action, ITestMessageEventHandler runEventsHandler)

更新 3

我们的构建服务器是一台 Windows 2012 标准版服务器。我们能够成功安装 VS 2017,但在安装 Windows SDK 负载时出现了错误。之后我们不得不手动安装 Windows SDK for Windows 8,因为 VS 2017 Enterprise 中包含的 Windows SDK 负载不能在 Windows Server 2012 上安装。

我刚刚注意到 Windows Server 2012 不在 支持的操作系统列表 中,这对于 VS 2017 来说可能会有问题...

更新 4

当从 VS 2017 开发人员命令提示符中运行 vstest.console.exe /? 时,详细输出如下:

Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Usage: vstest.console.exe [Arguments] [Options] [[--] <args>...]]
Description: Runs tests from the specified files.

然后它会列出所有的参数。

/Enablecodecoverage

switch找不到。使用开始菜单中的快捷方式打开开发人员命令提示符。

我尝试运行一个测试套件,其中数据收集器在.runsettings文件中定义,而不是使用/enablecodecoverage开关,但是这会产生上面列出的JSON错误。

更新5

在将我们的TFS升级到2017更新1之后,我们成功执行了单元测试并获得了代码覆盖率。

然而,我们正在使用SonarQube,显然在撰写本文时,在Marketplace Extension v. 3.0.0中提供的SonarQube扫描程序无法正确地定位代码覆盖率报告,因为在VS 2017中更改了注册表键。

已向SonarQube提交了有关此问题的错误报告

您可以通过在以下注册表键下手动创建名为“ShellFolder”的注册表值来解决此限制:

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\15.0

“ShellFolder”字符串值应该是:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise

我已经将上述信息留在这里,以防其他人遇到与我们类似的问题。

安装VS 2017代理后的结果是什么? - starian chen-MSFT
不,我安装在Windows 2012 R2上而不是Windows 8.1或Windows 10上。 - starian chen-MSFT
这不是安装包告诉我的。它声称我的构建服务器是一个Windows 8机器,因此无法安装。 所以你也许可以在Windows 2012 R2服务器上安装它,但很可能不能在Windows 2012上安装。 - llykke
你说构建机上安装了VS 2017,是指Windows 2012吗? - starian chen-MSFT
安装 VS 2017 代理时出现错误? - starian chen-MSFT
显示剩余3条评论
1个回答

1
原因是如果直接调用vstest.console.exe工具,则没有/EnableCodeCoverage选项。(为Visual Studio Test步骤/任务指定vstest.console.exe路径)。如果通过VS 2017开发人员命令提示符调用vstest.console命令,则会有/EnableCodeCoverage。
要解决此问题,您可以将TFS 2017升级到TFS 2017更新1,然后选择Visual Studio Test步骤/任务的2.*版本,之后您可以选择Visual Studio 2017选项作为VSTest版本。
更新:
另一个解决方法是,在构建代理上安装VS 2017代理(Visual Studio 2017的工具),然后在Visual Studio Test步骤/任务中为测试代理安装文件夹中的vstest.console.exe路径进行指定。(C:\Program Files (x86)\Microsoft Visual Studio\2017\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe)

enter image description here


如果我无法更新我的TFS服务器,我应该如何配置我的测试步骤? - llykke
@llykke 另一个解决方法是,在构建代理上安装测试代理 2017,然后使用测试代理安装文件夹中的 vstest.console.exe 工具。(我已经更新了我的答案) - starian chen-MSFT
只是在你的回答下面放置备注: 即使在开发人员提示符中运行,我也无法使vstest.console.exe接受/Enablecodecoverage开关。这是开发人员命令提示符的15.0版本... - llykke
@llykke,企业版刚刚加入了代码覆盖率功能,你的VS 2017版本是多少?(https://www.visualstudio.com/vs/compare/) - starian chen-MSFT
我们正在构建机器上使用企业版。如果我从2017开发人员命令提示符中运行vstest.console.exe并使用/enablecodecoverage开关,程序会将该开关解释为测试文件输入而不是开关(即失败)。如果我在没有开关的情况下运行程序,它只会静默停止。 - llykke
@llykke,在开发人员命令提示符中运行vstest.console /?命令的详细结果是什么? - starian chen-MSFT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接