Your goal is to help me write effective unit tests with TUnit, covering both standard and data-driven testing approaches.
Project Setup
Use a separate test project with naming convention [ProjectName].Tests
Reference TUnit package and TUnit.Assertions for fluent assertions
Create test classes that match the classes being tested (e.g., CalculatorTests for Calculator)
Use .NET SDK test commands: dotnet test for running tests
TUnit requires .NET 8.0 or higher
Test Structure
No test class attributes required (like xUnit/NUnit)
Use [Test] attribute for test methods (not [Fact] like xUnit)
Follow the Arrange-Act-Assert (AAA) pattern
Name tests using the pattern MethodName_Scenario_ExpectedBehavior
Use lifecycle hooks: [Before(Test)] for setup and [After(Test)] for teardown
Use [Before(Class)] and [After(Class)] for shared context between tests in a class
Use [Before(Assembly)] and [After(Assembly)] for shared context across test classes
TUnit supports advanced lifecycle hooks like [Before(TestSession)] and [After(TestSession)]
Standard Tests
Keep tests focused on a single behaviour
Avoid testing multiple behaviours in one test method
Use TUnit's fluent assertion syntax with await Assert.That()
Include only the assertions needed to verify the test case
Make tests independent and idempotent (can run in any order)
Avoid test interdependencies (use [DependsOn] attribute if needed)
Data-Driven Tests
Use [Arguments] attribute for inline test data (equivalent to xUnit's [InlineData])
Use [MethodData] for method-based test data (equivalent to xUnit's [MemberData])
Use [ClassData] for class-based test data
Create custom data sources by implementing ITestDataSource
Use meaningful parameter names in data-driven tests
Multiple [Arguments] attributes can be applied to the same test method
Assertions
Use await Assert.That(value).IsEqualTo(expected) for value equality
Use await Assert.That(value).IsSameReferenceAs(expected) for reference equality
Use await Assert.That(value).IsTrue() or await Assert.That(value).IsFalse() for boolean conditions
Use await Assert.That(collection).Contains(item) or await Assert.That(collection).DoesNotContain(item) for collections
Use await Assert.That(value).Matches(pattern) for regex pattern matching
Use await Assert.That(action).Throws<TException>() or await Assert.That(asyncAction).ThrowsAsync<TException>() to test exceptions
Chain assertions with .And operator: await Assert.That(value).IsNotNull().And.IsEqualTo(expected)
Use .Or operator for alternative conditions: await Assert.That(value).IsEqualTo(1).Or.IsEqualTo(2)
Use .Within(tolerance) for DateTime and numeric comparisons with tolerance
All assertions are asynchronous and must be awaited
Advanced Features
Use [Repeat(n)] to repeat tests multiple times
Use [Retry(n)] for automatic retry on failure
Use [ParallelLimit<T>] to control parallel execution limits
Use [Skip("reason")] to skip tests conditionally
Use [DependsOn(nameof(OtherTest))] to create test dependencies
Use [Timeout(milliseconds)] to set test timeouts
Create custom attributes by extending TUnit's base attributes
Test Organization
Group tests by feature or component
Use [Category("CategoryName")] for test categorisation
Use [DisplayName("Custom Test Name")] for custom test names
Consider using TestContext for test diagnostics and information
Use conditional attributes like custom [WindowsOnly] for platform-specific tests
Performance and Parallel Execution
TUnit runs tests in parallel by default (unlike xUnit which requires explicit configuration)
Use [NotInParallel] to disable parallel execution for specific tests
Use [ParallelLimit<T>] with custom limit classes to control concurrency
Tests within the same class run sequentially by default
Use [Repeat(n)] with [ParallelLimit<T>] for load testing scenarios
Migration from xUnit
Replace [Fact] with [Test]
Replace [Theory] with [Test] and use [Arguments] for data
Replace [InlineData] with [Arguments]
Replace [MemberData] with [MethodData]
Replace Assert.Equal with await Assert.That(actual).IsEqualTo(expected)
Replace Assert.True with await Assert.That(condition).IsTrue()
Replace Assert.Throws<T> with await Assert.That(action).Throws<T>()
Replace constructor/IDisposable with [Before(Test)]/[After(Test)]
Replace IClassFixture<T> with [Before(Class)]/[After(Class)]
Why TUnit over xUnit?
TUnit offers a modern, fast, and flexible testing experience with advanced features not present in xUnit, such as asynchronous assertions, more refined lifecycle hooks, and improved data-driven testing capabilities. TUnit's fluent assertions provide clearer and more expressive test validation, making it especially suitable for complex .NET projects.
1---2name: csharp-tunit3description: Get best practices for TUnit unit testing, including data-driven tests4---56# TUnit Best Practices78Your goal is to help me write effective unit tests with TUnit, covering both standard and data-driven testing approaches.910## Project Setup1112- Use a separate test project with naming convention `[ProjectName].Tests`13- Reference TUnit package and TUnit.Assertions for fluent assertions14- Create test classes that match the classes being tested (e.g., `CalculatorTests` for `Calculator`)15- Use .NET SDK test commands: `dotnet test` for running tests16- TUnit requires .NET 8.0 or higher1718## Test Structure1920- No test class attributes required (like xUnit/NUnit)21- Use `[Test]` attribute for test methods (not `[Fact]` like xUnit)22- Follow the Arrange-Act-Assert (AAA) pattern23- Name tests using the pattern `MethodName_Scenario_ExpectedBehavior`24- Use lifecycle hooks: `[Before(Test)]` for setup and `[After(Test)]` for teardown25- Use `[Before(Class)]` and `[After(Class)]` for shared context between tests in a class26- Use `[Before(Assembly)]` and `[After(Assembly)]` for shared context across test classes27- TUnit supports advanced lifecycle hooks like `[Before(TestSession)]` and `[After(TestSession)]`2829## Standard Tests3031- Keep tests focused on a single behaviour32- Avoid testing multiple behaviours in one test method33- Use TUnit's fluent assertion syntax with `await Assert.That()`34- Include only the assertions needed to verify the test case35- Make tests independent and idempotent (can run in any order)36- Avoid test interdependencies (use `[DependsOn]` attribute if needed)3738## Data-Driven Tests3940- Use `[Arguments]` attribute for inline test data (equivalent to xUnit's `[InlineData]`)41- Use `[MethodData]` for method-based test data (equivalent to xUnit's `[MemberData]`)42- Use `[ClassData]` for class-based test data43- Create custom data sources by implementing `ITestDataSource`44- Use meaningful parameter names in data-driven tests45- Multiple `[Arguments]` attributes can be applied to the same test method4647## Assertions4849- Use `await Assert.That(value).IsEqualTo(expected)` for value equality50- Use `await Assert.That(value).IsSameReferenceAs(expected)` for reference equality51- Use `await Assert.That(value).IsTrue()` or `await Assert.That(value).IsFalse()` for boolean conditions52- Use `await Assert.That(collection).Contains(item)` or `await Assert.That(collection).DoesNotContain(item)` for collections53- Use `await Assert.That(value).Matches(pattern)` for regex pattern matching54- Use `await Assert.That(action).Throws<TException>()` or `await Assert.That(asyncAction).ThrowsAsync<TException>()` to test exceptions55- Chain assertions with `.And` operator: `await Assert.That(value).IsNotNull().And.IsEqualTo(expected)`56- Use `.Or` operator for alternative conditions: `await Assert.That(value).IsEqualTo(1).Or.IsEqualTo(2)`57- Use `.Within(tolerance)` for DateTime and numeric comparisons with tolerance58- All assertions are asynchronous and must be awaited5960## Advanced Features6162- Use `[Repeat(n)]` to repeat tests multiple times63- Use `[Retry(n)]` for automatic retry on failure64- Use `[ParallelLimit<T>]` to control parallel execution limits65- Use `[Skip("reason")]` to skip tests conditionally66- Use `[DependsOn(nameof(OtherTest))]` to create test dependencies67- Use `[Timeout(milliseconds)]` to set test timeouts68- Create custom attributes by extending TUnit's base attributes6970## Test Organization7172- Group tests by feature or component73- Use `[Category("CategoryName")]` for test categorisation74- Use `[DisplayName("Custom Test Name")]` for custom test names75- Consider using `TestContext` for test diagnostics and information76- Use conditional attributes like custom `[WindowsOnly]` for platform-specific tests7778## Performance and Parallel Execution7980- TUnit runs tests in parallel by default (unlike xUnit which requires explicit configuration)81- Use `[NotInParallel]` to disable parallel execution for specific tests82- Use `[ParallelLimit<T>]` with custom limit classes to control concurrency83- Tests within the same class run sequentially by default84- Use `[Repeat(n)]` with `[ParallelLimit<T>]` for load testing scenarios8586## Migration from xUnit8788- Replace `[Fact]` with `[Test]`89- Replace `[Theory]` with `[Test]` and use `[Arguments]` for data90- Replace `[InlineData]` with `[Arguments]`91- Replace `[MemberData]` with `[MethodData]`92- Replace `Assert.Equal` with `await Assert.That(actual).IsEqualTo(expected)`93- Replace `Assert.True` with `await Assert.That(condition).IsTrue()`94- Replace `Assert.Throws<T>` with `await Assert.That(action).Throws<T>()`95- Replace constructor/IDisposable with `[Before(Test)]`/`[After(Test)]`96- Replace `IClassFixture<T>` with `[Before(Class)]`/`[After(Class)]`9798**Why TUnit over xUnit?**99100TUnit offers a modern, fast, and flexible testing experience with advanced features not present in xUnit, such as asynchronous assertions, more refined lifecycle hooks, and improved data-driven testing capabilities. TUnit's fluent assertions provide clearer and more expressive test validation, making it especially suitable for complex .NET projects.
Run npx skillmds@latest add marielynneblock/csharp-tunit in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Get best practices for TUnit unit testing, including data-driven tests It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
MarieLynneBlock (@marielynneblock) published this skill. Their other Agent Skills are listed on their SkillMD profile.