Lesson 04 of 93 · 6 min read · Updated

MSTest vs NUnit vs xUnit: The Same Unit Tests Written in Three .NET Frameworks

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

  1. .NET 11 SDK (projects target net11.0)
  2. xUnit: xunit 2.9.3 with xunit.runner.visualstudio 4.0.1
  3. MSTest: the MSTest meta package 4.5.3
  4. NUnit: NUnit 5.0.0 with NUnit3TestAdapter 6.3.1
  5. Microsoft.NET.Test.Sdk 18.12.0 and coverlet.collector 10.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

<ItemGroup>
<!-- "MSTest" meta package: TestFramework + TestAdapter + Analyzers + Microsoft.NET.Test.Sdk -->
<PackageReference Include="MSTest" />
<PackageReference Include="coverlet.collector" />
</ItemGroup>

M01.Foundations.NUnitExamples/M01.Foundations.NUnitExamples.csproj

<ItemGroup>
<!-- NUnit: DEFINES the tests -->
<PackageReference Include="NUnit" />
<!-- NUnit3TestAdapter: lets VSTest discover and run NUnit tests -->
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="coverlet.collector" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

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

ConceptxUnitMSTestNUnit
Test class(no attribute)[TestClass][TestFixture] (optional)
Single test[Fact][TestMethod][Test]
Parameterized test[Theory] + [InlineData][TestMethod] + [DataRow][TestCase]
Before each testconstructor[TestInitialize][SetUp]
After each testIDisposable.Dispose[TestCleanup][TearDown]
Once per classIClassFixture<T>[ClassInitialize] (static)[OneTimeSetUp]
Category[Trait("k","v")][TestCategory("v")][Category("v")]
EqualityAssert.Equal(expected, actual)Assert.AreEqual(expected, actual)Assert.That(actual, Is.EqualTo(expected))
ExceptionAssert.Throws<T>Assert.ThrowsExactly<T>Assert.Throws<T>

The MSTest Version

M01.Foundations.MSTestExamples/DiscountPolicyMSTests.cs

using M01.Foundations.Domain;

namespace M01.Foundations.MSTestExamples;

[TestClass]
[TestCategory("Unit")]
public class DiscountPolicyMSTests
{
// [TestInitialize] is like the xUnit constructor: it runs BEFORE each test.
private decimal _threshold;

[TestInitialize]
public void SetUp() => _threshold = DiscountPolicy.Threshold;

[TestMethod]
public void CalculateDiscount_WhenSubtotalIsBelowThreshold_ReturnsZero()
{
// Arrange
const decimal subtotal = 999.99m;

// Act
var discount = DiscountPolicy.CalculateDiscount(subtotal);

// Assert - in MSTest the order is (expected, actual)
Assert.AreEqual(0m, discount);
}

[TestMethod]
public void CalculateDiscount_WhenSubtotalIsExactlyThreshold_AppliesDiscount()
{
var discount = DiscountPolicy.CalculateDiscount(_threshold);

Assert.AreEqual(100m, discount);
}

// [DataRow] is like xUnit [InlineData]. MSTest has no separate [Theory] attribute.
[TestMethod]
[DataRow(1_000.00, 100.00)]
[DataRow(2_500.00, 250.00)]
[DataRow(999.99, 0.00)]
public void CalculateDiscount_ReturnsExpectedAmount(double subtotal, double expected)
{
// Attributes do not accept decimal; we take double and convert to decimal
// (xUnit and NUnit have the same limitation).
var discount = DiscountPolicy.CalculateDiscount((decimal)subtotal);

Assert.AreEqual((decimal)expected, discount);
}

[TestMethod]
public void CalculateDiscount_WhenSubtotalIsNegative_ThrowsArgumentOutOfRange()
{
// ThrowsExactly does NOT accept derived exception types (like xUnit Assert.Throws).
var exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(
() => DiscountPolicy.CalculateDiscount(-1m));

Assert.AreEqual("subtotal", exception.ParamName);
}
}

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

using M01.Foundations.Domain;

namespace M01.Foundations.NUnitExamples;

[TestFixture]
[Category("Unit")]
public class DiscountPolicyNUnitTests
{
private decimal _threshold;

// [SetUp] is like the xUnit constructor / MSTest [TestInitialize]
[SetUp]
public void SetUp() => _threshold = DiscountPolicy.Threshold;

[Test]
public void CalculateDiscount_WhenSubtotalIsBelowThreshold_ReturnsZero()
{
// Arrange
const decimal subtotal = 999.99m;

// Act
var discount = DiscountPolicy.CalculateDiscount(subtotal);

// Assert - in NUnit the ACTUAL value comes first, then the constraint
Assert.That(discount, Is.EqualTo(0m));
}

[Test]
public void CalculateDiscount_WhenSubtotalIsExactlyThreshold_AppliesDiscount()
{
var discount = DiscountPolicy.CalculateDiscount(_threshold);

Assert.That(discount, Is.EqualTo(100m));
}

// [TestCase] is like xUnit [Theory] + [InlineData], and MSTest [DataRow]
[TestCase(1_000.00, 100.00)]
[TestCase(2_500.00, 250.00)]
[TestCase(999.99, 0.00)]
public void CalculateDiscount_ReturnsExpectedAmount(double subtotal, double expected)
{
var discount = DiscountPolicy.CalculateDiscount((decimal)subtotal);

Assert.That(discount, Is.EqualTo((decimal)expected));
}

[Test]
public void CalculateDiscount_WhenSubtotalIsNegative_ThrowsArgumentOutOfRange()
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(
() => DiscountPolicy.CalculateDiscount(-1m));

Assert.That(exception!.ParamName, Is.EqualTo("subtotal"));
}
}

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:

dotnet test M01.Foundations.MSTestExamples
dotnet test M01.Foundations.NUnitExamples

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:

  1. xUnit creates a new instance of the test class for every test.
  2. MSTest also creates a new instance for every test by default.
  3. 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

  1. MSTest, NUnit and xUnit express the same test ideas with different attributes; the Arrange-Act-Assert logic does not change.
  2. The framework package defines tests; the adapter lets VSTest discover and run them.
  3. Watch the assertion order: (expected, actual) in xUnit and MSTest, Assert.That(actual, constraint) in NUnit.
  4. Attribute arguments cannot be decimal in any of the three frameworks, so parameterized tests take double and cast.
  5. NUnit shares one test class instance across tests by default; reset state in [SetUp] instead of relying on fields.