.NET Test Framework Reference
Language-specific detection patterns for .NET test frameworks (MSTest, xUnit, NUnit, TUnit).
Test File Identification
| Framework |
Test class markers |
Test method markers |
| MSTest |
[TestClass] |
[TestMethod], [DataTestMethod] |
| xUnit |
(none — convention-based) |
[Fact], [Theory] |
| NUnit |
[TestFixture] |
[Test], [TestCase], [TestCaseSource] |
| TUnit |
[ClassDataSource] |
[Test] |
Assertion APIs by Framework
| Category |
MSTest |
xUnit |
NUnit |
| Equality |
Assert.AreEqual |
Assert.Equal |
Assert.That(x, Is.EqualTo(y)) |
| Boolean |
Assert.IsTrue / Assert.IsFalse |
Assert.True / Assert.False |
Assert.That(x, Is.True) |
| Null |
Assert.IsNull / Assert.IsNotNull |
Assert.Null / Assert.NotNull |
Assert.That(x, Is.Null) |
| Exception |
Assert.Throws<T>() / Assert.ThrowsExactly<T>() |
Assert.Throws<T>() |
Assert.That(() => ..., Throws.TypeOf<T>()) |
| Collection |
CollectionAssert.Contains |
Assert.Contains |
Assert.That(col, Has.Member(x)) |
| String |
StringAssert.Contains |
Assert.Contains(str, sub) |
Assert.That(str, Does.Contain(sub)) |
| Type |
Assert.IsInstanceOfType |
Assert.IsAssignableFrom |
Assert.That(x, Is.InstanceOf<T>()) |
| Inconclusive |
Assert.Inconclusive() |
skip via [Fact(Skip)] |
Assert.Inconclusive() |
| Fail |
Assert.Fail() |
Assert.Fail() (.NET 10+) |
Assert.Fail() |
Third-party assertion libraries: Should* (Shouldly), .Should() (FluentAssertions / AwesomeAssertions), Verify() (Verify).
Sleep/Delay Patterns
| Pattern |
Example |
| Thread sleep |
Thread.Sleep(2000) |
| Task delay |
await Task.Delay(1000) |
| SpinWait |
SpinWait.SpinUntil(() => condition, timeout) |
Skip/Ignore Annotations
| Framework |
Annotation |
With reason |
| MSTest |
[Ignore] |
[Ignore("reason")] |
| xUnit |
[Fact(Skip = "reason")] |
(reason is required) |
| NUnit |
[Ignore("reason")] |
(reason is required) |
| TUnit |
[Skip("reason")] |
(reason is required) |
| Conditional |
#if false / #if NEVER |
(no reason possible) |
Exception Handling — Idiomatic Alternatives
When a test uses try/catch to verify exceptions, suggest the framework-native alternative:
MSTest:
// Instead of try/catch (matches exact type):
var ex = Assert.ThrowsExactly<InvalidOperationException>(
() => processor.ProcessOrder(emptyOrder));
Assert.AreEqual("Order must contain at least one item", ex.Message);
// Or (also matches derived types):
var ex = Assert.Throws<InvalidOperationException>(
() => processor.ProcessOrder(emptyOrder));
Assert.AreEqual("Order must contain at least one item", ex.Message);
xUnit:
var ex = Assert.Throws<InvalidOperationException>(
() => processor.ProcessOrder(emptyOrder));
Assert.Equal("Order must contain at least one item", ex.Message);
NUnit:
var ex = Assert.Throws<InvalidOperationException>(
() => processor.ProcessOrder(emptyOrder));
Assert.That(ex.Message, Is.EqualTo("Order must contain at least one item"));
Mystery Guest — Common .NET Patterns
| Smell indicator |
What to look for |
| File system |
File.ReadAllText, File.Exists, File.WriteAllBytes, Directory.GetFiles, Path.Combine with hard-coded paths |
| Database |
SqlConnection, DbContext (without in-memory provider), SqlCommand |
| Network |
HttpClient without HttpMessageHandler override, WebRequest, TcpClient |
| Environment |
Environment.GetEnvironmentVariable, Environment.CurrentDirectory |
| Acceptable |
MemoryStream, StringReader, InMemory database providers, custom DelegatingHandler |
Integration Test Markers
Recognize these as integration tests (adjust smell severity accordingly):
- Class name contains
Integration, E2E, EndToEnd, or Acceptance
[TestCategory("Integration")] (MSTest)
[Trait("Category", "Integration")] (xUnit)
[Category("Integration")] (NUnit)
- Project name ending in
.IntegrationTests or .E2ETests
Setup/Teardown Methods
| Framework |
Setup |
Teardown |
| MSTest |
[TestInitialize] or constructor |
[TestCleanup] or IDisposable.Dispose / IAsyncDisposable.DisposeAsync |
| xUnit |
constructor |
IDisposable.Dispose / IAsyncDisposable.DisposeAsync |
| NUnit |
[SetUp] |
[TearDown] |
| MSTest (class) |
[ClassInitialize] |
[ClassCleanup] |
| NUnit (class) |
[OneTimeSetUp] |
[OneTimeTearDown] |
| xUnit (class) |
IClassFixture<T> |
fixture's Dispose |
1---2name: dotnet-test-frameworks3description: Reference data for .NET test framework detection patterns, assertion APIs, skip annotations, setup/teardown methods, and common test smell indicators across MSTest, xUnit, NUnit, and TUnit. Loaded by test analysis skills (test-anti-patterns) as framework-specific lookup tables.4license: MIT5---67# .NET Test Framework Reference89Language-specific detection patterns for .NET test frameworks (MSTest, xUnit, NUnit, TUnit).1011## Test File Identification1213| Framework | Test class markers | Test method markers |14| --------- | ------------------ | ------------------- |15| MSTest | `[TestClass]` | `[TestMethod]`, `[DataTestMethod]` |16| xUnit | *(none — convention-based)* | `[Fact]`, `[Theory]` |17| NUnit | `[TestFixture]` | `[Test]`, `[TestCase]`, `[TestCaseSource]` |18| TUnit | `[ClassDataSource]` | `[Test]` |1920## Assertion APIs by Framework2122| Category | MSTest | xUnit | NUnit |23| -------- | ------ | ----- | ----- |24| Equality | `Assert.AreEqual` | `Assert.Equal` | `Assert.That(x, Is.EqualTo(y))` |25| Boolean | `Assert.IsTrue` / `Assert.IsFalse` | `Assert.True` / `Assert.False` | `Assert.That(x, Is.True)` |26| Null | `Assert.IsNull` / `Assert.IsNotNull` | `Assert.Null` / `Assert.NotNull` | `Assert.That(x, Is.Null)` |27| Exception | `Assert.Throws<T>()` / `Assert.ThrowsExactly<T>()` | `Assert.Throws<T>()` | `Assert.That(() => ..., Throws.TypeOf<T>())` |28| Collection | `CollectionAssert.Contains` | `Assert.Contains` | `Assert.That(col, Has.Member(x))` |29| String | `StringAssert.Contains` | `Assert.Contains(str, sub)` | `Assert.That(str, Does.Contain(sub))` |30| Type | `Assert.IsInstanceOfType` | `Assert.IsAssignableFrom` | `Assert.That(x, Is.InstanceOf<T>())` |31| Inconclusive | `Assert.Inconclusive()` | *skip via `[Fact(Skip)]`* | `Assert.Inconclusive()` |32| Fail | `Assert.Fail()` | `Assert.Fail()` (.NET 10+) | `Assert.Fail()` |3334Third-party assertion libraries: `Should*` (Shouldly), `.Should()` (FluentAssertions / AwesomeAssertions), `Verify()` (Verify).3536## Sleep/Delay Patterns3738| Pattern | Example |39| ------- | ------- |40| Thread sleep | `Thread.Sleep(2000)` |41| Task delay | `await Task.Delay(1000)` |42| SpinWait | `SpinWait.SpinUntil(() => condition, timeout)` |4344## Skip/Ignore Annotations4546| Framework | Annotation | With reason |47| --------- | ---------- | ----------- |48| MSTest | `[Ignore]` | `[Ignore("reason")]` |49| xUnit | `[Fact(Skip = "reason")]` | *(reason is required)* |50| NUnit | `[Ignore("reason")]` | *(reason is required)* |51| TUnit | `[Skip("reason")]` | *(reason is required)* |52| Conditional | `#if false` / `#if NEVER` | *(no reason possible)* |5354## Exception Handling — Idiomatic Alternatives5556When a test uses `try`/`catch` to verify exceptions, suggest the framework-native alternative:5758**MSTest:**5960```csharp61// Instead of try/catch (matches exact type):62var ex = Assert.ThrowsExactly<InvalidOperationException>(63 () => processor.ProcessOrder(emptyOrder));64Assert.AreEqual("Order must contain at least one item", ex.Message);6566// Or (also matches derived types):67var ex = Assert.Throws<InvalidOperationException>(68 () => processor.ProcessOrder(emptyOrder));69Assert.AreEqual("Order must contain at least one item", ex.Message);70```7172**xUnit:**7374```csharp75var ex = Assert.Throws<InvalidOperationException>(76 () => processor.ProcessOrder(emptyOrder));77Assert.Equal("Order must contain at least one item", ex.Message);78```7980**NUnit:**8182```csharp83var ex = Assert.Throws<InvalidOperationException>(84 () => processor.ProcessOrder(emptyOrder));85Assert.That(ex.Message, Is.EqualTo("Order must contain at least one item"));86```8788## Mystery Guest — Common .NET Patterns8990| Smell indicator | What to look for |91| --------------- | ---------------- |92| File system | `File.ReadAllText`, `File.Exists`, `File.WriteAllBytes`, `Directory.GetFiles`, `Path.Combine` with hard-coded paths |93| Database | `SqlConnection`, `DbContext` (without in-memory provider), `SqlCommand` |94| Network | `HttpClient` without `HttpMessageHandler` override, `WebRequest`, `TcpClient` |95| Environment | `Environment.GetEnvironmentVariable`, `Environment.CurrentDirectory` |96| Acceptable | `MemoryStream`, `StringReader`, `InMemory` database providers, custom `DelegatingHandler` |9798## Integration Test Markers99100Recognize these as integration tests (adjust smell severity accordingly):101102- Class name contains `Integration`, `E2E`, `EndToEnd`, or `Acceptance`103- `[TestCategory("Integration")]` (MSTest)104- `[Trait("Category", "Integration")]` (xUnit)105- `[Category("Integration")]` (NUnit)106- Project name ending in `.IntegrationTests` or `.E2ETests`107108## Setup/Teardown Methods109110| Framework | Setup | Teardown |111| --------- | ----- | -------- |112| MSTest | `[TestInitialize]` or constructor | `[TestCleanup]` or `IDisposable.Dispose` / `IAsyncDisposable.DisposeAsync` |113| xUnit | constructor | `IDisposable.Dispose` / `IAsyncDisposable.DisposeAsync` |114| NUnit | `[SetUp]` | `[TearDown]` |115| MSTest (class) | `[ClassInitialize]` | `[ClassCleanup]` |116| NUnit (class) | `[OneTimeSetUp]` | `[OneTimeTearDown]` |117| xUnit (class) | `IClassFixture<T>` | fixture's `Dispose` |