MsTest and NUnit are the two most popular unit testing frameworks in the .NET ecosystem. I always get questions from my students asking about which should be used and why. Let’s talk about the advantages and disadvantages of each solution.

Parallelization

Parallelization is one of the most critical components when it comes to picking a unit testing library. A unit testing library that has poor parallelization is automatically at a massive disadvantage because it will slow down your continuous integration pipeline.

Parallelization with MsTest x NUnit

Parallelization at the Class aka Fixture level with NUnit is just fine. The problem arises when you want to parallelize at the method aka test method level.

How to parallelize methods with NUnit

The problem is that class level fields are not natively thread safe. As a result, NUnit expects the end-user to make sure that our code is thread-safe and will not handle that for us.

Here is what you have to do in order to have your NUnit test methods run in parallel. This article also does an excellent job at explaining.

The key here is that you cannot reuse the SetUp and Teardown from NUnit and have to write your own.

The SetUp needs to be part of the TestScope of the IDisposable. The Teardown needs to happen in the Dispose().

This may be acceptable to you.

The question is do you want to do the extra work to manage thread-safety of your tests?

Execution speed

MsTest is a native unit testing library that comes with Visual Studio from Microsoft. NUnit is an extra Nuget package that needs to be installed on top and interact with the APIs of Visual Studio. Nunit likely doesn’t have direct integration into the native APIs like MsTest does.

As a result, from my experience, MsTest is faster. I don’t have hard data on this, but from my experience and intuition, it only makes sense that MsTest is faster with its tight integration with Visual Studio.

Which one is more future-proof?

Can Microsoft break NUnit’s integration with Visual Studio or can NUnit break Visual Studio? Which organization has the control here? It’s obviously Microsoft and MsTest. There’s no guarantee that their future changes will not break something about NUnit.

MsTest is less likely to be impacted by future changes to software.

Should you use MsTest or NUnit?

The parity between the APIs is so similar that it’s negligible. The main difference is the ability of MsTest to execute in parallel at the method level. Also, the tight integration of MsTest with Visual Studio provides advantages in both speed and robustness when compared to NUnit. As a result, I recommend MsTest.

0
Share
Tweet
Share