問題描述
我發現 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:
現在,我的問題:
- 首選方法是什么,或者您什么時候會使用其中一種方法而不是另一種方法?
- 方法 B 允許通過擴展 @Test 注釋來測試異常,如
@Test(expected = ArithmeticException.class)
.但是在使用方法 A 時如何測試異常? 當使用方法 A 時,您可以將多個測試類分組到一個測試套件中,如下所示:
- What is the preferred approach, or when would you use one instead of the other?
- 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? 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.
這篇關于JUnit 混淆:使用“擴展 TestCase"或“@Test"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!