24 November 2010 HowTo ; MSBuild ; MSTest CI Team

imageAccording to the blogpost about "HowTo: build solutions with MSBuild" I´m going to show you a little example about how to call MSTests.

Scenario

The structure is nearly the same like in this blogpost. As a little add-on I created a new test project:

image

I´ve added one more "RunTests" target to my BuildSolutions.target file where the MSBuild Script is included:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
   <PropertyGroup>
		<OutDir>$(MSBuildStartupDirectory)\OutDir\</OutDir>
		<SolutionProperties>
					OutDir=$(OutDir);
					Platform=Any CPU;
					Configuration=Release
		</SolutionProperties>
   </PropertyGroup>
	<ItemGroup>
		<Solution Include="..\MsBuildSample.sln">
			<Properties>
							$(SolutionProperties)
			</Properties>
		</Solution>
	</ItemGroup>
	<Target Name="Build">
		<MSBuild Projects="@(Solution)"/>
	</Target>
	<Target Name="RunTests">
		<Exec Command='"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe" /testcontainer:"$(MSBuildStartupDirectory)\OutDir\MsBuildSample.WebApp.Tests.dll" /testcontainer:"$(MSBuildStartupDirectory)\OutDir\AnotherTestProject.dll"' />
	</Target>
</Project>

With the MSBuild command "Exec" I open the MSTest.exe with the adequate parameter. In this case the "testcontainer". Because we have to test-projects and both are loading into the OutDir as Assembly, we also need two testcontainer. Of course it´s possible to have numerous other tests.

Call the Script

To make the call a little easier I saved it in a .bat file:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe Buildsolution.targets /t:Build,RunTests

With the "t" parameter I´m able to decide which target should be opened. First I call "Build" and then "RunTests". I see the main things in the output:

image

Works with several other tools too

Once you understand the basics of MSBuild you are able to call everything working with CMD. I just need to think about a better way how to integrate the Tests into the Buildfile. With many tests you are going to lose the overview with a line like this:

<Exec Command='"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe" /testcontainer:"$(MSBuildStartupDirectory)\OutDir\MsBuildSample.WebApp.Tests.dll" /testcontainer:"$(MSBuildStartupDirectory)\OutDir\AnotherTestProject.dll"' />

If someone has a good idea, please let me now! Zwinkerndes Smiley

I know I´m not writing about something new but I had to google it so I thought you might be interested in it too.

[ Download Democode ]