Reducing Code Redundancy Using Many Test Fixtures

Introduction

Test code redundancy is reduced by using the setup method and test method parameterization. The NUnit Setup attribute helps arrange data consistently across unit tests. When two or more unit tests need to arrange the same data, we may use a Setup method for this purpose. The Setup method is called before each test method is run. This helps avoid test code duplication. The NUnit TestCase attribute helps parameterize your test case input and expected data for a single test method.

This lesson details the TestFixture technique that may further reduce test code redundancy. The TestCase attribute parameterizes test methods, and the TestFixture attribute parameterizes the setup method.

The TestFixture attribute

Unit tests are typically expressed in NUnit as test methods. Test methods are methods decorated with the Test attribute. Since NUnit is a testing framework for object-oriented .NET languages, the test methods are always included within a test class. Such classes are called test fixtures. They are designated as such using the TestFixture decoration attribute. This is demonstrated below:

Get hands-on with 1200+ tech skills courses.