These tests are also known as Characterization tests.
A characterization test describes the actual behavior of an existing piece of software, and therefore protects existing behavior of legacy code against unintended changes via automated testing. This term was coined by Michael Feathers.
They enable and provide a safety net for extending and refactoring code that does not have adequate unit tests. A test can be written that asserts that the output of the legacy code matches the observed result for the given inputs.
These are my learnings one year after reading Working Effectively with Legacy Code and applying it to the different projects I’ve been working on since then.
Find out the assertions. Create a unit test file for your class and a testing method for the function that you want to test.
Hint:
If we have the following method
applySomeLogic(): ReturnType
,
the test we will write should be
testApplySomeLogic(): void
.
final class MyBusinessLogic
{
private DependencyInterface dependencyInterface;
private ConcreteDependency concrete;
public function __construct(
DependencyInterface dependencyInterface,
ConcreteDependency concrete
) {
this.dependencyInterface = dependencyInterface;
this.concrete = concrete;
}
public function applySomeLogic(Input input): ReturnType
{
// black box responsible to create a ReturnType
// based on the given Input
return returnType;
}
}
final class MyBusinessLogicTest extends TestCase
{
public function testApplySomeLogic(): void
{
// I want to assert that "applying some logic"
// from MyBusinessLogic with the given Input
// I will receive a concrete ReturnType with a
// certain value as its property. Something like:
returnType = myBusinessLogic.applySomeLogic(input);
assertEquals('expected', returnType.getProperty());
}
}
#software-testing #software-development #testing #software #programming #testing