Automated Unit Testing: MSTest vs XUnit vs NUnit
Automated Unit testing can be run as often as you want, on as many kinds of data as you want and with next to no human involvement beyond once the tests are written. Not only that but using code to test code will often result in you noticing flaws with your program that would have been very difficult to spot from a programmer’s viewpoint.Once you have finalized for the unit test, the next question is to which test framework should you use? The main contenders are Ms Test, NUnit and the newer kid on the block, xUnit.
Today we will take a look into a few popular C# unit testing frameworks and try them out first hand so you can choose which one best suits your project.
Popular Automated Unit Testing Frameworks
- Built-in Visual Studio testing tools
- Ms Test
- NUnit
- XUnit
All of these unit testing frameworks offer a similar end goal, to help make writing unit tests faster, simpler and easier! But there are still a few key differences between them. Some are more focused towards powerful complex tests, while others rank simplicity and usability as a higher priority. Let’s take a look :
Built-in Visual Studio Testing Tools
Microsoft unit test framework for C++. The Microsoft unit test framework for C++ is installed withVisual Studio and provides a framework for testing native code. Code coverage tools. You can determine the amount of product code that your unit tests exercise from one command in TestExplorer.
- Test Explorer. Test Explorer lets you run unit tests and view their results. Test Explorer can use any unit test framework, including a third-party framework, that has an adapter for the Explorer.
- Microsoft unit test framework for managed code. The Microsoft unit test framework for managed code is installed with Visual Studio and provides a framework for testing .NET code.
- Microsoft unit test framework for C++. The Microsoft unit test framework for C++ is installed with Visual Studio and provides a framework for testing native code.
- Code coverage tools. You can determine the amount of product code that your unit tests exercise from one command in Test Explorer.
- Microsoft Fakes isolation framework. The Microsoft Fakes isolation framework can create substitute classes and methods for production and system code that create dependencies in the code under test. By implementing the fake delegates for a function, you control the behavior and output of the dependency object.
MSTest
MSTest has been around since Visual Studio 2015, at least. When it first came out, didn’t have a way to pass parameters into your unit tests. For this reason, a lot of people opted to use NUnit instead. Since V2 MSTest also supports parameters, so the difference between the frameworks on a day-to-day basis has lessened a lot. A very basic test class using MSTest will look like this:
XUnit
xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages. xUnit.net works with ReSharper, CodeRush, TestDriven.NET and Xamarin.
NUnit
NUnit is an open-source unit testing framework for Microsoft .NET. It serves the same purpose as JUnit does in the Java world and is one of many programs in the xUnit family. Tests can be run from a console runner, within Visual Studio through a Test Adapter or through 3rd party runners. Tests can be run in parallel. It has Strong support for data driven tests. Unit supports multiple platforms including .NET Core, Xamarin Mobile, Compact Framework and Silverlight. In Nunit every test case can be added to one or more categories, to allow for selective running. The basic Nunit console looks as below:
Below is the basic comparison of the 3 tools mentioned above.
Attributes
NUnit 3.x | MSTest 15.x | xUnit.net 2.x | Comments |
[Test] | [TestMethod] | [Fact] | Marks a test method. |
[TestFixture] | [TestClass] | n/a | xUnit.net does not require an attribute for a test class; it looks for all test methods in all public (exported) classes in the assembly. |
Assert.That Record.Exception | [ExpectedException] | Assert.Throws Record.Exception | xUnit.net has done away with the ExpectedException attribute in favor of Assert.Throws . See Note 1 |
[SetUp] | [TestInitialize] | Constructor | We believe that use of [SetUp] is generally bad. However, you can implement a parameterless constructor as a direct replacement. |
[TearDown] | [TestCleanup] | IDisposable.Dispose | We believe that use of [TearDown] is generally bad. However, you can implement IDisposable.Dispose as a direct replacement. |
[OneTimeSetUp] | [ClassInitialize] | IClassFixture<T> | To get per-class fixture setup, implement IClassFixture<T> on your test class. |
[OneTimeTearDown] | [ClassCleanup] | IClassFixture<T> | To get per-class fixture teardown, implement IClassFixture<T> on your test class. |
n/a | n/a | ICollectionFixture<T> | To get per-collection fixture setup and teardown, implement ICollectionFixture<T> on your test collection. |
[Ignore("reason")] | [Ignore] | [Fact(Skip="reason")] | Set the Skip parameter on the [Fact] attribute to temporarily skip a test. |
[Property] | [TestProperty] | [Trait] | Set arbitrary metadata on a test |
[Theory] | [DataSource] | [Theory] [XxxData] | Theory (data-driven test). |
Conclusion
All 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 Ms Test made NUnit a better consideration. Today, that gap has narrowed so the choice between NUnit and MsTest is less. Historically unit testing maintenance has nightmares with the set-up code. Packages like Autofxiture will help alleviate those issues, but choosing a framework that forces the team to write more modular and flexible code feels like a better choice nowadays.
No comments:
Post a Comment