Regression test means: a bug happened before, you fixed it, and now you add a test so nobody breaks it again later. So the right order is: show the bug, show the fix, then show the test that protects the fix.
Summary: Test an old bug so it never comes back.
Here is the buggy code first, and then the fixed code:
namespace SmallLearning;
// Before fix: this was wrong.
// It said every lesson id exists.
public function hasLesson(string $lessonId): bool
{
return true;
}
// After fix: this is correct.
public function hasLesson(string $lessonId): bool
{
return $this->lessons->exists($lessonId);
}Now we can create a regression test for the fixed code:
use SmallLearning\LearningService;
use SmallLearning\LessonRepository;
$lessons = new LessonRepository();
$service = new LearningService($lessons);
$this->assertSame(true, $service->hasLesson('php-basics'));
$this->assertSame(false, $service->hasLesson('non-existent-lesson'));Why this test works
- The class we test is LearningService in src/LearningService.php.
- The file starts with namespace SmallLearning;, so the test imports it with use SmallLearning\LearningService;.
- Imagine the old bug was: every lesson id returned true, even when the id did not exist.
- Then we fix the method so it checks the repository correctly.
- After the fix, we add a regression test with one known good value and one known bad value.
- It needs LessonRepository, so we create the dependency first, just like in the unit test.
- Put this file in tests/regression/DuplicateCompletionRegressionTest.php because it protects one old bug.
- The test knows the class exactly like the unit test: import the class and create the object.
- This test is small on purpose.
- Simple rule: pick one old bug, write one small assert for the fixed behavior, and keep it forever.
Real-life examples
- Example: on a shop website, a coupon once failed for guest users, so add a regression test to make sure the discount still works for guests.
- Example: on a blog website, search once crashed on empty input, so add a regression test that empty search returns a safe result.
Remember this
- Find the class in src/ first
- Create dependency first
- Put file in tests/regression
- Test one old bug only
- Run with php tests/run.php regression