David
David

Reputation: 16130

Automatic running of unit tests on build

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.

Upvotes: 23

Views: 9563

Answers (2)

AlainD
AlainD

Reputation: 6625

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".

Upvotes: 2

fletcher
fletcher

Reputation: 13780

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.

Upvotes: 18

Related Questions