Lesson 04 of 93 · 6 min read · Updated
MSTest vs NUnit vs xUnit: The Same Unit Tests Written in Three .NET Frameworks
On this page
Most .NET teams use one of three test frameworks: MSTest, NUnit or xUnit. Comparisons of them often stay abstract. This lesson takes one set of tests for a discount rule and writes it in all three frameworks, side by side. You will see that the test logic stays the same, while attributes, assertion order and test class lifetime differ in ways that matter.
Technical Requirements
- .NET 11 SDK (projects target
net11.0) - xUnit:
xunit2.9.3 withxunit.runner.visualstudio4.0.1 - MSTest: the
MSTestmeta package 4.5.3 - NUnit:
NUnit5.0.0 withNUnit3TestAdapter6.3.1 Microsoft.NET.Test.Sdk18.12.0 andcoverlet.collector10.1.1
All three test projects reference the same class library and test the same method, DiscountPolicy.CalculateDiscount. It returns 10% of the subtotal when the subtotal is 1000 or more, returns 0 below that, and throws ArgumentOutOfRangeException for a negative subtotal.
The Project Files
A test framework has two parts. The framework package defines tests: it gives you the attributes and the assertions. The adapter lets the test platform (VSTest) discover and run those tests. You need both.
M01.Foundations.MSTestExamples/M01.Foundations.MSTestExamples.csproj
M01.Foundations.NUnitExamples/M01.Foundations.NUnitExamples.csproj
The versions are not in these files because the solution uses central package management. The versions live in one Directory.Packages.props file. The xUnit project gets xunit, xunit.runner.visualstudio and Microsoft.NET.Test.Sdk the same way, through shared build settings.
Side-by-Side Comparison
| Concept | xUnit | MSTest | NUnit |
|---|---|---|---|
| Test class | (no attribute) | [TestClass] | [TestFixture] (optional) |
| Single test | [Fact] | [TestMethod] | [Test] |
| Parameterized test | [Theory] + [InlineData] | [TestMethod] + [DataRow] | [TestCase] |
| Before each test | constructor | [TestInitialize] | [SetUp] |
| After each test | IDisposable.Dispose | [TestCleanup] | [TearDown] |
| Once per class | IClassFixture<T> | [ClassInitialize] (static) | [OneTimeSetUp] |
| Category | [Trait("k","v")] | [TestCategory("v")] | [Category("v")] |
| Equality | Assert.Equal(expected, actual) | Assert.AreEqual(expected, actual) | Assert.That(actual, Is.EqualTo(expected)) |
| Exception | Assert.Throws<T> | Assert.ThrowsExactly<T> | Assert.Throws<T> |
The MSTest Version
M01.Foundations.MSTestExamples/DiscountPolicyMSTests.cs
Two details stand out. MSTest has no separate attribute for parameterized tests: a [TestMethod] with [DataRow] attributes becomes data-driven. And Assert.ThrowsExactly<T> passes only for that exact type. A subclass of ArgumentOutOfRangeException would fail the test. This is the same rule as xUnit's Assert.Throws<T>.
The double parameters are a C# limitation, not a framework choice. Attribute arguments must be compile-time constants, and decimal is not allowed there. All three frameworks share this limit, so the tests take double values and cast them.
The NUnit Version
M01.Foundations.NUnitExamples/DiscountPolicyNUnitTests.cs
NUnit uses a constraint model: Assert.That(actual, Is.EqualTo(expected)). It reads like a sentence, close to FluentAssertions, and it removes a classic mistake. With Assert.Equal(expected, actual) and Assert.AreEqual(expected, actual) it is easy to swap the two arguments. The test still passes or fails correctly, but the failure message then reports the values the wrong way round. In NUnit the actual value always comes first, so the order is hard to get wrong.
The MSTest and NUnit projects run with the same command as the xUnit project, because all three use VSTest adapters. The output has the same format too, which shows that the platform, not the framework, is in charge of running the tests:
Each project reports 6 passed tests: four test methods, one of which runs three data rows.
Common Mistake: State in Fields with NUnit
The most important difference is not in the syntax. It is in the lifetime of the test class:
- xUnit creates a new instance of the test class for every test.
- MSTest also creates a new instance for every test by default.
- NUnit, by default, creates one instance and shares it across all tests in the fixture.
So in NUnit, a field that one test changes is still changed when the next test runs. Tests start to depend on their execution order, and they fail or pass at random when the order changes. Do not carry state in fields between NUnit tests. Reset it in [SetUp], as the sample does with _threshold, so every test starts from a known state.
Which One Should You Choose?
None of the three is "better" in general. They all support the same core ideas: single tests, parameterized tests, setup and cleanup, categories and exception assertions. Choose based on the ecosystem your team already uses, the habits of the team, and what your runner and test platform need. Consistency inside one solution matters more than the choice itself.
Summary
- MSTest, NUnit and xUnit express the same test ideas with different attributes; the Arrange-Act-Assert logic does not change.
- The framework package defines tests; the adapter lets VSTest discover and run them.
- Watch the assertion order:
(expected, actual)in xUnit and MSTest,Assert.That(actual, constraint)in NUnit. - Attribute arguments cannot be
decimalin any of the three frameworks, so parameterized tests takedoubleand cast. - NUnit shares one test class instance across tests by default; reset state in
[SetUp]instead of relying on fields.