I know this is nothing new, but I will share my perspective anyway:
How I fix bugs with unit tests:
- Write a unit test that reproduces the bug so the test fails
- Fix the bug so the unit test passes
- Send a link with both to the bug submitter
Why I like it:
- Saves time debugging – just run the unit test instead of recreating the bug scenario every time
- Creates a simple yes/no criteria to verify whether the bug is fixed
- Regression can be detected quickly in the future
- Better communication: bug is documented for other developers looking at this code
- If you don’t have time to fix the bug immediately, write a failing test. Then you or someone else can fix the bug later
- An organic way to increase code coverage for existing code
Why unit tests specifically? Surely there will be cases where another type of test would be more appropriate.
What other kind of test do you mean?