使用VSTest运行unit testing用例而不是MSTest

我在TFS2010服务器上有一个x64平台C#解决方案(VS2012)。 我已将unit testing项目(也是x64)附加到此解决方案并创建了构建定义。 当我对构建进行排队时,它会成功,但不会执行unit testing用例。 这是因为MSTest是一个32位应用程序。 因此,我决定自定义默认构建过程模板(DefaultTemplate.xaml)以调用VSTest(VSTest.console.exe)而不是MSTest。 这非常复杂,我无法为VSTest的工具箱添加构建活动。

有人做过这种定制吗? 我还考虑过配置.runsettings文件等其他方法。 我们是否有可以添加到.runsettings文件中的VSTest适配器接口?

通过VSTest执行unit testing并通过MSTest发布测试结果给了我一个成功的结果。 以下是Powershell脚本:

# Get the UnitTest Binaries $files = Get-ChildItem $TestAssembliesDir\*est*.dll # VSTest.console.exe path $VSTestPath = 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe' # MSTest path $MSTestpath = "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe" # Loop through the test assemblies and add them to the VSTestFiles $VSTestFiles = '' foreach($file in $files) { $VSTestFiles += "`"$file`"" $VSTestFiles += " " } # Run the UnitTests using VSTest &$VSTestPath $vstestplatform "/Framework:Framework45" "/InIsolation" $VSTestFiles "/logger:trx" # Get TRX files $TrxFilesList = Get-ChildItem $TestResDir\*.trx $TrxFiles = '' $loop = 1 foreach($file in $TrxFilesList) { $TrxFiles = "$file" # copy the trx file into a space-less named trx $newTrxFileName = "$BuildUri" + "_" + "$loop" + ".trx" copy-item $TrxFiles -destination $TestResDir\$newTrxFileName $loop = $loop + 1 $newTrxFile += "$TestResDir\$newTrxFileName" $newTrxFile += " " } # specify MSTest arguments $mspubl = "/publish:"+$TeamProjColUri $msteampr = "/teamproject:" + $TeamProj $mspublbuild = "/publishbuild:" +$BuildUri $mspubresfile = "/publishresultsfile:" +"`"$newTrxFile`"" #Publish test results through MSTest &$MSTestpath $mstestplatform $flavor $mspubl $msteampr $mspublbuild $mspubresfile 

我也有完全相同的需要使用VSTest.Console.exe而不是MSTest.exe来编译VS2012 / .NET 4.5 x64应用程序的TFS2010构建过程,同时等待升级到TFS2012开始。

我采用的方法是编辑构建脚本XAML,删除unit testing的现有工作流,并将其替换为构建VSTest.Console.exe参数的自定义工作流,然后通过InvokeProcess执行VSTest.Console.exe。 然后我确保在Finally块中,无论测试结果如何,我们都使用构建服务器上的VS2012安装中的MSTest.exe将测试结果和代码覆盖率发布到TFS。

不幸的是我不能在答案中发布XAML,因为它超出了字符长度,但我确实有一个文本文件,包含要在DefaultTemplate.xaml中替换的片段以及要替换它的内容。 该文件可在此处找到。 请注意,虽然这种方法有效,但它是一种黑客行为。

另一种方法是使用NUnit而不是MSTest或VSTest.Console作为支持64位二进制文​​件。 本文将介绍如何在TFS2010构建脚本中集成NUnit,并提供实现此目的所需的工具和资源的链接。 NUnit的唯一问题是代码覆盖(需要另一个工具以及如何将这些结果发布到TFS)以及使用诸如DeploymentItem之类的属性和TestContext等属性的MSTest样式集成测试,这就是为什么我工作的地方我们选择了VSTest.Console.exe方法。

从我读到的内容来看,TFS2012可以从构建脚本轻松集成到VSTest.Console.exe,因此如果你曾经升级到TFS2012,可能不需要我记录的VSTest.Console.exe hack。

这不能直接回答你的问题,但它可能有所帮助。 我为TeamCity做了类似的事情。 我使用命令行来调用vstest.console.exe并创建了一个.runsettings文件。

我将此Microsoft模板用于runsettings文件。 但请注意,在我的机器上,第5行注释中提到的路径是相对于.runsettings位置而不是.sln。

如果使用vstest.console.exe的/ logger:trx选项,它将以与MSTest相同的格式生成输出(适用于结果可视化)。