Unit Testing in C#
  • Unit testing in C#
  • Unit testing
    • What to test
    • When to test
    • Qualities of a good unit test suite
    • Qualities of a good unit test
    • Dealing with dependencies
    • Running the tests
  • NUnit
    • Quick glance at NUnit
    • Creating a NUnit test project
    • Anatomy of a test fixture
    • Lifecycle of a test fixture
    • Assertions
    • Asynchronous executions
    • Parameterized tests
    • Assumptions
    • Describing your tests
  • Moq
    • Quick glance at Moq
    • Method arguments
    • Method calls
    • Properties
    • Results
    • Callbacks
    • Exceptions
    • Events
    • Verifications
    • Base class
    • Mock customization
    • Implicit mocks
    • Mock repository
    • Custom matchers
    • Multiple interfaces
    • Protected members
    • Generic methods
    • Delegates
  • AutoFixture
    • Quick glance at AutoFixture
    • Fixture
    • Create and Build
    • Type customization
    • Data annotations
    • Default configurations
    • Building custom types
    • Relays
    • Tricks
    • Idioms
    • Integration with NUnit
    • Integration with Moq
    • Combining AutoFixture with NUnit and Moq
    • Extending AutoFixture
  • Advanced topics
    • Testing HttpClient
Powered by GitBook
On this page
  1. NUnit

Anatomy of a test fixture

We already saw that a test fixture is a class decorated with the TestFixture attribute and tests are public methods decorated with the Test attribute.

Even by most conservative estimations, test fixture classes tend to be multiple times bigger than the tested component. For this reason, it's better to structure the test fixtures to increase readibility.

Following the common C# coding style, a test fixture should be structured as follow

  • private fields

  • test fixture constructor

  • test fixture set up methods

  • tests targeting constructors of the system under test

  • tests targeting methods of the system under tests, grouped by method

  • tests targeting properties of the system under tests, grouped by properties

  • test fixture tear-down methods

Additionally, local helper methods should be placed next to the method using it. If a helper method is shared by multiple tests, it should be placed at the end of the class.

PreviousCreating a NUnit test projectNextLifecycle of a test fixture

Last updated 4 years ago