在单元测试中有多个断言是不好的做法吗? [英] Is it bad practice to have more than one assertion in a unit test?
问题描述
在一个单元测试中有多个断言是不好的做法吗?重要吗?
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.
这篇关于在单元测试中有多个断言是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!