22

I would love to know if there is a way I can get Visual Studio to run unit tests corresponding to a given assembly whenever I build it.

Given a solution containing projects structured like this:

Assembly1

Assembly1.Tests 

Assembly2 

Assembly2.Tests

Is there a way I can get the unit tests in Assembly2.Tests to run whenever Assembly2 is built?

That would be amazing.

I'm using Visual Studio 2008 Standard Edition.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
David
  • 15,750
  • 22
  • 90
  • 150

2 Answers2

18

You can use the nUnit console utility to run the tests as a post-build event for the individual project.

You call the nunit-console.exe and supply your assembly containing your tests as an argument.

"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit-console.exe" "PathToTestAssembly.dll"

or

You can run the tests in the GUI:

"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit.exe" "PathToTestAssembly.dll" /run

Edit:

Removed the part about the post-build event for the test assembly project.

fletcher
  • 13,380
  • 9
  • 52
  • 69
  • I won't be able to try this just yet, but if you're right it's a rare case of something really useful being easy to set up. I'll hit you back in a bit... – David Aug 13 '10 at 09:26
  • Sorry, does your caveat (about where to put the post-build event) apply to both of your alternatives, or only the latter? – David Aug 13 '10 at 09:27
  • Only the latter, as the $(TargetPath) variable refers to the outputted assembly for that project. So, if you used the latter postbuild event in the build for the main assembly (that which is under test) it wouldn't work in nunit. – fletcher Aug 13 '10 at 09:34
  • This is one of the most useful things I've learnt on SO. Thanks again. – David Aug 16 '10 at 15:41
  • 1
    Very helpful, thank you! I'd also suggest adding the NUnit bin directory to your path, so you don't have to change it in your build event for every project after upgrading NUnit. (Not sure why NUnit specifies its entire version in its install directory when it's the kind of program you'd add to your Path). – System.Cats.Lol Jul 25 '13 at 20:46
  • 1
    Also, if you get an exit code -100, check for an architecture mismatch between your assembly and nunit; you may need to use nunit-console-x86.exe instead. (I had to do this before it would work.) – System.Cats.Lol Jul 25 '13 at 21:04
  • 1
    One last tip: Be sure to set "Run the post-build event" drop-down to "When the build updates the project output". Otherwise, NUnit runs _every_ build even if no code was touched. – System.Cats.Lol Jul 25 '13 at 21:13
1

Update for Visual Studio 2019 which has Test Explorer > Settings > Run Tests After Build. Works with unit tests targeting any combination of NUnit, xUnit or MSTest, provided the tests are picked up by the Test Explorer.

Worth noting that if a compilation error occurs in one project, unit tests which do not target the failed project still run. There does not appear to be an option to "Only run tests if all projects build successfully".

AlainD
  • 5,413
  • 6
  • 45
  • 99