我正致力于将单元测试集成到我所在团队的开发过程中,有一些人对此持怀疑态度。有什么好方法可以让团队中持怀疑态度的开发人员相信单元测试的价值?在我的具体情况下,我们将在添加功能或修复错误时添加单元测试。不幸的是,我们的代码库并不容易进行测试。


当前回答

[我有一个观点,我不能在上面看到]

“每个人都在进行单元测试,他们不一定意识到这一点——事实”

想想看,你写了一个函数来解析一个字符串并删除新的行字符。作为一个开发新手,你要么在命令行中通过Main()实现它来运行几个用例,要么用一个按钮组合一个可视化前端,将你的函数绑定到几个文本框和一个按钮上,然后看看会发生什么。

这就是单元测试——基本的和糟糕的组合在一起,但是你测试了一些情况下的代码段。

你写一些更复杂的东西。当您抛出一些用例(单元测试)并将其调试到代码中并进行跟踪时,它会抛出错误。你在浏览过程中查看这些值,并决定它们是对还是错。在某种程度上,这是单元测试。

这里的单元测试实际上是采用这种行为,将其形式化为结构化模式并保存,以便您可以轻松地重新运行这些测试。如果您编写了一个“适当的”单元测试用例而不是手动测试,那么它所花费的时间是相同的,或者随着您的经验的增加可能会更少,并且您可以一次又一次地重复它

其他回答

关于单元测试要记住的一件事是,它对开发人员来说是一种安慰。

相反,功能测试是针对用户的:无论何时添加功能测试,您都是在测试用户将看到的东西。当您添加单元测试时,您只是让开发人员的生活更轻松。在这方面有点奢侈。

当您必须在编写单元或功能测试之间做出选择时,请记住这种二分法。

在我们的办公室里,每天都有这样的对话:

“伙计,我就是喜欢单元测试,我刚刚能够对某些东西的工作方式进行了一系列更改,然后能够通过再次运行测试来确认我没有破坏任何东西……”

细节每天都在变化,但情绪却不变。单元测试和测试驱动开发(TDD)有很多隐藏的和个人的好处,也有很多明显的好处,除非他们自己做,否则你无法真正向他们解释。

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

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 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. 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 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 :) 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应用程序用户交互等),但即使如此,我们在这个商店里都被测试感染了,当我们把测试束缚住时,我们最高兴。我怎么极力推荐这种方法都不为过。

还没有人提到的一件事是,让所有开发人员承诺实际运行和更新任何现有的自动化测试。当您重新进行自动化测试并发现由于新的开发而出现故障时,会失去很多价值并使自动化测试非常痛苦。因为开发人员已经手动测试了代码,所以大多数测试不会指出错误,所以花在更新它们上的时间只是浪费。

说服怀疑者不要破坏其他人在单元测试上所做的工作,对于从测试中获得价值更为重要,而且可能更容易。

每次从存储库更新时,花费数小时更新由于新特性而损坏的测试既没有效率也没有乐趣。

单元测试很像去健身房。你知道这对你有好处,所有的争论都有意义,所以你开始锻炼。刚开始有一种冲动,这很好,但几天后你开始怀疑是否值得这样做。你每天花一个小时换衣服,在仓鼠转轮上跑步,你不确定除了腿和手臂疼痛之外,你真的得到了什么。

然后,也许一两周后,就在疼痛消失的时候,一个重要的截止日期开始来临。你需要把醒着的每一个小时都用来完成“有用的”工作,所以你要去掉无关紧要的事情,比如去健身房。你改掉了这个习惯,当截止日期结束的时候,你又回到了起点。如果你设法回到健身房,你会觉得和你第一次去的时候一样酸痛。

You do some reading, to see if you're doing something wrong. You begin feel a little bit of irrational spite toward all the fit, happy people extolling the virtues of exercise. You realize that you don't have a lot in common. They don't have to drive 15 minutes out of the way to go to the gym; there is one in their building. They don't have to argue with anybody about the benefits of exercise; it is just something everybody does and accepts as important. When a Big Deadline approaches, they aren't told that exercise is unnecessary any more than your boss would ask you to stop eating.

所以,回答你的问题,单元测试通常是值得付出努力的,但是所需的努力量对每个人来说都不一样。如果你在一家不重视代码质量的公司处理意大利面条式的代码库,那么单元测试可能需要付出巨大的努力。(许多经理会歌颂单元测试,但这并不意味着他们会在关键时刻支持单元测试。)

如果你正试图将单元测试引入到你的工作中,并且没有看到你所期待的所有阳光和彩虹,不要责怪自己。你可能需要找一份新工作来真正让单元测试为你工作。

单元测试非常值得最初的投资。自从几年前开始使用单元测试以来,我发现了一些真正的好处:

回归测试消除了恐惧 对代码进行更改(什么都没有 就像看到代码的温暖光芒 每次有变化,要么工作,要么爆发 了) 以下的可执行代码示例 其他团队成员(包括你自己) 6个月的时间…) 无情的重构——这是令人难以置信的回报,试试吧!

代码片段可以极大地帮助减少创建测试的开销。创建能够在几秒钟内创建类大纲和相关单元测试fixture的代码片段并不困难。