C# TUnit Testing

C# TUnit Testing

Gives an AI assistant best practices for writing C# unit tests with TUnit, including project setup, lifecycle hooks, assertions and data-driven tests.

Skill files1 file
SKILL.md101 lines
1
---
2
name: csharp-tunit
3
description: 'Get best practices for TUnit unit testing, including data-driven tests'
4
---
5
6
# TUnit Best Practices
7
8
Your goal is to help me write effective unit tests with TUnit, covering both standard and data-driven testing approaches.
9
10
## Project Setup
11
12
- Use a separate test project with naming convention `[ProjectName].Tests`
13
- Reference TUnit package and TUnit.Assertions for fluent assertions
14
- Create test classes that match the classes being tested (e.g., `CalculatorTests` for `Calculator`)
15
- Use .NET SDK test commands: `dotnet test` for running tests
16
- TUnit requires .NET 8.0 or higher
17
18
## Test Structure
19
20
- 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) pattern
23
- Name tests using the pattern `MethodName_Scenario_ExpectedBehavior`
24
- Use lifecycle hooks: `[Before(Test)]` for setup and `[After(Test)]` for teardown
25
- Use `[Before(Class)]` and `[After(Class)]` for shared context between tests in a class
26
- Use `[Before(Assembly)]` and `[After(Assembly)]` for shared context across test classes
27
- TUnit supports advanced lifecycle hooks like `[Before(TestSession)]` and `[After(TestSession)]`
28
29
## Standard Tests
30
31
- Keep tests focused on a single behavior
32
- Avoid testing multiple behaviors in one test method
33
- Use TUnit's fluent assertion syntax with `await Assert.That()`
34
- Include only the assertions needed to verify the test case
35
- Make tests independent and idempotent (can run in any order)
36
- Avoid test interdependencies (use `[DependsOn]` attribute if needed)
37
38
## Data-Driven Tests
39
40
- 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 data
43
- Create custom data sources by implementing `ITestDataSource`
44
- Use meaningful parameter names in data-driven tests
45
- Multiple `[Arguments]` attributes can be applied to the same test method
46
47
## Assertions
48
49
- Use `await Assert.That(value).IsEqualTo(expected)` for value equality
50
- Use `await Assert.That(value).IsSameReferenceAs(expected)` for reference equality
51
- Use `await Assert.That(value).IsTrue()` or `await Assert.That(value).IsFalse()` for boolean conditions
52
- Use `await Assert.That(collection).Contains(item)` or `await Assert.That(collection).DoesNotContain(item)` for collections
53
- Use `await Assert.That(value).Matches(pattern)` for regex pattern matching
54
- Use `await Assert.That(action).Throws<TException>()` or `await Assert.That(asyncAction).ThrowsAsync<TException>()` to test exceptions
55
- 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 tolerance
58
- All assertions are asynchronous and must be awaited
59
60
## Advanced Features
61
62
- Use `[Repeat(n)]` to repeat tests multiple times
63
- Use `[Retry(n)]` for automatic retry on failure
64
- Use `[ParallelLimit<T>]` to control parallel execution limits
65
- Use `[Skip("reason")]` to skip tests conditionally
66
- Use `[DependsOn(nameof(OtherTest))]` to create test dependencies
67
- Use `[Timeout(milliseconds)]` to set test timeouts
68
- Create custom attributes by extending TUnit's base attributes
69
70
## Test Organization
71
72
- Group tests by feature or component
73
- Use `[Category("CategoryName")]` for test categorization
74
- Use `[DisplayName("Custom Test Name")]` for custom test names
75
- Consider using `TestContext` for test diagnostics and information
76
- Use conditional attributes like custom `[WindowsOnly]` for platform-specific tests
77
78
## Performance and Parallel Execution
79
80
- TUnit runs tests in parallel by default (unlike xUnit which requires explicit configuration)
81
- Use `[NotInParallel]` to disable parallel execution for specific tests
82
- Use `[ParallelLimit<T>]` with custom limit classes to control concurrency
83
- Tests within the same class run sequentially by default
84
- Use `[Repeat(n)]` with `[ParallelLimit<T>]` for load testing scenarios
85
86
## Migration from xUnit
87
88
- Replace `[Fact]` with `[Test]`
89
- Replace `[Theory]` with `[Test]` and use `[Arguments]` for data
90
- 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)]`
97
98
**Why TUnit over xUnit?**
99
100
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.
101

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,587 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen, realistic-person

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…