且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用VSTest而不是MSTest运行单元测试用例

更新时间:2023-02-20 18:10:50

通过VSTest执行单元测试并发布通过MSTest的测试结果使我获得了成功的结果。以下是Powershell脚本:

Executing unit tests through VSTest and publishing the test results through MSTest gave me a successful outcome. Given below is the Powershell script:

#  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