我正致力于将单元测试集成到我所在团队的开发过程中,有一些人对此持怀疑态度。有什么好方法可以让团队中持怀疑态度的开发人员相信单元测试的价值?在我的具体情况下,我们将在添加功能或修复错误时添加单元测试。不幸的是,我们的代码库并不容易进行测试。
当前回答
使用单元测试套件,可以在保持其余功能不变的情况下对代码进行更改。这是一个很大的优势。当你完成新功能的编码时,你会使用单元测试套件和回归测试套件吗?
其他回答
在我们的办公室里,每天都有这样的对话:
“伙计,我就是喜欢单元测试,我刚刚能够对某些东西的工作方式进行了一系列更改,然后能够通过再次运行测试来确认我没有破坏任何东西……”
细节每天都在变化,但情绪却不变。单元测试和测试驱动开发(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应用程序用户交互等),但即使如此,我们在这个商店里都被测试感染了,当我们把测试束缚住时,我们最高兴。我怎么极力推荐这种方法都不为过。
单元测试的好处之一是可预测性。
在单元测试之前,我可以非常准确地预测编写代码所需的时间,但无法预测调试所需的时间。
现在,因为我可以计划将要编写什么测试,所以我知道编码需要多长时间,并且在编码结束时,系统已经调试好了!这为开发过程带来了可预见性,消除了许多压力,但仍然保留了所有的乐趣!!
多年来,我一直试图说服人们,他们需要为自己的代码编写单元测试。无论他们是先编写测试(如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.
改变不会在一夜之间发生,但只要有一点耐心,你就可能实现你的目标。
单元测试的一个好处是,它们可以作为说明代码行为方式的文档。好的测试有点像参考实现,团队成员可以通过查看它们来了解如何将他们的代码与您的代码集成。
就在今天,我不得不更改一个类,之前已经为其编写了单元测试。 测试本身写得很好,包括我甚至没有想过的测试场景。 幸运的是,所有测试都通过了,我的更改很快得到了验证,并自信地放到了测试环境中。
推荐文章
- python中的assertEquals和assertEqual
- 使用Mockito测试抽象类
- 使用Moq模拟单元测试的异步方法
- 你的项目没有引用“. net framework,Version=v4.6.2”框架。在“TargetFrameworks”中添加对“.NETFramework,Version=v4.6.2”的引用
- 使用Moq模拟扩展方法
- 基于输入参数模拟python函数
- 用于单元测试的NUnit vs. Visual Studio 2008测试项目
- 在使用信号时,你不能在'原子'块的末尾执行查询,但只能在单元测试期间执行
- .NET核心单元测试-模拟IOptions<T>
- 如何使用JUnit来测试异步进程
- 获取JUnit 4中当前正在执行的测试的名称
- 2个JUnit Assert类之间的区别
- 覆盖默认的Spring-Boot应用程序。属性设置在Junit测试
- 如何指定摩卡的测试目录?
- Karma vs测试框架Jasmine, Mocha, QUnit