Homepage | About Me | Testing ASP.net Book | Best Blog Posts | Personal Projects | Follow me on Twitter | GitHub | SlideShare | RSS
Blog.BenHall.me.uk

MSBuild – Build Visual Studio 2008 solution and execute Unit Tests

Tuesday, July 29, 2008

Previously, I have spoke about how you can update your AssemblyInfo file using MSBuild. However, I haven’t spoken about the very basics – how to build a Visual Studio solution and execute your unit tests.

MSBuild is a very interesting build scripting language, out of the box it includes a set of standard tasks which you can use based on your requirements, such as creating a directory or copying a file in order to correctly build your project. I also take advantage of the MSBuild community task project, while it hasn’t been updated in a while, the tasks work perfectly.

Below is the basic MSBuild script.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Test" xmlns="
http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/> 
    <Target Name="Build">
        <Message Text="Starting to Build"/>
        <MSBuild Projects="TeamCityBlogExample.sln" Properties="Configuration=Release" />
    </Target>
    <Target Name="Test" DependsOnTargets="Build">
        <Message Text="Starting to Test"/>
        <NUnit Assemblies="TeamCityBlogExample.Tests\bin\Release\TeamCityBlogExample.Tests.dll"
               ContinueOnError="false"
               ToolPath="C:\Program Files\TestDriven.NET 2.0\NUnit\2.4\" 
               OutputXmlFile="NUnitResults.xml" />
    </Target>
</Project>

Within the project section at the top, I set the default target to Test. This is the target which will be executed first, the Test target then has a DependsOnTarget attribute which enforces that the build must be done before we test.

On line 3, I import a reference to the MSBuild community tasks so I can access all of the custom tasks included – such as NUnit.  Next I define my build target, within this I define the solution I want to build and the configuration. To execute my unit tests, I use the NUnit task from the community task project, this takes a list of all the assemblies which we need to run, define if a test fails wether it should fail the build and finally tell it where NUnit lives on the local machine.

Now, the script can be executed by a build system, such as CCNet or TeamCity, or from the command line using the msbuild.exe <scriptname> command and build your solution and execute your unit tests.

Download script: http://blog.benhall.me.uk/Code/Build/BaseMSBuildScript.txt

Technorati Tags: ,

Labels: ,

Blogger comments

At 9:21 PM, Blogger Jeff Brown said...
You might find this interesting:
http://code.google.com/p/metabuild/