Fork me on GitHub

Friday, May 1, 2009

Build Operate Check Clear - Test Pattern

A common pattern which lot of people including me know and practise but I am writing here as a reference to me as well for people who are not familiar with this.

This is a common pattern for writing tests the idea being initializing the infrastructure and SUT we need for the test and cleaning it up after we test. This pattern applies for any kind of test unit or UI level but the clear phase may depend on the kind of fixture we use. This pattern helps to a level to keep the test reliable by making them independent but this largely depends on how Build and Clear phases are run.

Build - Create the infrastructure we need for the test and initialise system under test (SUT) to a state where we can start testing it. In case of unit test we may set up mocks or objects we want to test, in case of functional test we may bring the application to the page or screen where we want to test.

Operate - Exercise the behaviour we want to test. Work on the SUT in case of functional or call relevant methods on objects in case of unit.

Check - Assert if exercising the behaviour has produced relevant effects. Assert the return from a method, verify expectations on mock.

Clear - This phase depends on what kind of fixtures have been set up for the test. It helps in tests being independent as it brings the world back to the state as it was before the test started running. The set up and clear stages may be run at suite level or test case level or test level depending on the cost of running them. The more frequent the better as they reduce the tests interdependency.

In case of transient fixture which is in memory we don't need to explicitly clear it, the garbage collector may do the work for us. If it is permanent fixture like writing files, or set up of services we need to bring them down explicitly. If a test fails or an exception is thrown while test is run the clear phase should still run to bring SUT to base state.

I have found this pattern to be useful when writing tests. It is simple and effective and I am sure a lot will be practising this. If you have any good test patterns let me know. I am interested in collecting these patterns and documenting them.

Please let me know your feedback through comments :).


Bret said...

Wondering if this pattern should be called Setup Operate Assert Teardown, that being the names used in JUnit and other popular frameworks for these parts of a test.

Sai Venkatakrishnan said...

Interesting observation.. I didn't have any framework in mind when I was writing this. Need to think about this :)

Markus Gärtner said...

The name that Gerard Meszaros uses in his xUnit Test Patterns is "Setup - Execute - Verify - TearDown" I think. Since you're interested in more, I would like to point you out to xUnit Test Patterns page or to the book. Keep me updated if you find new patterns, would be interested, too.