如何知道您的单元测试是否“合适大小"? [英] How to know if your Unit Test is "right-sized"?

查看:133
本文介绍了如何知道您的单元测试是否“合适大小"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单元测试中经常注意到的一件事是,它们变得有些冗长;看到它们可能也不够冗长,您如何看待单元测试的大小正确?

One thing that I've always noticed with my unit tests is that they get to be kind of verbose; seeing as they could also be not verbose enough, how do you get a sense of when your unit tests are the right size?

我知道这是一个很好的报价,它是:

I know of a good quote for this and it's:

完美无缺,而不是什么时候 没有什么可添加的,但是当 没有什么可以删除的." -Antoine de Saint-Exupery.

"Perfection is achieved, not when there is nothing left to add, but when there is nothing left to remove." - Antoine de Saint-Exupery.

推荐答案

他们变得冗长的原因之一是他们正在测试多种事物.我尝试使每个单元测试测试一件事,也只能做一件事.因此,我最终进行了许多测试,每个测试都声明了一个功能.它之所以有效,是因为一个故障不会在做出其他断言之前通过退出单元测试来掩盖另一个故障.

One reason why them become verbose is that they're testing multiple things. I try to make each unit test test one thing, and one thing only. So I end up with a lot of tests, each asserting one piece of functionality. It works well since a failure won't disguise another failure by bailing out of the unit test before other assertions are made.

像这样编写细粒度的测试意味着,如果您不小心,将复制很多设置/拆卸代码,而这正是您需要花费时间进行抽象的工作出来(以任何合适的方式).

Writing fine-grained tests like this means that if you're not careful, you'll duplicate a lot of setup/teardown code, and that's the stuff you need to spend time on, abstracting it out (in whatever way is suitable).

这篇关于如何知道您的单元测试是否“合适大小"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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