JUnit的困惑:使用'扩展TestCase的“或”@Test“? [英] JUnit confusion: use 'extends TestCase' or '@Test'?
问题描述
我已经找到了正确使用(或者至少是文档)的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类,并开始测试方法字测试
。当运行类作为JUnit测试(在Eclipse),所有的方法以单词测试
是自动运行。
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中,样式):创建正常类和prePEND一个 @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);
}
}
混合两种似乎不是一个好主意,例如见这个计算器问题:
现在,我的问题(S):
Now, my questions(s):
- 什么是preferred方法,或当你想使用一个,而不是其他?
- 办法b使用此功能用于测试的例外在
延长像@Test标注@Test(预期= ArithmeticException.class)
。 但是? 使用方法的一个时,你怎么测试异常 -
在使用方法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套件=新的TestSuite(所有测试);
suite.addTestSuite(DummyTestA.class);
suite.addTestSuite(DummyTestAbis.class);
但是,这不能用的方法使用b(因为每个应的TestClass TestCase的子类)。 什么是适当的方式组测试方法B'
编辑:我已经添加了JUnit版本,这两种方法
I've added the JUnit versions to both approaches
推荐答案
的区别是相当容易:
- 延长
测试用例
就是这样,单元测试,写在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
annotaton 是更明确和更容易的工具支持(例如它很容易搜索所有测试这种方式) - 多种方法可以
@Before
来注释A> /@BeforeClass
和的@After
/的@AfterClass
提供更多的灵活性 -
@rule
注释的支持之类的东西<一个href=\"http://junit.org/apidocs/org/junit/rules/ExpectedException.html\"><$c$c>ExpectedException$c$c> - 的
@Ignored
注解 - 使用替代测试运行支持<一个href=\"http://junit.sourceforge.net/javadoc/org/junit/runner/RunWith.html\"><$c$c>@RunWith$c$c>
- The
@Test
annotaton is more explicit and is easier to support in tools (for example it's easy to search for all tests this way) - Multiple methods can be annotated with
@Before
/@BeforeClass
and@After
/@AfterClass
providing more flexibility - Support for
@Rule
annotations on things likeExpectedException
- Support for the
@Ignored
annotation - Support for alternative test runners using
@RunWith
要测试在JUnit 3中预计例外测试用例
你必须使文字清晰。
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的困惑:使用'扩展TestCase的“或”@Test“?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!