I am currently developing in a solution with multiple test projects.  I only wanted to run the ones that are designated as “Unit” test projects on demand.  Normally I have used testing frameworks like XUnit and NUnit from a command line.  The team has done an excellent job with the naming conventions of dlls, so I wrote a msbuild script that gets these dlls from the solution directory structure and runs them all via the command line.

Items of note:

  1. I needed to exclude the obj directory and the TestResults directory.  Notice how I used an ItemGroup for excluded files.
  2. I place a ContinueOnError=”false” statement at the target level so I can see all tests that pass and fail.
  3. I added this script to run from the external tools menu in Visual Studio and deliver the results to the output window.

Ways to improve this script:

  1. Add a target to capture the test results so that they can be imported into a Visual Studio Test Project.
  2. Check the build condition and run in either Debug or Release mode.  I will probably fix this sooner than later, because I am going to have to do a release build at some point.  Also, the question will come up if I should even be building test projects in release mode I am sure.
  3. Set a property switch to fail when the first error is found

What are ways that you would change or update this script?

Get it here.  RunUnitTests.msbuild