在单元测试有多个断言是不好的做法吗?
问题说明
在一个单元测试中有多个断言是不好的做法吗?重要吗?
Is it bad practice to have more than one assertion in a unit test? Does it matter?
正确答案
有时我每个测试用例只有一个 assert
,但我认为更多时候我有几个 assert
声明.
Sometimes I have exactly one assert
per test case, but I think more often I have several assert
statements.
我见过@Arkain 所回避的案例,其中一段非常大的代码有一个单元测试套件,只有几个测试用例,并且它们都被标记为testCase1
,testCase2
等,每个测试用例有数百个断言.更好的是,每个条件通常取决于先前执行的副作用.每当构建失败时,总是在这样的单元测试中,需要相当长的时间来确定问题出在哪里.
I've seen the case that @Arkain eludes to, where a very large piece of code has a single unit test suite with just a few test cases, and they are all labeled testCase1
, testCase2
, etc, and each test case has hundreds of asserts. And even better, each condition usually depends upon the side-effects of previous execution. Whenever the build fails, invariably in such a unit test, it takes quite some time to determine where the problem was.
但另一个极端是您的问题所暗示的:每个可能条件的单独测试用例.根据您要测试的内容,这可能有意义,但我通常每个测试用例有多个 assert
.
But the other extreme is what your question suggests: a separate test case for each possible condition. Depending on what you're testing, this might make sense, but often I have several assert
s per test case.
例如,如果您编写了 java.lang.Integer
,您可能会遇到如下情况:
For instance, if you wrote java.lang.Integer
, you might have some cases that look like:
public void testValueOf() {
assertEquals(1, Integer.valueOf("1").intValue());
assertEquals(0, Integer.valueOf("0").intValue());
assertEquals(-1, Integer.valueOf("-1").intValue());
assertEquals(Integer.MAX_VALUE, Integer.valueOf("2147483647").intValue());
assertEquals(Integer.MIN_VALUE, Integer.valueOf("-2147483648").intValue());
....
}
public void testValueOfRange() {
assertNumberFormatException("2147483648");
assertNumberFormatException("-2147483649");
...
}
public void testValueOfNotNumbers() {
assertNumberFormatException("");
assertNumberFormatException("notanumber");
...
}
private void assertNumberFormatException(String numstr) {
try {
int number = Integer.valueOf(numstr).intValue();
fail("Expected NumberFormatException for string "" numstr
"" but instead got the number " number);
} catch(NumberFormatException e) {
// expected exception
}
}
一些简单的规则,我可以想到在测试用例中放置多少断言:
Some simple rules that I can think of off hand for how many assert's to put in a test case:
- 不要有多个
assert
依赖于先前执行的副作用. - 将用于测试相同功能/特性或其方面的
assert
分组在一起 - 不需要时不需要多个单元测试用例的开销. - 上述任何规则都应被实用性和常识所覆盖.您可能不希望有一千个单元测试用例在每个(甚至几个断言)中包含一个断言,并且您不希望一个测试用例包含数百个
assert
语句.
- Don't have more than one
assert
that depends on the side-effects of previous execution. - Group
assert
s together that test the same function/feature or facet thereof--no need for the overhead of multiple unit test cases when it's not necessary. - Any of the above rules should be overridden by practicality and common sense. You probably don't want a thousand unit test cases with a single assert in each (or even several asserts) and you don't want a single test case with hundreds of
assert
statements.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcajjhi
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20