Refactoring Tips: Don't Trust Your Unit Tests

May 7, 2014

Before you start making any changes, make sure you have an integration test covering the part of the application you're changing. While unit tests might be useful on a small scale refactorings, they usually break if you change the layout of your code, even a little bit. This is mostly because a big portion of the unit tests will depend on the exact API of your classes, instead of their behavior.

Once you start changing multiple classes and how they interact it will become harder and harder to rely on the unit tests. When they break it basically forces you to either ignore them, in which case they don't provide much value, or fix them, in which case they don't tell you if the original functionality still works.

On the other hand, having a test that goes through your whole application and verifies that things are wired up properly can easily give you enough confidence to keep refactoring further. It is important to have at least some verification that things aren't completely broken.

This might be a bit different from the usual TDD cycle, where you're supposed to refactor after every small iteration of red-green-refactor. The reason is that when you have a big application, you're sometimes forced to make a big change at once. No matter how many best practices you follow or how many patterns you use, there will be a time when you can't use any of them. The biggest the application is the more likely you will be forced to take drastic measures to fix things.

The best practices developers have are useful, but you need to learn to let them go from time to time. Sometimes you need to get your hands dirty, break all of your unit tests, or even delete them, and keep that single integration spec in your mind to have enough confidence to keep going. Because what really matters is that your application is still working, not that you have 1000 unit tests that don't prove anything.

I'm not saying unit tests are bad. They just aren't all that useful when you're refactoring the layout of your classes, or redesigning your application if you will

source: http://blog.jakubarnold.cz/2014/05/07/refactoring-tips-don-t-trust-your-unit-tests.html

转载于:https://www.cnblogs.com/glenblogs/p/3721508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值