My understanding is that Unit Tests are intended to protect you while refactoring. Refactoring is not intended to change implementation or purpose, it is intended to restructure without such changes. As such, Unit Tests should (pretty much) never be broken by true refactoring.
Enhancement, improvement and extension are all likely to break Unit Tests, because they can easily require that you change the way things currently work. In this case end-to-end tests are what you need, to make sure your system is producing the right results, including the new enhancements, but especially including the previous results.
In my experience, you can't make substantial improvements to a system with all Unit Tests in place and untouched.
Equally in my experience, with comprehensive Unit Tests you can refactor without fear, and constantly ensure your code is clean and ready for the next phase of development.
That's right Colin. I completely agree, however if you break unit tests during a refactor, yet there is no functional change - bin the tests as long as you have end to end tests higher up. I should add that any new code you add should have unit tests around. E.g the new code added as per the refactor if needs be.
With good unit tests you can indeed refactor without fear, but we should not take the stance of never breaking any unit tests. Providing there is no functional change, break away! Such stance relies on end to end tests/pairing/some other process to ensure you are not breaking valid behaviour though.
Refactoring can impact your unit interface too, and when that happens you'll have to refactor your tests accordingly. What you don't want while refactoring is being stuck with your old interfaces.
Exactly. Refactoring is the process of changing code without changing behaviour. If that means you change some tests so be it. There is no rule to say that you can't do this, but many devs seem to fear this.
4 comments
[ 2.8 ms ] story [ 21.6 ms ] threadEnhancement, improvement and extension are all likely to break Unit Tests, because they can easily require that you change the way things currently work. In this case end-to-end tests are what you need, to make sure your system is producing the right results, including the new enhancements, but especially including the previous results.
In my experience, you can't make substantial improvements to a system with all Unit Tests in place and untouched.
Equally in my experience, with comprehensive Unit Tests you can refactor without fear, and constantly ensure your code is clean and ready for the next phase of development.
With good unit tests you can indeed refactor without fear, but we should not take the stance of never breaking any unit tests. Providing there is no functional change, break away! Such stance relies on end to end tests/pairing/some other process to ensure you are not breaking valid behaviour though.