Click here to Skip to main content
13,259,847 members (53,859 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 3 Mar 2014

MsBuild and NUnit (A Simple Example)

, 3 Mar 2014
Rate this:
Please Sign up or sign in to vote.
MsBuild and NUnit (a simple example)

I have never really been too 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 first issue I had was that solution files are not in MsBuild format. Mmmm how strange. The thing is this doesn’t really matter, there is nothing to stop you from 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, let's see what my msbuild file ended up looking like, shall we:

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



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

  <Target Name="RunTests">
    <CreateItem Include="$(OutDir)*.Tests.dll">
      <Output TaskParameter="Include" ItemName="TestAssembly" />
    <NUnit     Assemblies="@(TestAssembly)" 

        ToolPath="C:\Program Files (x86)\NUnit 2.6.2\bin"




I did actually get this from someone's blog, but I seem to have lost the link, so if you think you know which one it was, please 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 build 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 /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:

As always here is a small demo project:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.


I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

You may also be interested in...

Comments and Discussions

Generalit is very helpful Pin
Southmountain23-May-15 12:12
memberSouthmountain23-May-15 12:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 3 Mar 2014
Article Copyright 2014 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid