Monday, April 25, 2016

Integrating NUnit into Visual Studio -- Update for NUnit 3

Overview: This article talks about using the NUnit Test Adapter to integrate the NUnit test runner with the Visual Studio Test Explorer. In particular, we need to ensure that we're using the right Test Adapter package for the version of NUnit that we're using.

I like using the Visual Studio Test Explorer. This is integrated into my environment, and I can always undock the window and move it to a different monitor. In particular, I love this button:


This is the "Run Tests After Build" button (it's currently only available in the expensive version of Visual Studio). When this button is toggled down, impacted unit tests are automatically run every time I build. This gives me immediate feedback when I break something.

Integrating NUnit with the Test Adapter
I've started using NUnit more and more (check out the "Why NUnit?" articles here: http://www.jeremybytes.com/Demos.aspx#UTMMF). In addition to the NUnit framework, there is also a Test Adapter package available from NuGet that integrates the NUnit test runner with the Visual Studio test explorer.

I've been using this functionality for quite some time now: Integrating NUnit into Visual Studio Test Explorer.

The problem is that we've had a bit of a version mis-match between NUnit and the Test Adapter since NUnit 3 came out last November. The good news is that last week (April 19th, 2016), the Test Adapter that's compatible with NUnit 3 finally hit release.

NUnit Version 3 and the NUnit3TestAdapter
To use NUnit and the Test Adapter with a particular project in Visual Studio, we just need to use NuGet to grab the appropriate packages. But we need to pay attention to the packages that we're pulling.

For NUnit 3, we want the following 2 packages:


Since we're using Nunit version 3 (specifically v3.2.1), we need to use the "NUnit3TestAdapter" package.

Note: This is a completely different package from the old test adapter. It is not merely the old package with a new version.

NUnit Version 2 and the NUnitTestAdapter
If you're still using NUnit version 2, we need to use a completely different test adapter package:


Here, we have NUnit version 2 (specifically v2.6.4), so we need to use the "NUnitTestAdapter" package.

Note: This is a different package from the new test adapter.

Wrap Up
The moral of the story is that we need to be careful about the packages that we pull down when using NUnit.

When using NUnit 3, we need to use the NUnit3TestAdapter package.
When using NUnit 2, we need to use the NUnitTestAdapter package.

Even though the NUnit framework packages are the same (with different versions), the test adapter packages are different packages. If we get a mismatch, then we won't see our tests in the test explorer.

But when we get the right packages together, things work great. We get to see our tests inside Visual Studio, and we can interact with them easily in the integrated environment.

Happy Coding!

4 comments:

  1. Hi Jeremy, nice post.

    I have a question, in this NUnit Version 3 integration to Visual Studio, did you find the same quirks with CodeLens and "Run Tests After Build" which you found in this post (http://jeremybytes.blogspot.co.id/2015/05/integrating-nunit-into-visual-studio.html)?

    Thanks.

    ReplyDelete
    Replies
    1. Hi Zain,
      Quite honestly, I haven't spent much time with NUnit 3 and the latest test runner yet. I'll be looking at it more closely over the next week or so, and I'll post a follow-up on what I find with CodeLens and "Run Tests After Build".
      Thanks,
      Jeremy

      Delete
    2. Hi Jeremy,

      I will also try to look for it, but since I am quite new in Unit Testing, you might find about it earlier than me.
      So, I am looking forward to your next follow-up post.

      Thank you for your effort,
      Zain

      Delete
    3. Hi Jeremy,

      Well, I just tried it out based on one of your posts, and I found out that the CodeLens issue still exists, as you can see in this screenshot: https://flic.kr/p/GvjqHu

      For the "Run Tests After Build", I just realized that the button doesn't exist in my Visual Studio due to a cheaper version (VS Professional 2015), so I am still waiting for your result about this. :D

      Thank you for writing so many posts regarding Unit Tests, I think your "Unit Testing Makes Me Faster" articles will help me so much in convincing my boss & coworkers to embrace this testing methodology.

      Best Regards,
      Zain

      Delete