❓ Do you know what characterization testing is?

The term was introduced by @mfeathers, the author of the well-known book:

📔 "Working Effectively with Legacy Code"

A thread 🧵 about characterization testing.
Imagine that you want to refactor a piece of legacy code, but the code:

🔹 Is too complex to know what it does.
🔹 Does not have any form of written documentation.
🔹 Has no tests that allow you to refactor safely.

💡 Characterization testing can help you!
When you write characterization tests, the purpose is not to find bugs.

The purpose is to invoke the legacy code to observe what it does and understand its behavior.

🔑 Characterization tests characterize the *actual* behavior of the code.
The key benefits of characterization testing are:

✅ Helps you gain understanding of the legacy system.

✅ Gives you a test suite almost for free.

✅ Allows you to preserve behavior: you can refactor the code and immediately know if you changed the behavior unintentionally.
❓ But, legacy code is often buggy, why would you want to preserve behavior?

When a system goes into production, it becomes its own specification.

Users depend on the actual behavior of the system.

We must know when behavior changes, regardless of whether it's correct or not.
When you are writing characterization tests:

🔹 If you suspect that the behavior you observe is a bug, seek clarification and escalate the issue, if necessary.

🔹If you are *absolutely* sure that it is a bug, fix it.
An algorithm for writing characterization tests:

1⃣ Create a test.
2⃣ Write a failing assertion.
3⃣ Execute the test to observe the actual behavior of the system.
4⃣ Change the test so that it expects the actual behavior.
5⃣ Repeat until you understand the code well.
You can follow @macerub.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.