C# xUnit Testing

C# xUnit Testing

Guides the AI to write effective xUnit unit tests in C#, covering project setup, test structure, data-driven theories and assertions.

Skill files1 file
SKILL.md69 lines
1
---
2
name: csharp-xunit
3
description: 'Get best practices for XUnit unit testing, including data-driven tests'
4
---
5
6
# XUnit Best Practices
7
8
Your goal is to help me write effective unit tests with XUnit, 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 Microsoft.NET.Test.Sdk, xunit, and xunit.runner.visualstudio packages
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
17
## Test Structure
18
19
- No test class attributes required (unlike MSTest/NUnit)
20
- Use fact-based tests with `[Fact]` attribute for simple tests
21
- Follow the Arrange-Act-Assert (AAA) pattern
22
- Name tests using the pattern `MethodName_Scenario_ExpectedBehavior`
23
- Use constructor for setup and `IDisposable.Dispose()` for teardown
24
- Use `IClassFixture<T>` for shared context between tests in a class
25
- Use `ICollectionFixture<T>` for shared context between multiple test classes
26
27
## Standard Tests
28
29
- Keep tests focused on a single behavior
30
- Avoid testing multiple behaviors in one test method
31
- Use clear assertions that express intent
32
- Include only the assertions needed to verify the test case
33
- Make tests independent and idempotent (can run in any order)
34
- Avoid test interdependencies
35
36
## Data-Driven Tests
37
38
- Use `[Theory]` combined with data source attributes
39
- Use `[InlineData]` for inline test data
40
- Use `[MemberData]` for method-based test data
41
- Use `[ClassData]` for class-based test data
42
- Create custom data attributes by implementing `DataAttribute`
43
- Use meaningful parameter names in data-driven tests
44
45
## Assertions
46
47
- Use `Assert.Equal` for value equality
48
- Use `Assert.Same` for reference equality
49
- Use `Assert.True`/`Assert.False` for boolean conditions
50
- Use `Assert.Contains`/`Assert.DoesNotContain` for collections
51
- Use `Assert.Matches`/`Assert.DoesNotMatch` for regex pattern matching
52
- Use `Assert.Throws<T>` or `await Assert.ThrowsAsync<T>` to test exceptions
53
- Use fluent assertions library for more readable assertions
54
55
## Mocking and Isolation
56
57
- Consider using Moq or NSubstitute alongside XUnit
58
- Mock dependencies to isolate units under test
59
- Use interfaces to facilitate mocking
60
- Consider using a DI container for complex test setups
61
62
## Test Organization
63
64
- Group tests by feature or component
65
- Use `[Trait("Category", "CategoryName")]` for categorization
66
- Use collection fixtures to group tests with shared dependencies
67
- Consider output helpers (`ITestOutputHelper`) for test diagnostics
68
- Skip tests conditionally with `Skip = "reason"` in fact/theory attributes
69

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

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 6,373 prompts

More by @OpenSourceBuffGuy

See all
Website Clone Architect prompt image

Website Clone Architect

Reverse-engineers a website section by section, writes component specs, and hands them to parallel builder agents in git worktrees to rebuild it.

agent-skill, orchestrator, parallel-agents, multi-agent, plan-then-execute, web-development, frontend, codebase-analysis, scaffolding, css, html, nextjs, typescript, git, mcp, claude-code, for-developers, for-designers, long-form, advanced, technical-spec, task-decomposition, structured-output, design-systems, ui-ux, tool-use, checklist, report, vibe-coding

Italian Terrace Cafe Candid Portrait prompt image

Italian Terrace Cafe Candid Portrait

Generates a photorealistic candid shot of a woman in a white corset dress laughing at a sunny hotel terrace cafe, written as detailed JSON.

image-prompt, json-prompt, structured-output, photorealistic, portrait, woman, candid, fashion, cottagecore, natural-light, golden-hour, bokeh, summer, travel, cityscape, mountains, medium-shot, vibrant-colors, for-photographers, nano-banana, fashion-shoot, luxury

Night Street Photo With Leashed Cat prompt image

Night Street Photo With Leashed Cat

Generates a flash-lit night street photo of a blonde woman in a retro track jacket walking a hissing tabby cat on a leash.

image-prompt, json-prompt, nano-banana, structured-output, portrait, woman, animals, pet-portrait, night-scene, street-photography, candid, editorial, fashion, dramatic-lighting, photorealistic, vertical-format, medium-shot, graffiti, for-photographers, for-content-creators

1 view

Reviews

Loading reviews…

Discussion

Loading comments…