C# NUnit Testing

C# NUnit Testing

Gives the AI NUnit best practices for C# unit tests: project setup, structure, assertions and data-driven tests with TestCase and TestCaseSource.

Skill files1 file
SKILL.md72 lines
1
---
2
name: csharp-nunit
3
description: 'Get best practices for NUnit unit testing, including data-driven tests'
4
---
5
6
# NUnit Best Practices
7
8
Your goal is to help me write effective unit tests with NUnit, 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, NUnit, and NUnit3TestAdapter 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
- Apply `[TestFixture]` attribute to test classes
20
- Use `[Test]` attribute for test methods
21
- Follow the Arrange-Act-Assert (AAA) pattern
22
- Name tests using the pattern `MethodName_Scenario_ExpectedBehavior`
23
- Use `[SetUp]` and `[TearDown]` for per-test setup and teardown
24
- Use `[OneTimeSetUp]` and `[OneTimeTearDown]` for per-class setup and teardown
25
- Use `[SetUpFixture]` for assembly-level setup and teardown
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 `[TestCase]` for inline test data
39
- Use `[TestCaseSource]` for programmatically generated test data
40
- Use `[Values]` for simple parameter combinations
41
- Use `[ValueSource]` for property or method-based data sources
42
- Use `[Random]` for random numeric test values
43
- Use `[Range]` for sequential numeric test values
44
- Use `[Combinatorial]` or `[Pairwise]` for combining multiple parameters
45
46
## Assertions
47
48
- Use `Assert.That` with constraint model (preferred NUnit style)
49
- Use constraints like `Is.EqualTo`, `Is.SameAs`, `Contains.Item`
50
- Use `Assert.AreEqual` for simple value equality (classic style)
51
- Use `CollectionAssert` for collection comparisons
52
- Use `StringAssert` for string-specific assertions
53
- Use `Assert.Throws<T>` or `Assert.ThrowsAsync<T>` to test exceptions
54
- Use descriptive messages in assertions for clarity on failure
55
56
## Mocking and Isolation
57
58
- Consider using Moq or NSubstitute alongside NUnit
59
- Mock dependencies to isolate units under test
60
- Use interfaces to facilitate mocking
61
- Consider using a DI container for complex test setups
62
63
## Test Organization
64
65
- Group tests by feature or component
66
- Use categories with `[Category("CategoryName")]`
67
- Use `[Order]` to control test execution order when necessary
68
- Use `[Author("DeveloperName")]` to indicate ownership
69
- Use `[Description]` to provide additional test information
70
- Consider `[Explicit]` for tests that shouldn't run automatically
71
- Use `[Ignore("Reason")]` to temporarily skip tests
72

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

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 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

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…