在一个单元测试中有多个断言是不好的做法吗? [英] 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屋!