Reducing Code Redundancy Between our Test Cases
Explore techniques to reduce code redundancy in NUnit unit tests by centralizing common Arrange steps in a Setup method. Understand how NUnit runs Setup before each test, enabling cleaner, more maintainable, and readable test methods focused on Act and Assert steps.
Introduction
Every test case requires the Arrange-Act-Assert steps. Most of the time, arranging data is the same between test methods. If this is the case, you will need to set up your test methods. Test method setup is the process of arranging data once so it may subsequently be used by various test methods.
Demonstrating the problem
Suppose you have an application code that contains a class called HundredMeterSprint. This class stores a list of sprint competitor times. These values represent the time taken to complete a hundred-meter sprint by various athletes. The class has the three following public methods:
AverageTime(line 17):