如何在VSTS中使用NUnit?

4
我正在尝试在Visual Studio Team System中使用NUnit 3,但这似乎非常困难。我已经将测试程序集步骤添加到构建过程中,并在高级执行选项->自定义测试适配器路径中按照帮助文档的建议输入了NUnitVisualStudioTestAdapter-2.0.0.vsix插件的目录路径。
无论我做什么,它似乎都找不到或尝试运行这个插件。它只运行Visual Studio Test并报告找不到测试。我还尝试将测试适配器作为NuGet包和其中的工具路径放入,但那也不起作用。
有人成功地让NUnit在VSTS中工作并返回测试结果吗?
我得到的日志是:
2017-02-07T10:59:26.2778725Z ##[section]Starting: Test Assemblies **\*.Unit.Tests.dll;-:**\obj\**
2017-02-07T10:59:26.2798723Z ==============================================================================
2017-02-07T10:59:26.2798723Z Task         : Visual Studio Test
2017-02-07T10:59:26.2798723Z Description  : Run tests with Visual Studio test runner
2017-02-07T10:59:26.2798723Z Version      : 2.0.10
2017-02-07T10:59:26.2798723Z Author       : Microsoft Corporation
2017-02-07T10:59:26.2798723Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkId=835764)
2017-02-07T10:59:26.2798723Z ==============================================================================
2017-02-07T10:59:28.6268742Z ##[warning]No test assemblies found matching the pattern: **\*.Unit.Tests.dll;-:**\obj\**.
2017-02-07T10:59:28.6998724Z ##[section]Finishing: Test Assemblies **\*.Unit.Tests.dll;-:**\obj\**

单元测试的DLL文件名为App.Web.Unit.Tests.dll。

当system.debug = true时,我得到的日志如下(为了适应30000个字符,去掉了一些导入语句):

2017-02-08T10:44:46.2171630Z ##[section]Starting: Test Assemblies **\*.Unit.Tests.dll;-:**\obj\**
2017-02-08T10:44:46.2251636Z ==============================================================================
2017-02-08T10:44:46.2251636Z Task         : Visual Studio Test
2017-02-08T10:44:46.2251636Z Description  : Run tests with Visual Studio test runner
2017-02-08T10:44:46.2251636Z Version      : 1.0.84
2017-02-08T10:44:46.2251636Z Author       : Microsoft Corporation
2017-02-08T10:44:46.2251636Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkId=624539)
2017-02-08T10:44:46.2251636Z ==============================================================================
2017-02-08T10:44:46.2381636Z Preparing task execution handler.
2017-02-08T10:44:52.1286673Z Executing the powershell script: D:\a\_tasks\VSTest_ef087383-ee5e-42c7-9a53-ab56c98420f9\1.0.84\VSTest.ps1
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddCommand(D:\a\_tasks\VSTest_ef087383-ee5e-42c7-9a53-ab56c98420f9\1.0.84\VSTest.ps1)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - Add inputParameters
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(testAssembly=**\*Tests.dll;-:**\obj\**)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(testFiltercriteria=)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(runSettingsFile=D:\a\1\s)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(overrideTestrunParameters=)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(codeCoverageEnabled=true)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(runInParallel=false)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(vstestLocationMethod=version)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(vsTestVersion=latest)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(vstestLocation=)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(pathtoCustomTestAdapters=D:\a\1\s\packages)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(otherConsoleOptions=)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(testRunTitle=)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(platform=any cpu)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(configuration=release)
2017-02-08T10:44:52.1296976Z ##[debug]PowerShellHandler.Execute - AddParameter(publishRunAttachments=true)
2017-02-08T10:44:52.4586977Z ##[debug]PowerShellHandler.Execute - Invoke
2017-02-08T10:44:52.5076979Z ##[debug]Entering script VSTest.ps1
2017-02-08T10:44:52.5096982Z ##[debug]vsTestVersion = latest
2017-02-08T10:44:52.5096982Z ##[debug]testAssembly = **\*Tests.dll;-:**\obj\**
2017-02-08T10:44:52.5096982Z ##[debug]testFiltercriteria = 
2017-02-08T10:44:52.5096982Z ##[debug]runSettingsFile = D:\a\1\s
2017-02-08T10:44:52.5096982Z ##[debug]codeCoverageEnabled = true
2017-02-08T10:44:52.5106990Z ##[debug]pathtoCustomTestAdapters = D:\a\1\s\packages
2017-02-08T10:44:52.5106990Z ##[debug]overrideTestrunParameters = 
2017-02-08T10:44:52.5106990Z ##[debug]otherConsoleOptions = 
2017-02-08T10:44:52.5106990Z ##[debug]testRunTitle = 
2017-02-08T10:44:52.5106990Z ##[debug]platform = any cpu
2017-02-08T10:44:52.5106990Z ##[debug]configuration = release
2017-02-08T10:44:52.5106990Z ##[debug]publishRunAttachments = true
2017-02-08T10:44:52.5106990Z ##[debug]vstestLocation = 
2017-02-08T10:44:52.5476990Z ##[debug]Loading module from path 'C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\2.111.1\externals\vstshost\Microsoft.TeamFoundation.DistributedTask.Task.Internal\Microsoft.TeamFoundation.DistributedTask.Task.Internal.psd1'.
2017-02-08T10:44:52.5566992Z ##[debug]Loading module from path 'C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\2.111.1\externals\vstshost\Microsoft.TeamFoundation.DistributedTask.Task.Internal\Microsoft.TeamFoundation.DistributedTask.Task.Internal.psm1'.
2017-02-08T10:44:52.5656993Z ##[debug]Loading module from path 'C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\2.111.1\externals\vstshost\Microsoft.TeamFoundation.DistributedTask.Task.LegacySDK.dll'.
2017-02-08T10:44:52.6656999Z ##[debug]Loading module from path 'C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\2.111.1\externals\vstshost\Microsoft.TeamFoundation.DistributedTask.Task.Common\Microsoft.TeamFoundation.DistributedTask.Task.Common.psd1'.
2017-02-08T10:44:52.6727018Z ##[debug]Loading module from path 'C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\2.111.1\externals\vstshost\Microsoft.TeamFoundation.DistributedTask.Task.Common\Microsoft.TeamFoundation.DistributedTask.Task.Common.psm1'.
2017-02-08T10:44:52.7086999Z ##[debug]Loading module from path 'C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\2.111.1\externals\vstshost\Microsoft.TeamFoundation.DistributedTask.Task.TestResults\Microsoft.TeamFoundation.DistributedTask.Task.TestResults.psm1'.
2017-02-08T10:44:52.7336993Z ##[debug]Loading module from path 'C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\2.111.1\externals\vstshost\Microsoft.TeamFoundation.DistributedTask.Task.CodeCoverage\Microsoft.TeamFoundation.DistributedTask.Task.CodeCoverage.psm1'.
2017-02-08T10:44:52.7406994Z ##[debug]Importing cmdlet 'Invoke-ResultPublisher'.
2017-02-08T10:44:52.7416986Z ##[debug]Importing cmdlet 'Publish-TestResults'.
2017-02-08T10:44:52.7416986Z ##[debug]Importing cmdlet 'Add-BuildArtifactLink'.
2017-02-08T10:44:52.7416986Z ##[debug]Importing cmdlet 'Get-MSBuildLocation'.
2017-02-08T10:44:52.7416986Z ##[debug]Importing cmdlet 'Get-ToolPath'.
2017-02-08T10:44:52.7416986Z ##[debug]Importing cmdlet 'Get-VisualStudioPath'.
2017-02-08T10:44:52.7427007Z ##[debug]Importing cmdlet 'Get-VssConnection'.
2017-02-08T10:44:52.7427007Z ##[debug]Importing cmdlet 'Invoke-Ant'.
2017-02-08T10:44:52.7427007Z ##[debug]Importing cmdlet 'Register-XamarinLicense'.
2017-02-08T10:44:52.7427007Z ##[debug]Importing cmdlet 'Remove-Environment'.
2017-02-08T10:44:52.7427007Z ##[debug]Importing cmdlet 'Remove-EnvironmentResources'.
2017-02-08T10:44:52.7427007Z ##[debug]Importing cmdlet 'Complete-EnvironmentOperation'.
2017-02-08T10:44:52.7427007Z ##[debug]Importing cmdlet 'Complete-EnvironmentResourceOperation'.
2017-02-08T10:44:52.7436999Z ##[debug]Importing cmdlet 'Complete-ResourceOperation'.
2017-02-08T10:44:52.7436999Z ##[debug]Importing cmdlet 'Get-Environment'.
2017-02-08T10:44:52.7436999Z ##[debug]Importing cmdlet 'Get-EnvironmentProperty'.
2017-02-08T10:44:52.7436999Z ##[debug]Importing cmdlet 'Get-EnvironmentResources'.
2017-02-08T10:44:52.7436999Z ##[debug]Importing cmdlet 'Get-ExternalIpAddress'.
2017-02-08T10:44:52.7436999Z ##[debug]Importing cmdlet 'Get-ParsedSessionVariables'.
2017-02-08T10:44:52.7436999Z ##[debug]Importing cmdlet 'Add-TaskIssue'.
2017-02-08T10:44:52.7447001Z ##[debug]Importing cmdlet 'Publish-CodeCoverage'.
2017-02-08T10:44:52.7447001Z ##[debug]Exporting cmdlet 'Enable-CodeCoverage'.
2017-02-08T10:44:52.7447001Z ##[debug]Exporting cmdlet 'Publish-CodeCoverage'.
2017-02-08T10:44:52.7447001Z ##[debug]Importing cmdlet 'Enable-CodeCoverage'.
2017-02-08T10:44:52.7447001Z ##[debug]Importing cmdlet 'Publish-CodeCoverage'.
2017-02-08T10:44:52.8017010Z ##[debug]Invoke - Get-TaskVariable cmdlet
2017-02-08T10:44:52.8107020Z ##[debug]Pattern found in solution parameter. Calling Find-Files.
2017-02-08T10:44:52.8107020Z ##[debug]Calling Find-Files with pattern: **\*Tests.dll;-:**\obj\**
2017-02-08T10:44:52.8147008Z ##[debug]FindFiles.FindMatchingFiles(rootFolder = D:\a\1\s, matchPattern = **\*Tests.dll;-:**\obj\**, includeFiles = True, includeFolders = False
2017-02-08T10:44:52.8177007Z ##[debug]FindFiles.GetMatchingItems(includePatterns.Count = 1, excludePatterns.Count = 1, includeFiles = True, includeFolders = False
2017-02-08T10:44:53.4177077Z ##[debug]FindFiles.FindMatchingFiles - Found 0 matches
2017-02-08T10:44:53.4237058Z ##[debug]Found files: 
2017-02-08T10:44:53.4467065Z ##[debug]Processed: ##vso[task.logissue type=warning;code=002004;]
2017-02-08T10:44:53.4467065Z 
2017-02-08T10:44:53.4467065Z 
2017-02-08T10:44:53.4587060Z ##[warning]No test assemblies found matching the pattern: '**\*Tests.dll;-:**\obj\**'.
2017-02-08T10:44:53.4587060Z ##[debug]Processed: ##vso[task.logissue type=warning]No test assemblies found matching the pattern: '**\*Tests.dll;-:**\obj\**'.
2017-02-08T10:44:53.4607060Z ##[debug]Leaving script VSTest.ps1
2017-02-08T10:44:53.4914646Z ##[section]Finishing: Test Assemblies **\*.Unit.Tests.dll;-:**\obj\**

更新:

我已经将NUnit3TestAdapter包安装到单元测试项目中。我已经修改了VSTS中的构建步骤:

它是版本2.*(预览版)

搜索模式为: *test*.dll !\obj** 并且在高级执行选项中,我已将自定义测试适配器路径设置为$(Build.SourcesDirectory)\

然后我得到了这个调试信息,至少现在它已经找到了测试适配器,尽管没有找到测试:

[command]"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" D:\a\1\s\packages\NUnit3TestAdapter.3.7.0\tools\NUnit3.TestAdapter.dll /EnableCodeCoverage /logger:trx "/TestAdapterPath:\"D:\a\1\s\\\""
Microsoft (R) Test Execution Command Line Tool Version 14.0.25420.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Warning: Using Isolation mode to run tests as required by effective Platform:X86 and .Net Framework:Framework35 settings for test run. Use the /inIsolation parameter to suppress this warning.
Information: NUnit Adapter 3.7.0.0: Test execution started

Information: Running all tests in D:\a\1\s\packages\NUnit3TestAdapter.3.7.0\tools\NUnit3.TestAdapter.dll

Information: NUnit failed to load D:\a\1\s\packages\NUnit3TestAdapter.3.7.0\tools\NUnit3.TestAdapter.dll

Information: NUnit Adapter 3.7.0.0: Test execution complete

Warning: No test is available in D:\a\1\s\packages\NUnit3TestAdapter.3.7.0\tools\NUnit3.TestAdapter.dll. Make sure that installed test discoverers & executors, platform & framework version settings are appropriate and try again.

你使用的是哪个 NUnit 版本? - Chris
添加了日志和NUnit 3细节。 - johnstaveley
我应该在哪里做这个? - johnstaveley
你能分享完整的构建日志和构建定义设置吗? - Eddie Chen - MSFT
日志在这里:https://www.dropbox.com/s/x8najikfii09xxq/logs_148.zip?dl=0 - johnstaveley
显示剩余3条评论
2个回答

2
自定义测试适配器的路径必须指向提取的 vsix 或提取的 NuGet 包。
使其工作的最简单方法是将 NUnit Test Adapter NuGet package 2.x/3.x 添加到您的单元测试项目中。VsTest 任务应该会自动检测已还原到 NuGet 包目录中的测试运行程序:

NuGet restored packages are automatically searched for

确保nunit.framework版本和NUnit适配器版本匹配。确保两个包都添加到您的测试项目中。

如果我添加了NUnit测试适配器包,我需要设置路径吗? - johnstaveley
我已将其设置为此:\packages\NUnit3TestAdapter.3.7.0\,但仍无法运行测试。 - johnstaveley
我觉得你只是想把它设置为包的根目录?在这里看一下:http://nocture.dk/2015/06/19/xunit-or-nunit-with-visual-studio-online-build/ 此外,请确保您正在使用NUnit3适配器,就像您在上面的评论中所说的那样 - 在您的原始问题中,您声明了您正在使用NUnit v2 Adapter,这是行不通的。 - Chris
我正在使用 NUnit 3 和适配器。我得到了一个不同的消息:准备任务执行处理程序。 2017-02-07T13:36:49.2019486Z 执行 PowerShell 脚本:D:\a_tasks\VSTest_ef087383-ee5e-42c7-9a53-ab56c98420f9\1.0.84\VSTest.ps1 - johnstaveley
但它仍然找不到测试。 - johnstaveley
这比尝试弄清楚MSBuild社区的东西要容易得多。非常感谢。 - John H

1
你在原帖中的问题是 Visual Studio Test 任务无法找到要测试的测试程序集(App.Web.Unit.Tests.dll)。在你更新后,当你将搜索模式更改为“*test.dll”之后,它仍然无法找到测试程序集。它只能找到“NUnit3.TestAdapter.dll”,这是 NUnit 测试适配器的二进制文件,而二进制文件不包含任何测试方法。因此,你需要检查构建日志以查看测试程序集是否在构建步骤中正确生成以及其路径,然后根据该路径设置搜索模式。
更新:
根据你提供的日志,输出文件放置在“D:\ a \ 1 \ a”文件夹中,因为你指定了参数“/ p:OutDir = D:\ a \ 1 \ a”。尝试将搜索模式更改为: D:\a\1\a\*.Unit.Tests.dll$(build.stagingDirectory)\*.Unit.Tests.dll

我想要测试的dll位于构建产物的根目录中,但由于某种原因,VSTS正在测试适配器的目录中搜索它。 - johnstaveley

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