MsBuild and NUnit (A Simple example)

I have never really been to actively involved with the build part of setting up continuous deployment environment, as such I have not had too much exposure to MsBuild.exe, ok I have done a bit with Nant.exe before, but MsBuild is still pretty novel to me.

Anyway I decided I needed a small demo concept to see if I could get something up and running, and what I decided to have a go at was the following

  • Build entire solution
  • Run the unit tests

 

Solution File

The 1st issue I had was that solution files are not in MsBuild format. Mmmm
how strange. Thing is this doesn’t really matter, there is nothing to stop you
creating a custom MsBuild file, which just has to be a valid msbuild file.

Here is what my solution structure looked like

Like I say all I wanted to do was build the solution and run the tests.

So without further ado lets see what my msbuild file ended up looking like
shall we:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run">
<UsingTask AssemblyFile="$(MSBuildStartupDirectory)\Lib\MsBuildCommunityTasks\MSBuild.Community.Tasks.dll" TaskName="NUnit"/>
	<PropertyGroup> 
    <!-- After Compile: Result will be saved in OutDir -->
    <OutDir>$(MSBuildStartupDirectory)\OutDir\</OutDir>
    
    <!-- Configuration -->
    <Configuration>Release</Configuration>
    
    <!-- Solutionproperties-->
    <SolutionProperties>
      OutDir=$(OutDir);
      Platform=Any CPU;
      Configuration=$(Configuration)
    </SolutionProperties>
	</PropertyGroup>
	<ItemGroup>
		<Solution Include="NUnit.MsBuildDemo.sln">
			<Properties>
				$(SolutionProperties)
			</Properties>
		</Solution>
	</ItemGroup>
	<Target Name="Run">
    
    <CallTarget Targets="BuildSolution" />
    <CallTarget Targets="RunTests" />

  </Target>

  <PropertyGroup>
	<NUnitResultsFile>$(OutDir)\nunit-result.xml</NUnitResultsFile>
  </PropertyGroup>



  <Target Name="BuildSolution">
    <Message Text="BuildSolution called." />
    <MSBuild Projects="@(Solution)"/>
  </Target>


  <Target Name="RunTests">
    <CreateItem Include="$(OutDir)*.Tests.dll">
      <Output TaskParameter="Include" ItemName="TestAssembly" />
    </CreateItem>
    <NUnit 	Assemblies="@(TestAssembly)" 
		ToolPath="C:\Program Files (x86)\NUnit 2.6.2\bin"
		OutputXmlFile="$(NUnitResultsFile)"
	/>
  </Target>


</Project>

 

I did actually get this from someones blog, but I seem to have lost the link,
so if you think you know which one it was let me know, and I’ll put a link to it
here.

 

I don’t think that is too bad, and that hard to understand, we simply do
these things:

  1. Setup some solution properties
  2. Use the MsBuild msbuild task to build the solution
  3. Use the MsBuild Community Tasks to run Nunit (making sure we point to
    the version we want to use)
  4. The built files and test results will be in a folder names OutDir after
    we run this custom msbuild file

In order to run this custom msbuild file we simply use the following command
line:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
NUnit.MsBuildDemo.build /t:Run

 

MsBuild Community Tasks

The

MsBuild Community Tasks are awesome and they have many many useful MsBuild
tasks in them, I would encourage all of you to check them out at :


https://github.com/loresoft/msbuildtasks

 

As always here is a small demo project :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: