- JUnit 教程
- JUnit - 首頁
- JUnit - 概述
- JUnit - 環境設定
- JUnit - 測試框架
- JUnit - 基本用法
- JUnit - API
- JUnit - 編寫測試
- JUnit - 使用斷言
- JUnit - 執行過程
- JUnit - 執行測試
- JUnit - 套件測試
- JUnit - 忽略測試
- JUnit - 時間測試
- JUnit - 異常測試
- JUnit - 引數化測試
- JUnit - 與 Ant 整合
- JUnit - 與 Eclipse 整合
- JUnit - 擴充套件
- JUnit 有用資源
- JUnit - 問答
- JUnit - 快速指南
- JUnit - 有用資源
- JUnit - 討論
JUnit - API
JUnit 中最重要的包是junit.framework,它包含所有核心類。一些重要的類如下:
| 序號 | 類名 | 功能 |
|---|---|---|
| 1 | Assert | 一組斷言方法。 |
| 2 | TestCase | 測試用例定義了執行多個測試的fixture。 |
| 3 | TestResult | TestResult 收集執行測試用例的結果。 |
| 4 | TestSuite | TestSuite 是測試的組合。 |
Assert 類
以下是org.junit.Assert 類的宣告:
public class Assert extends java.lang.Object
此類提供了一組用於編寫測試的斷言方法。只有失敗的斷言才會被記錄。Assert 類的一些重要方法如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | void assertEquals(boolean expected, boolean actual) 檢查兩個基本型別/物件是否相等。 |
| 2 | void assertFalse(boolean condition) 檢查條件是否為假。 |
| 3 | void assertNotNull(Object object) 檢查物件是否不為空。 |
| 4 | void assertNull(Object object) 檢查物件是否為空。 |
| 5 | void assertTrue(boolean condition) 檢查條件是否為真。 |
| 6 | void fail() 測試失敗,無訊息。 |
讓我們在一個例子中使用上面提到的某些方法。在C:\>JUNIT_WORKSPACE中建立一個名為TestJunit1.java 的java類檔案。
import org.junit.Test;
import static org.junit.Assert.*;
public class TestJunit1 {
@Test
public void testAdd() {
//test data
int num = 5;
String temp = null;
String str = "Junit is working fine";
//check for equality
assertEquals("Junit is working fine", str);
//check for false condition
assertFalse(num > 6);
//check for not null value
assertNotNull(temp);
}
}
接下來,在C:\>JUNIT_WORKSPACE中建立一個名為TestRunner1.java的java類檔案來執行測試用例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner1 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit1.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用javac編譯測試用例和測試執行器類。
C:\JUNIT_WORKSPACE>javac TestJunit1.java TestRunner1.java
現在執行測試執行器,它將執行在提供的測試用例類中定義的測試用例。
C:\JUNIT_WORKSPACE>java TestRunner1
驗證輸出。
true
TestCase 類
以下是org.junit.TestCase 類的宣告:
public abstract class TestCase extends Assert implements Test
測試用例定義了執行多個測試的fixture。TestCase類的一些重要方法如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | int countTestCases() 計算run(TestResult result)執行的測試用例數量。 |
| 2 | TestResult createResult() 建立一個預設的TestResult物件。 |
| 3 | String getName() 獲取TestCase的名稱。 |
| 4 | TestResult run() 一個方便的方法來執行這個測試,使用預設的TestResult物件收集結果。 |
| 5 | void run(TestResult result) 執行測試用例並在TestResult中收集結果。 |
| 6 | void setName(String name) 設定TestCase的名稱。 |
| 7 | void setUp() 設定fixture,例如,開啟網路連線。 |
| 8 | void tearDown() 拆除fixture,例如,關閉網路連線。 |
| 9 | String toString() 返回測試用例的字串表示。 |
讓我們在一個例子中使用上面提到的某些方法。在C:\>JUNIT_WORKSPACE中建立一個名為TestJunit2.java的java類檔案。
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
public class TestJunit2 extends TestCase {
protected double fValue1;
protected double fValue2;
@Before
public void setUp() {
fValue1 = 2.0;
fValue2 = 3.0;
}
@Test
public void testAdd() {
//count the number of test cases
System.out.println("No of Test Case = "+ this.countTestCases());
//test getName
String name = this.getName();
System.out.println("Test Case Name = "+ name);
//test setName
this.setName("testNewAdd");
String newName = this.getName();
System.out.println("Updated Test Case Name = "+ newName);
}
//tearDown used to close the connection or clean up activities
public void tearDown( ) {
}
}
接下來,在C:\>JUNIT_WORKSPACE中建立一個名為TestRunner2.java的java類檔案來執行測試用例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit2.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用javac編譯測試用例和測試執行器類。
C:\JUNIT_WORKSPACE>javac TestJunit2.java TestRunner2.java
現在執行測試執行器,它將執行在提供的測試用例類中定義的測試用例。
C:\JUNIT_WORKSPACE>java TestRunner2
驗證輸出。
No of Test Case = 1 Test Case Name = testAdd Updated Test Case Name = testNewAdd true
TestResult 類
以下是org.junit.TestResult類的宣告:
public class TestResult extends Object
TestResult 收集執行測試用例的結果。它是收集引數模式的一個例項。測試框架區分失敗和錯誤。失敗是預期的,並透過斷言進行檢查。錯誤是意外的問題,例如ArrayIndexOutOfBoundsException。TestResult類的一些重要方法如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | void addError(Test test, Throwable t) 向錯誤列表中新增錯誤。 |
| 2 | void addFailure(Test test, AssertionFailedError t) 向失敗列表中新增失敗。 |
| 3 | void endTest(Test test) 通知結果測試已完成。 |
| 4 | int errorCount() 獲取檢測到的錯誤數量。 |
| 5 | Enumeration<TestFailure> errors() 返回錯誤的Enumeration。 |
| 6 | int failureCount() 獲取檢測到的失敗數量。 |
| 7 | void run(TestCase test) 執行TestCase。 |
| 8 | int runCount() 獲取執行的測試數量。 |
| 9 | void startTest(Test test) 通知結果將要啟動測試。 |
| 10 | void stop() 標記測試執行應該停止。 |
在C:\>JUNIT_WORKSPACE中建立一個名為TestJunit3.java的java類檔案。
import org.junit.Test;
import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
public class TestJunit3 extends TestResult {
// add the error
public synchronized void addError(Test test, Throwable t) {
super.addError((junit.framework.Test) test, t);
}
// add the failure
public synchronized void addFailure(Test test, AssertionFailedError t) {
super.addFailure((junit.framework.Test) test, t);
}
@Test
public void testAdd() {
// add any test
}
// Marks that the test run should stop.
public synchronized void stop() {
//stop the test here
}
}
接下來,在C:\>JUNIT_WORKSPACE中建立一個名為TestRunner3.java的java類檔案來執行測試用例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner3 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit3.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用javac編譯測試用例和測試執行器類。
C:\JUNIT_WORKSPACE>javac TestJunit3.java TestRunner3.java
現在執行測試執行器,它將執行在提供的測試用例類中定義的測試用例。
C:\JUNIT_WORKSPACE>java TestRunner3
驗證輸出。
true
TestSuite 類
以下是org.junit.TestSuite類的宣告
public class TestSuite extends Object implements Test
TestSuite 是測試的組合。它執行一系列測試用例。TestSuite類的一些重要方法如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | void addTest(Test test) 向套件中新增測試。 |
| 2 | void addTestSuite(Class<? extends TestCase> testClass) 將給定類中的測試新增到套件中。 |
| 3 | int countTestCases() 計算此測試將執行的測試用例數量。 |
| 4 | String getName() 返回套件的名稱。 |
| 5 | void run(TestResult result) 執行測試並在TestResult中收集它們的結果。 |
| 6 | void setName(String name) 設定套件的名稱。 |
| 7 | Test testAt(int index) 返回給定索引處的測試。 |
| 8 | int testCount() 返回此套件中的測試數量。 |
| 9 | static Test warning(String message) 返回一個將失敗並記錄警告訊息的測試。 |
在C:\>JUNIT_WORKSPACE中建立一個名為JunitTestSuite.java的java類檔案來建立測試套件。
import junit.framework.*;
public class JunitTestSuite {
public static void main(String[] a) {
// add the test's in the suite
TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class, TestJunit3.class );
TestResult result = new TestResult();
suite.run(result);
System.out.println("Number of test cases = " + result.runCount());
}
}
使用javac編譯測試套件類。
C:\JUNIT_WORKSPACE>javac JunitTestSuite.java
現在執行測試套件。
C:\JUNIT_WORKSPACE>java JunitTestSuite
驗證輸出。
No of Test Case = 1 Test Case Name = testAdd Updated Test Case Name = testNewAdd Number of test cases = 3