Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VSTest to run unit test cases instead of MSTest

I have an x64 platform C# solution(VS2012) on a TFS2010 server. I have attached a unit test project (also x64) to this solution and created a build definition. When I queue the build, it succeeds but the unit test cases will not be executed. This is because MSTest is a 32 bit application. So, I decided to customize the default build process template (DefaultTemplate.xaml) to invoke VSTest(VSTest.console.exe) instead of MSTest. This is quite complex and I am unable to add a build activity to the toolbox for VSTest.

Has anyone done this kind of customization? I have also considered other approaches like configuring .runsettings file. Do we have a VSTest adapter interface that can be added in the .runsettings file ?

like image 273
Arkanoid Avatar asked Feb 20 '13 05:02

Arkanoid


People also ask

What is the difference between MSTest and VSTest?

There is a difference between vstest and mstest - the former is a test runner and the later is a test framework. Both could be used independently.

How do I run a specific unit test in Visual Studio?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

Do unit tests run in parallel C#?

Unit tests run one at a time. There is no parallelism.

Is MSTest a testing framework?

MSTest is a number-one open-source test framework that is shipped along with the Visual Studio IDE. It is also referred to as the Unit Testing Framework. However, MSTest is the same within the developer community. MSTest is used to run tests.


2 Answers

This does not directly answer you question, but it might help. I did a similar thing for TeamCity. I used command-line to call vstest.console.exe and created a .runsettings file.

I used this Microsoft template for the runsettings file. Note however that on my machine, the path mentioned in the comment in Line 5 is relative to the .runsettings location, not the .sln.

If you use /logger:trx option of vstest.console.exe, it will generate output in the same format as MSTest (good for result visualization).

like image 198
Wilbert Avatar answered Oct 04 '22 19:10

Wilbert


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
like image 36
Arkanoid Avatar answered Oct 04 '22 17:10

Arkanoid