On any .NET project, you should be writing unit tests. I won't go into the benefits of unit testing here, however, when you are new to unit testing in .NET and C# it can be hard to decide which test framework should you use? The main contenders are MsTest, NUnit and the newer kid on the block, xUnit. In today's tutorial, I'm going to quickly cover all three frameworks and hopefully give you enough information to pick which one works for you!

MSTest

MSTest has been around since Visual Studio 2015, at least. When it first came out, it didn't have a way to pass parameters into a unit test. For this reason, a lot of people opted to use NUnit instead. Since V2 MSTest also supports parameters, so the difference between the frameworks has lessened a lot. A very basic test class using MSTest will look like this:

NUNit

NUnit has been around since 2006 and has been the most popular testing framework of choice ever since. Like most things .NET, NUnit is installed via a NuGet package. It integrates with the Resharper test suite well, so you can run your tests within Visual Studio and get real-time metrics about the state of your tests.

An NUnit test class is constrcuted like this:

In terms of the overall structure, things are similar, the main difference is syntax. Syntax is definitely a personal preference, so you will likely have a natural affinity to one or the other. Personally, I prefer NUnits attribute naming conventions as I find it a little simpler and cleaner, however, in the grand scheme of life, it is a very tiny difference. The main reason I have always used NUnit was the ability to pass parameters into it tests:

I think this feature is very handy and it can reduce the amount of boilerplate test code you need to write. This is great 👌

xUnit

xUnit is the new kid on the block. The makers of NUnit didn't like a few things about it so they went off and created xUnit. So xUnit has been built from the learnings of NUnit. xUnit has definitely changed the game in terms of its approach. In xUnit, you don't have a test set-up and tear-down like you would in MsTest and NUnit. In xUnit you also don't have one test attribute, instead, you have [Facts] and [Theories]! A Fact is a test that doesn't take any input parameters and a Theory is a test that does. Obviously, as the newer kid, XUnit is also installed via NuGet with this command Install-Package xunit

I like xUnits notation and syntax and if you are installing a new test framework it is worth using. All three frameworks will probably do 99% of the things that you need to test on a day-to-day basis. 3 or 4 years ago the lack of certain features in MsTest made NUnit a better consideration. Today, that gap has narrowed so the choice between all three is less.

From my experience with unit testing, historically I've had worse problems dealing with the quality of the tests. Packages like Autofxiture will help alleviate those issues. From my experience, it's these maintenance considerations that are key when writing tests on a project. These are the things that are really important things to focus on and get correct compared to which test framework you choose. Bad code can be written using all three frameworks! If I had to pick, I would go with xUnit, if that's not possible for whatever reason, then I use NUnit. Remember, focus on quality code and the rest should magically fall into place. Happy Coding 🤘