• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在单元测试有多个断言是不好的做法吗?

用户头像
it1352
帮助3

问题说明

在一个单元测试中有多个断言是不好的做法吗?重要吗?

Is it bad practice to have more than one assertion in a unit test? Does it matter?

正确答案

#1

有时我每个测试用例只有一个 assert,但我认为更多时候我有几个 assert声明.

Sometimes I have exactly one assert per test case, but I think more often I have several assert statements.

我见过@Arkain 所回避的案例,其中一段非常大的代码有一个单元测试套件,只有几个测试用例,并且它们都被标记为testCase1testCase2 等,每个测试用例有数百个断言.更好的是,每个条件通常取决于先前执行的副作用.每当构建失败时,总是在这样的单元测试中,需要相当长的时间来确定问题出在哪里.

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 asserts 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 asserts 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
系列文章
更多 icon
同类精品
更多 icon
继续加载