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


当前回答

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

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

细节每天都在变化,但情绪却不变。单元测试和测试驱动开发(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应用程序用户交互等),但即使如此,我们在这个商店里都被测试感染了,当我们把测试束缚住时,我们最高兴。我怎么极力推荐这种方法都不为过。

其他回答

最好的说服方式是……找到一个bug,为它写一个单元测试,修复这个bug。

这个特定的错误不太可能再次出现,您可以通过测试来证明它。

如果你做得足够多,其他人很快就会明白。

我在其他任何答案中都没有看到这一点,但我注意到的一件事是,我可以更快地调试。你不需要通过正确的步骤序列深入到你的应用程序中,只发现你犯了一个布尔错误,需要重新做一遍。使用单元测试,您可以直接进入正在调试的代码。

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

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

细节每天都在变化,但情绪却不变。单元测试和测试驱动开发(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应用程序用户交互等),但即使如此,我们在这个商店里都被测试感染了,当我们把测试束缚住时,我们最高兴。我怎么极力推荐这种方法都不为过。

单元测试的全部意义在于使测试变得简单。这是自动的。“make test”就完成了。如果您面临的问题之一是难以测试代码,那么这就是使用单元测试的最佳理由。

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

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

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

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

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

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