Test Fixture Introduction

Duplicated Code is a code smell. Duplication makes the code harder to change since you have to find and modify all instances of the duplicated code. The same argument is also valid for test code.

In this learning hour we write a test fixture and reduce duplication.

Learning Objective

Session Outline

Connect: Duplication

Concept: Test Fixture

A unit test usually has three phases:

It is not uncommon that unit tests for a function or class duplicates code in the Arrange part of the tests. A test fixture allows us to share that code between the tests and thereby avoid duplication.

Concrete: Convert duplication to fixture

The exercise is Supermarket-TestDesign-Kata. Ask the participants to write a test fixture in SuperMarketTest that allows them to remove code duplication in the Arrange part of the existing unit tests. Ask them to expand the test fixture iteratively by adopting it for one new test case at a time.

If you’ve worked on the exercise in pairs, take a few minutes at the end to showcase the various solutions. The git branch “sample_solution” contains one possible solution for the exercise if you want to compare it against yours.

Conclusions

Note down in your own words what a test fixture is and how you can use it to remove duplication.