Choose the existing framework convention
Check whether the project uses MSTest, NUnit, or xUnit and follow its discovery and parameterization conventions. The names and attributes differ, so copying a test from another framework can leave a case undiscovered. Verify that the normal project test command actually runs the new cases before relying on a green summary.
Name tests for the condition and expected behavior, such as an expired token being rejected. Keep expected results explicit rather than recomputing them with production helpers. This makes the test useful documentation and helps reviewers see whether the assertion challenges the requirement or merely repeats the implementation's current behavior.
Keep asynchronous checks observable
Await the Task returned by asynchronous application code before asserting the result or exception. Avoid async void tests because the runner may not observe completion and failures as intended. Use the framework's supported asynchronous exception assertion when the contract requires a failure from the awaited operation under controlled conditions.
Use a clock abstraction or controlled dependency for time and remote calls. Avoid Thread.Sleep as a way to make a rule deterministic. For a cancellation path, arrange the cancellation state explicitly and check the defined result so timing variation on a busy machine does not decide whether the test passes.
Separate object behavior from infrastructure
Test a domain service with small controlled collaborators when the risk is local decision logic. Use integration checks for Entity Framework mappings, provider-specific queries, and transaction behavior. A mocked repository can return representative data, but it does not establish that the real query produces that data in the configured database provider.
Keep fixtures independent and dispose resources using the framework's supported lifecycle. Run tests through the repository's configured runner and SDK. Record any required environment setup, and confirm that a deliberate wrong result fails with a useful message before accepting the suite as evidence for an important behavior change.
Working sheet
| Concern | Prefer | Reason |
|---|---|---|
| Async operation | Awaited Task test | Observe failures |
| Current time | Controlled clock | Stable boundary |
| Data access mapping | Real provider integration | Check actual query behavior |
| Case discovery | Existing framework attributes | Cases actually execute |
Questions about c# unit testing with xUnit
Which C# framework should I choose?
Prefer the repository's established framework unless a concrete limitation justifies a change.
Is a mocked database enough?
It can support local rule tests. Use integration evidence for actual query, mapping, and transaction behavior.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
