发布时间:2019-09-20 07:34:44编辑:auto阅读(1886)
JUnit 是 Java? 语言事实上的 标准单元测试库。JUnit 4 是该库三年以来最具里程碑意义的一次发布。它的新特性主要是通过采用 Java 5 中的标记(annotation)而不是利用子类、反射或命名机制来识别测试,从而简化测试。在本文中,执着的代码测试人员 Elliotte Harold 以 JUnit 4 为例,详细介绍了如何在自己的工作中使用这个新框架。注意,本文假设读者具有 JUnit 的使用经验。
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
public void testAddition() {
int z = x + y;
assertEquals(2, z);
}
} |
@Test 注释来识别的,如下所示:import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
@Test public void testAddition() {
int z = x + y;
assertEquals(2, z);
}
} |
testFoo()、testBar(),等等。例如,下面的方法也可以工作:import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
@Test public void additionTest() {
int z = x + y;
assertEquals(2, z);
}
} |
import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
@Test public void addition() {
int z = x + y;
assertEquals(2, z);
}
} |
List.contains() 由 ListTest.contains() 测试,List.add() 由 ListTest.addAll() 测试,等等。TestCase 类仍然可以工作,但是您不再需要扩展它了。只要您用 @Test 来注释测试方法,就可以将测试方法放到任何类中。但是您需要导入 junit.Assert 类以访问各种 assert 方法,如下所示:import org.junit.Assert;
public class AdditionTest {
private int x = 1;
private int y = 1;
@Test public void addition() {
int z = x + y;
Assert.assertEquals(2, z);
}
} |
import static org.junit.Assert.assertEquals;
public class AdditionTest {
private int x = 1;
private int y = 1;
@Test public void addition() {
int z = x + y;
assertEquals(2, z);
}
} |
setUp() 方法。该方法一般会初始化字段,打开日志记录,重置环境变量,等等。例如,下面是摘自 XOM 的 XSLTransformTest 中的 setUp() 方法:protected void setUp() {
System.setErr(new PrintStream(new ByteArrayOutputStream()));
inputDir = new File("data");
inputDir = new File(inputDir, "xslt");
inputDir = new File(inputDir, "input");
} |
setUp(),只要用 @Before 注释来指示即可,如下所示: @Before protected void initialize() {
System.setErr(new PrintStream(new ByteArrayOutputStream()));
inputDir = new File("data");
inputDir = new File(inputDir, "xslt");
inputDir = new File(inputDir, "input");
} |
@Before 来注释多个方法,这些方法都在每个测试之前运行: @Before protected void findTestDataDirectory() {
inputDir = new File("data");
inputDir = new File(inputDir, "xslt");
inputDir = new File(inputDir, "input");
}
@Before protected void redirectStderr() {
System.setErr(new PrintStream(new ByteArrayOutputStream()));
} |
tearDown() 方法,该方法类似于我在 XOM 中为消耗大量内存的测试所使用的方法:protected void tearDown() {
doc = null;
System.gc();
} |
@After 注释它: @After protected void disposeDocument() {
doc = null;
System.gc();
} |
@Before 一样,也可以用 @After 来注释多个清除方法,这些方法都在每个测试之后运行。@Before 方法在子类中的 @Before 方法之前被调用(这反映了构造函数调用的顺序)。@After 方法以反方向运行:子类中的方法在超类中的方法之前被调用。否则,多个 @Before 或 @After 方法的相对顺序就得不到保证。setUp() 和 tearDown() 方法。任何用 @BeforeClass 注释的方法都将在该类中的测试方法运行之前刚好运行一次,而任何用 @AfterClass 注释的方法都将在该类中的所有测试都运行之后刚好运行一次。System.err,以便输出不被预期的错误消息打乱。然后我在测试结束后还原它,如下所示:// This class tests a lot of error conditions, which
// Xalan annoyingly logs to System.err. This hides System.err
// before each test and restores it after each test.
private PrintStream systemErr;
@BeforeClass protected void redirectStderr() {
systemErr = System.err; // Hold on to the original value
System.setErr(new PrintStream(new ByteArrayOutputStream()));
}
@AfterClass protected void tearDown() {
// restore the original value
System.setErr(systemErr);
} |
@BeforeClass 所初始化的一个对象,那么它有可能会影响其他测试的结果。它有可能在测试套件中引入顺序依赖,并隐藏 bug。与任何优化一样,只在剖析和基准测试证明您具有实际的问题之后才实现这一点。这就是说,我看到了不止一个测试套件运行时间如此之长,以至不能像它所需要的那样经常运行,尤其是那些需要建立很多网络和数据库连接的测试。(例如,LimeWire 测试套件运行时间超过两小时。)要加快这些测试套件,以便程序员可以更加经常地运行它们,您可以做的就是减少 bug。try 块,然后在 try 块的末尾加入一个 fail() 语句。例如,该方法测试被零除抛出一个 ArithmeticException:public void testDivisionByZero() {
try {
int n = 2 / 0;
fail("Divided by zero!");
}
catch (ArithmeticException success) {
assertNotNull(success.getMessage());
}
} |
@Test(expected=ArithmeticException.class)
public void divideByZero() {
int n = 2 / 0;
} |
try-catch 样式。@Ignore,如下所示:// Java doesn't yet support
// the UTF-32BE and UTF32LE encodings
@Ignore public void testUTF32BE()
throws ParsingException, IOException, XIncludeException {
File input = new File(
"data/xinclude/input/UTF32BE.xml"
);
Document doc = builder.build(input);
Document result = XIncluder.resolve(doc);
Document expectedResult = builder.build(
new File(outputDir, "UTF32BE.xml")
);
assertEquals(expectedResult, result);
} |
$ java -classpath .:junit.jar org.junit.runner.JUnitCore nu.xom.tests.XIncludeTest JUnit version 4.0rc1 .....I.. Time: 1.149 OK (7 tests) |
@Test(timeout=500) public void retrieveAllElementsInDocument() {
doc.query("//*");
} |
@Test(timeout=2000)
public void remoteBaseRelativeResolutionWithDirectory()
throws IOException, ParsingException {
builder.build("http://www.ibiblio.org/xml");
} |
assert() 方法:public static void assertEquals(Object[] expected, Object[] actual) public static void assertEquals(String message, Object[] expected, Object[] actual) |
suite() 方法,这些方法用于从多个测试类构建一个测试套件。相反,可变长参数列表用于允许将不确定数量的测试传递给测试运行程序。 org.junit.runner.JUnitCore 类:$ java -classpath .:junit.jar org.junit.runner.JUnitCore TestA TestB TestC... JUnit version 4.0rc1 Time: 0.003 OK (0 tests) |
JUnit4TestAdapter 中。将下面的方法添加到您的 JUnit 4 测试类中应该就足够了: public static junit.framework.Test suite() {
return new JUnit4TestAdapter(AssertionTest.class);
} |
上一篇: Python中文编码问题
下一篇: python 使用stmp发送邮件
51140
50544
41160
37998
32461
29364
28232
23080
23048
21370
1416°
2114°
1756°
1679°
1980°
1758°
2433°
4100°
3969°
2835°