JUnit 混淆:使用“扩展 TestCase"或“@Test"?

2022-01-14 00:00:00 automated-tests java junit

我发现 JUnit 的正确使用(或至少是文档)非常令人困惑.这个问题既可以作为未来的参考,也可以作为一个真实的问题.

I've found the proper use (or at least the documentation) of JUnit very confusing. This question serves both as a future reference and as a real question.

如果我理解正确的话,创建和运行 JUnit 测试有两种主要方法:

If I've understood correctly, there are two main approaches to create and run a JUnit test:

方法 A(JUnit 3 风格):创建一个扩展 TestCase 的类,并使用单词 test 开始测试方法.当将该类作为 JUnit 测试运行时(在 Eclipse 中),所有以单词 test 开头的方法都会自动运行.

Approach A (JUnit 3-style): create a class that extends TestCase, and start test methods with the word test. When running the class as a JUnit Test (in Eclipse), all methods starting with the word test are automatically run.

import junit.framework.TestCase;

public class DummyTestA extends TestCase {

    public void testSum() {
        int a = 5;
        int b = 10;
        int result = a + b;
        assertEquals(15, result);
    }
}

方法 B(JUnit 4 风格): 创建一个普通"类并在方法前添加 @Test 注释.请注意,您不必以单词 test 开始方法.

Approach B (JUnit 4-style): create a 'normal' class and prepend a @Test annotation to the method. Note that you do NOT have to start the method with the word test.

import org.junit.*;
import static org.junit.Assert.*;

public class DummyTestB {

    @Test
    public void Sum() {
        int a = 5;
        int b = 10;
        int result = a + b;
        assertEquals(15, result);
    }
}

将两者混合似乎不是一个好主意,请参见例如这个stackoverflow问题:

Mixing the two seems not to be a good idea, see e.g. this stackoverflow question:

现在,我的问题:

  1. 首选方法是什么,或者您什么时候会使用其中一种方法而不是另一种方法?
  2. 方法 B 允许通过扩展 @Test 注释来测试异常,如 @Test(expected = ArithmeticException.class).但是在使用方法 A 时如何测试异常?
  3. 当使用方法 A 时,您可以将多个测试类分组到一个测试套件中,如下所示:

  1. What is the preferred approach, or when would you use one instead of the other?
  2. Approach B allows for testing for exceptions by extending the @Test annotation like in @Test(expected = ArithmeticException.class). But how do you test for exceptions when using approach A?
  3. When using approach A, you can group a number of test classes in a test suite like this:

TestSuite suite = new TestSuite("All tests");
suite.addTestSuite(DummyTestA.class);
suite.addTestSuite(DummyTestAbis.class);

但这不能与方法 B 一起使用(因为每个测试类都应该是 TestCase 的子类).为方法 B 分组测试的正确方法是什么?

我已将 JUnit 版本添加到这两种方法中

I've added the JUnit versions to both approaches

推荐答案

区分比较简单:

  • 扩展 TestCase 是在 JUnit 3 中编写单元测试的方式(当然 JUnit 4 仍然支持它)
  • 使用@Test注解是JUnit 4引入的方式
  • extending TestCase is the way unit tests were written in JUnit 3 (of course it's still supported in JUnit 4)
  • using the @Test annotation is the way introduced by JUnit 4

通常您应该选择注释路径,除非需要与 JUnit 3(和/或早于 Java 5 的 Java 版本)兼容.新方法有几个优点:

Generally you should choose the annotation path, unless compatibility with JUnit 3 (and/or a Java version earlier than Java 5) is needed. The new way has several advantages:

  • @Test 注释 更明确,更容易在工具中支持(例如,通过这种方式可以轻松搜索所有测试)
  • 可以用 @Before 注释多个方法/@BeforeClass@After/@AfterClass提供更大的灵活性
  • 支持 @Rule 注释ExpectedException 之类的东西
  • 支持 @Ignored 注释
  • 支持使用 @RunWith 的替代测试运行器

要在 JUnit 3 TestCase 中测试预期的异常,您必须明确文本.

To test for expected exceptions in a JUnit 3 TestCase you'd have to make the text explicit.

public void testMyException() {
  try {
    objectUnderTest.myMethod(EVIL_ARGUMENT);
    fail("myMethod did not throw an Exception!");
  } catch (MyException e) {
    // ok!
    // check for properties of exception here, if desired
  }
}

JUnit 5 引入了另一个 API 更改,但仍使用注释.新的 @Test 注释是 org.junit.jupiter.api.Test(旧"JUnit 4 是 org.junit.Test),但它的工作方式与 JUnit 4 几乎相同.

JUnit 5 introduced yet another API change, but still uses annotations. The new @Test annotation is org.junit.jupiter.api.Test (the "old" JUnit 4 one was org.junit.Test), but it works pretty much the same as the JUnit 4 one.

相关文章