单元测试值得付出努力吗? [英] Is Unit Testing worth the effort?

查看:28
本文介绍了单元测试值得付出努力吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将单元测试集成到我工作的团队的开发过程中,但有一些怀疑论者.有什么好的方法可以让团队中持怀疑态度的开发人员相信单元测试的价值?在我的特定情况下,我们将在添加功能或修复错误时添加单元测试.不幸的是,我们的代码库不适合轻松测试.

I am working to integrate unit testing into the development process on the team I work on and there are some sceptics. What are some good ways to convince the sceptical developers on the team of the value of Unit Testing? In my specific case we would be adding Unit Tests as we add functionality or fixed bugs. Unfortunately our code base does not lend itself to easy testing.

推荐答案

我们办公室每天都有这样的交流:

Every day in our office there is an exchange which goes something like this:

伙计,我只是喜欢单元测试,我刚刚能够对某些工作的方式进行大量更改,然后能够通过再次运行测试来确认我没有破坏任何东西..."

"Man, I just love unit tests, I've just been able to make a bunch of changes to the way something works, and then was able to confirm I hadn't broken anything by running the test over it again..."

细节每天都在变化,但情绪不会.单元测试和测试驱动开发 (TDD) 有许多隐藏的和个人的好处以及显而易见的好处,除非他们自己做,否则您无法真正向某人解释.

The details change daily, but the sentiment doesn't. Unit tests and test-driven development (TDD) have so many hidden and personal benefits as well as the obvious ones that you just can't really explain to somebody until they're doing it themselves.

但是,忽略这一点,这是我的尝试!

But, ignoring that, here's my attempt!

  1. 单元测试允许您快速对代码进行重大更改.您知道它现在起作用了,因为您已经运行了测试,当您进行需要进行的更改时,您需要让测试再次运行.这可以节省数小时.

  1. Unit Tests allows you to make big changes to code quickly. You know it works now because you've run the tests, when you make the changes you need to make, you need to get the tests working again. This saves hours.

TDD 可帮助您了解何时停止编码.你的测试让你相信你现在已经做得足够了,可以停止调整并继续下一步.

TDD helps you to realise when to stop coding. Your tests give you confidence that you've done enough for now and can stop tweaking and move on to the next thing.

测试和代码协同工作以实现更好的代码.您的代码可能很糟糕/有问题.您的测试可能很糟糕/有问题.在 TDD 中,您寄希望于两者都是坏的/错误的可能性很低.通常需要修复的是测试,但这仍然是一个好的结果.

The tests and the code work together to achieve better code. Your code could be bad / buggy. Your TEST could be bad / buggy. In TDD you are banking on the chances of both being bad / buggy being low. Often it's the test that needs fixing but that's still a good outcome.

TDD 有助于解决编码便秘.当面临大量艰巨的工作时,编写测试将使您快速行动.

TDD helps with coding constipation. When faced with a large and daunting piece of work ahead writing the tests will get you moving quickly.

单元测试可帮助您真正了解您正在处理的代码的设计.您不是编写代码来做某事,而是首先概述您对代码施加的所有条件以及您期望从中得到什么输出.

Unit Tests help you really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outputs you'd expect from that.

单元测试为您提供即时的视觉反馈,我们都喜欢完成后所有这些绿灯的感觉.这是非常令人满意的.在中断后从上次停下的地方继续前进也容易得多,因为您可以看到到达的地方 - 下一个需要修复的红灯.

Unit Tests give you instant visual feedback, we all like the feeling of all those green lights when we've done. It's very satisfying. It's also much easier to pick up where you left off after an interruption because you can see where you got to - that next red light that needs fixing.

与流行的信念相反,单元测试并不意味着编写两倍的代码,或者编码更慢.一旦你掌握了它,它比没有测试的编码更快、更健壮.测试代码本身通常相对简单,并且不会给您正在做的事情增加很大的开销.这是你只有在做的时候才会相信的:)

Contrary to popular belief unit testing does not mean writing twice as much code, or coding slower. It's faster and more robust than coding without tests once you've got the hang of it. Test code itself is usually relatively trivial and doesn't add a big overhead to what you're doing. This is one you'll only believe when you're doing it :)

我认为是 Fowler 说的:不完美的测试,经常运行,比完全没有写过的完美测试要好得多".我将此解释为允许我编写我认为最有用的测试,即使我的其余代码覆盖范围非常不完整.

I think it was Fowler who said: "Imperfect tests, run frequently, are much better than perfect tests that are never written at all". I interpret this as giving me permission to write tests where I think they'll be most useful even if the rest of my code coverage is woefully incomplete.

好的单元测试可以帮助记录和定义应该做什么

Good unit tests can help document and define what something is supposed to do

单元测试有助于代码重用.将您的代码您的测试迁移到您的新项目.调整代码直到测试再次运行.

Unit tests help with code re-use. Migrate both your code and your tests to your new project. Tweak the code till the tests run again.

我参与的很多工作都没有很好地进行单元测试(Web 应用程序用户交互等),但即便如此,我们在这家商店中都被测试感染了,当我们将测试绑定时最开心下.我不能高度推荐这种方法.

A lot of work I'm involved with doesn't Unit Test well (web application user interactions etc.), but even so we're all test infected in this shop, and happiest when we've got our tests tied down. I can't recommend the approach highly enough.

这篇关于单元测试值得付出努力吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆