csharp-tunit
$
npx mdskill add foxminchan/BookWorm/csharp-tunitProvides best practices for writing TUnit unit tests in C#
- Helps structure unit tests using the Arrange-Act-Assert pattern
- Leverages TUnit and TUnit.Assertions for test creation and assertions
- Follows naming conventions and lifecycle hooks for test organization
- Delivers guidance on standard and data-driven test scenarios
SKILL.md
.github/skills/csharp-tunitView on GitHub ↗
---
name: csharp-tunit
description: "Get best practices for TUnit unit testing, including data-driven tests"
---
# TUnit Best Practices
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].[TestType]` (e.g., `MyApp.UnitTests`)
- 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 `Given[Condition]_When[Action]_Then[ExpectedResult]` for clarity
- 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 behavior
- Avoid testing multiple behaviors in one test method
- Use Shouldly's fluent assertion syntax (e.g., `actual.ShouldBe(expected)`)
- 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 (Shouldly)
- Use `actual.ShouldBe(expected)` for value equality
- Use `actual.ShouldBeSameAs(expected)` for reference equality
- Use `actual.ShouldBeTrue()` or `actual.ShouldBeFalse()` for boolean conditions
- Use `actual.ShouldBeNull()` or `actual.ShouldNotBeNull()` for null checks
- Use `actual.ShouldContain(item)` or `actual.ShouldNotContain(item)` for collections
- Use `actual.ShouldContain("substring")` for string containment
- Use `actual.ShouldMatch(pattern)` for regex pattern matching
- Use `actual.ShouldBeGreaterThan(value)`, `actual.ShouldBeLessThan(value)` for comparisons
- Use `actual.ShouldBeOfType<T>()` for type assertions
- Use `actual.ShouldBeEmpty()` or `actual.ShouldNotBeEmpty()` for collections and strings
- Use `Should.Throw<TException>(() => action)` for sync exception testing
- Use `Should.Throw<TException>(async () => await asyncAction)` for async exception testing
- Use `Should.NotThrow(() => action)` to verify no exception is thrown
- All Shouldly assertions are synchronous (no `await` needed except for async exception testing)
## 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 categorization
- 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 `actual.ShouldBe(expected)`
- Replace `Assert.True` with `condition.ShouldBeTrue()`
- Replace `Assert.Throws<T>` with `Should.Throw<T>(() => action)`
- 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.
More from foxminchan/BookWorm
- aspire>-
- aspire-deployment**WORKFLOW SKILL** — Deploy Aspire apps from AppHost models to Docker Compose, Kubernetes, Azure, or AWS. WHEN: "deploy Aspire app", "publish Aspire artifacts", "deploy to Azure Container Apps", "generate Kubernetes artifacts", "tear down Aspire deployment". INVOKES: aspire CLI, Aspire docs, target cloud/container CLIs. FOR SINGLE OPERATIONS: use generic Azure, Kubernetes, Docker, or AWS tools only when no Aspire AppHost exists.
- aspire-init>-
- aspire-monitoring>-
- aspire-orchestration>-
- aspireify>-
- book-catalogSearch and recommend books from BookWorm's catalog. Use when customers ask about finding books, getting personalized recommendations, exploring genres, discovering authors, or comparing titles.
- catalog-documentation-creatorGenerates EventCatalog documentation files (services, agents, events, commands, queries, domains, flows, channels, containers) with correct frontmatter, folder structure, and best practices. Use when user asks to "document a service", "document an agent", "document an AI agent", "create EventCatalog files", "add an event to the catalog", "document my architecture", "generate catalog documentation", "create documentation for my microservice", or "document a database".
- code-to-catalogTurns a codebase into EventCatalog documentation through an evidence-based interview. Scans the code first, proposes an architectural model (domains, services, agents, messages, channels), grills the user on the structural decisions, produces a reviewable plan file, then hands off to catalog-documentation-creator. Use when user says "document my codebase in EventCatalog", "turn this repo into a catalog", "model my code as a catalog", "document my agents", "document my AI agents", "grill me on my architecture", "update my catalog from the code", "reconcile my catalog with my code", or "I don't know where to start documenting this codebase". Works for brand-new catalogs AND for updating existing catalogs that have drifted from the code.
- flow-wizardGuides users through documenting business flows step-by-step in EventCatalog, including services, agents, messages, actors, and external systems. Use when user asks to "document a flow", "map a business process", "create a flow diagram", "walk through a process", "document an end-to-end flow", "map an agent workflow", or "map out how something works in my architecture".