thePHP.cc Logo Deutsch Contact Menu
mood

Characterization Tests

A Characterization Test is a test that ensures that the behaviour of software does not change during refactoring. The term was coined by Michael Feathers in the book "Working Effectively with Legacy Code".

Unlike a unit test, a characterisation test makes no statement about whether the behaviour of the software is correct or incorrect. Rather, the result that the existing software currently delivers is used as an expectation for the test itself. The test environment of a characterisation test is usually larger than the test environment of a unit test. It is not uncommon for the entire application to be used as the test environment, often even with a dedicated test database to ensure that the state of the database does not change unintentionally between test runs.

Once you know what the output of the code is, you can start refactoring. If you unintentionally change the observable behaviour of the application, this will also affect the output and the characterisation test will fail. Thus, changes in the code can be safeguarded by automated tests, even if the existing code is not covered by unit tests.

A characterisation test is only useful in the short term. Over time, both the output of an application and the database will change. If the characterisation test is then run again, it will fail, but it is generally no longer possible to say exactly whether a change in the code being processed or a change in the global system state caused the test to fail.

The procedure for working with characterisation tests is similar to regression tests, but the objective is somewhat different.

Presentation The Myth of Untestable Code More presentations
Training

Find answers to all questions about test automation by solving problems using examples from your own code base.

More training