Test fixtures

I try to avoid the Arrange-Act-Assert (AAA) pattern for unit tests. I find that with multiple test methods depending on the same starting conditions, the ‘arrange’ code becomes repetitive, which makes tests tedious to write and difficult to maintain.

My preferred approach is to set one test fixture per test class, the test fixture being common for its test methods. In woodwork a fixture keeps a piece in place whilst it is being worked on; similarly, a test fixture keeps an object in a fixed state as the tests are executed.

Most test frameworks allow a method in a test class to be run before each test method is executed. In JUnit, the decorator @Before  designates this method; in MSTest, the attribute [TestInitialize] has the same effect. This method can be used to configure a test fixture as required for the tests in the class.


Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.