All Projects → ardalis → TestPatterns

ardalis / TestPatterns

Licence: MIT License
Examples of approaches to unit testing different kinds of code in C#.

Programming Languages

C#
18002 projects

Test Patterns

Examples of approaches to unit testing different kinds of code in C#. These examples start with simple cases and move on to more complex patterns.

Give a Star!

If you like or are using this project to learn, please give it a star to help others find it. Thanks!

State-Based Testing

The simplest kinds of unit tests create collaborators, call methods, and inspect the results. These work best with public methods with non-void return signatures and no side effects.

Resources and Practice

Tests should follow SOLID principles, including Single Responsibility. Grouping tests by the method being tested helps achieve this. Read the following naming convention for a proven approach to organizing and naming your tests. Also, the FizzBuzz and String Calculator katas are great to use when first learning state-based testing.

Behavior-Based Testing (with Mocks)

When you can't evaluate whether a method did the right thing by looking at the state it returns or modifies publicly, another approach is to test the behavior of the method itself. This is often called "behavior-based" testing and frequently uses mock objects (or spies or fakes or other techniques) to capture behavior that took place within the method being tested. Collectively these different techniques are known as test doubles.

Resources and Practice

Testing Hard-To-Access State

Confirming the state of private or internal values when testing from another class and assembly can be a challenge. There are some patterns that can be used to overcome these challenges in most cases.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].