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


当前回答

Unit Testing is one of the most adopted methodologies for high quality code. Its contribution to a more stable, independent and documented code is well proven . Unit test code is considered and handled as an a integral part of your repository, and as such requires development and maintenance. However, developers often encounter a situation where the resources invested in unit tests where not as fruitful as one would expect. In an ideal world every method we code will have a series of tests covering it’s code and validating it’s correctness. However, usually due to time limitations we either skip some tests or write poor quality ones. In such reality, while keeping in mind the amount of resources invested in unit testing development and maintenance, one must ask himself, given the available time, which code deserve testing the most? And from the existing tests, which tests are actually worth keeping and maintaining? See here

其他回答

多年来,我一直试图说服人们,他们需要为自己的代码编写单元测试。无论他们是先编写测试(如TDD)还是在编写功能之后,我总是试图向他们解释对代码进行单元测试的所有好处。几乎没有人反对我。你不能否认一些显而易见的事情,任何聪明的人都会看到单元测试和TDD的好处。

单元测试的问题在于它需要行为上的改变,而要改变人们的行为是非常困难的。用语言,你会让很多人同意你的观点,但你不会看到他们做事的方式有太多变化。

你必须通过行动来说服人们。你的个人成功会比你的争论吸引更多的人。如果他们看到你不只是在谈论单元测试或TDD,而是在做你鼓吹的事情,而且你是成功的,人们就会试图模仿你。

You should also take on a lead role because no one writes unit test right the first time, so you may need to coach them on how to do it, show them the way, and the tools available to them. Help them while they write their first tests, review the tests they write on their own, and show them the tricks, idioms and patterns you've learned through your own experiences. After a while, they will start seeing the benefits on their own, and they will change their behavior to incorporate unit tests or TDD into their toolbox.

改变不会在一夜之间发生,但只要有一点耐心,你就可能实现你的目标。

单元测试绝对是值得付出努力的。不幸的是,您选择了一个困难的(但不幸的是常见的)场景来实现它。

从单元测试中获得的最大好处是从头开始使用它——在一些精选的小项目中,我有幸在实现类之前编写单元测试(此时接口已经完成)。通过适当的单元测试,您可以在类还处于婴儿期时就发现并修复它们,而不是在将来毫无疑问会集成到复杂系统中的任何地方。

如果您的软件是完全面向对象的,那么您应该能够在类级别上添加单元测试,而不需要太多的努力。如果您没有那么幸运,您仍然应该尽可能地尝试合并单元测试。确保当你添加新功能时,新部分都有清晰的接口,你会发现单元测试让你的工作变得更容易。

我在几年前发现了TDD,从那时起我就使用它编写了所有我喜欢的项目。我估计,TDD一个项目所花费的时间与牛仔式地组合一个项目所花费的时间大致相同,但我对最终产品的信心增加了,以至于我忍不住有一种成就感。

我还觉得它改进了我的设计风格(更面向界面,以防我需要一起模拟东西),而且,正如顶部的绿色帖子所写的,它有助于“编码便秘”:当你不知道接下来要写什么,或者你有一个令人生畏的任务摆在你面前时,你可以写小一点的。

最后,我发现到目前为止,TDD最有用的应用是在调试中,如果仅仅因为您已经开发了一个询问框架,那么您就可以用它来刺激项目以可重复的方式产生错误。

单元测试适用于QA人员或你的经理,而不是你;所以绝对不值得。

您应该专注于编写正确的代码(不管这意味着什么),而不是测试用例。让其他人去担心吧。

当您手动测试软件时,通常会使用一小组测试/操作。最终,您将自动修改输入数据或操作,以便围绕已知问题进行导航。应该有单元测试来提醒您某些事情不能正常工作。

我建议在编写代码之前编写测试,添加新的测试/数据来改进主代码的功能!