Skip to main content

.NET Testing with xUnit and Best Practices

Write comprehensive .NET tests with xUnit — unit tests, integration tests with WebApplicationFactory, fixtures, and NSubstitute mocking.

Füllen Sie die Platzhalter aus

Edit the values, then copy your finished prompt.

Ihr Prompt
prompt.txt

                                

What this prompt does

This prompt sets up a full xUnit testing strategy for a [dotnet_version] [project_type] app. It is structured as eight steps so the model covers the whole pyramid rather than just unit tests: project layout ([test_structure]), unit tests for [unit_test_targets] using [mock_library], integration tests with WebApplicationFactory against [integration_scenarios], collection fixtures for [shared_resources], parameterized Theory tests for [theory_scenarios], custom assertions for [custom_assertions], naming via [naming_convention], and CI wiring with [ci_tool].

The variables control scope and realism. [test_db_approach] decides whether integration tests hit a real database (for example Testcontainers) or an in-memory substitute, which changes how much you can trust them. [shared_resources] and [mock_library] keep the suite fast and consistent, while [naming_convention] makes failures readable. Asking for [example_count] examples per area stops the answer from being abstract and gives you concrete templates to copy. Splitting the work across [test_structure] also signals intent: unit, integration, and architecture tests have different speeds and failure modes, and keeping them in separate projects lets you run the fast ones constantly and the slow ones in CI.

When to use it

  • Standing up a test suite from scratch on a new .NET service
  • You want integration tests that exercise real database and auth flows, not just mocked happy paths
  • Adding collection fixtures so expensive setup like a database container is shared across classes
  • Introducing parameterized Theory tests to cover many input combinations cleanly
  • You need consistent test naming and folder structure mirroring the source project
  • Wiring tests into [ci_tool] with coverage and parallel execution

Example output

Expect a layered plan with code: a solution structure showing [test_structure], several Arrange-Act-Assert unit tests using [mock_library], a WebApplicationFactory-based integration test class, a collection fixture for [shared_resources], InlineData examples, custom assertion extensions, and a CI config snippet. Each section is short code plus explanation rather than one giant test file.

Pro tips

  • Set [test_db_approach] deliberately — Testcontainers gives real confidence but needs Docker in CI; in-memory is faster but misses provider-specific behavior
  • Name your real [unit_test_targets] (validators, value objects, services) so the examples match your domain instead of generic calculators
  • Keep [mock_library] consistent with your team; mixing NSubstitute and Moq in one suite is a common source of confusion
  • Use [naming_convention] like MethodName_StateUnderTest_ExpectedBehavior so a failing test name alone tells you what broke
  • Push expensive setup into [shared_resources] collection fixtures; spinning up a container per test class is the usual cause of a slow suite
  • If parallel execution causes flaky integration tests, ask the model how to isolate the [test_db_approach] per test or reset state between runs
  • Reach for Theory and InlineData on [theory_scenarios] like boundary values; one parameterized method beats ten near-identical copies and keeps the failure output specific to the failing input
  • Build the custom assertions for [custom_assertions] early; a clear failure message on a Result<T> or validation collection saves far more debugging time than it costs to write

Frequently Asked Questions

Does this prompt use real databases or mocks for integration tests?
It depends on the `[test_db_approach]` you supply. With Testcontainers you get a real SQL Server (or other) instance per run, which tests provider-specific behavior. With an in-memory provider you trade fidelity for speed, so choose based on whether you need to trust database queries.
Why does it recommend WebApplicationFactory for integration tests?
WebApplicationFactory boots your ASP.NET Core app in-memory with the real DI container and middleware pipeline, so you test endpoints close to production. It lets you exercise `[integration_scenarios]` like API responses and authentication flows without deploying, while still allowing you to override services for tests.
Can I use Moq instead of NSubstitute as the mocking library?
Yes. Set `[mock_library]` to whatever your team uses and the generated unit tests will adapt the syntax accordingly. The Arrange-Act-Assert structure is the same; only the mock setup and verification calls differ between libraries.
How does it keep a growing test suite fast?
It pushes expensive setup into collection fixtures for `[shared_resources]` so a database container or seeded factory is created once and shared. Combined with parallel execution settings in `[ci_tool]`, this prevents per-class setup from dominating run time as the suite grows.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Mehr in C# & .NET Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support