Contents
What is refactoring and testing?
With unit testing in place, refactoring is then an iterative cycle of making a small program transformation, testing it to ensure correctness, and making another small transformation. If at any point a test fails, the last small change is undone and repeated in a different way.
Why is it necessary to run the tests after refactoring the code?
Test often The last thing you want to do when refactoring is mess something up in the process and create bugs or problems that affect the functionality of the product. This is why testing throughout the refactoring process is imperative.
Why do you need unit test for refactoring?
If the refactoring can’t be automated then it’s not truly a mechanical transformation of the code and so isn’t a refactoring. The unit tests are to make sure that you are truly just performing mechanical transformations from one codebase to an equivalent one. Refactoring without Unit Test is dangerous. Always have unit test.
When do you need to use unit testing?
Always have unit test. If you change something without having good testing you might be safe for some portion of the code but something elsewhere might not have the same behavior. With Unit Testing you protect any change. Refactoring other code is fine too but the extreme is not. It’s normal that someone else do not program like you do.
What to do in the absence of unit tests?
In the absence of unit tests, you’ll have to put the part of code you want to refactor under test. If unit tests are too hard to retrofit, as it usually is the case, then you can create characterization tests, as recommended per Michael Feathers in Working Effectively with Legacy Code.
Why do you need characterization tests in unit testing?
If unit tests are too hard to retrofit, as it usually is the case, then you can create characterization tests, as recommended per Michael Feathers in Working Effectively with Legacy Code. In short they are end-to-end tests that allow you to pin down the current behavior of the code (which is not assumed to be working perfectly all the time).