2. 測(cè)試術(shù)語(yǔ)
- Code (or application) under test:指被測(cè)試的代碼
- Test fixture:?穗慕?
- Unit tests and unit testing:?jiǎn)卧獪y(cè)試的目標(biāo)是一小段代碼庇楞,例如一個(gè)方法或一個(gè)類(lèi)。
- Integration tests:
- Performance tests:
-
Behavior vs. state testing:
behavior testing
檢查特定方法是否被調(diào)用with正確的參數(shù);State testing
驗(yàn)證函數(shù)調(diào)用的結(jié)果(返回值)
3. 測(cè)試管理
通常單元測(cè)試代碼放在特定的目錄下赂蠢,與項(xiàng)目源代碼分開(kāi)
4. 使用JUnit
一個(gè)JUnit測(cè)試就是測(cè)試類(lèi)中的一個(gè)方法磺樱,編寫(xiě)一個(gè)JUnit 4測(cè)試只要在函數(shù)上標(biāo)注@org.junit.Test
注解。這個(gè)函數(shù)就會(huì)執(zhí)行測(cè)試代碼磅网√附兀可以使用assert語(yǔ)句檢查實(shí)際的返回值是否和期望的一致。
一個(gè)簡(jiǎn)單的JUnit測(cè)試?yán)樱?/p>
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class MyTests {
@Test
public void multiplicationOfZeroIntegersShouldReturnZero() {
MyClass tester = new MyClass(); // MyClass is tested
// assert statements
assertEquals("10 x 0 must be 0", 0, tester.multiply(10, 0));
assertEquals("0 x 10 must be 0", 0, tester.multiply(0, 10));
assertEquals("0 x 0 must be 0", 0, tester.multiply(0, 0));
}
}
JUnit測(cè)試的命名規(guī)范涧偷,以被測(cè)試類(lèi)的名稱(chēng)添加后綴"-test"來(lái)命名測(cè)試類(lèi)
JUnit test suites:如果有多個(gè)測(cè)試類(lèi)簸喂,可以把它們結(jié)合到一個(gè)test suite
5. JUnit代碼結(jié)構(gòu)
- JUnit 4通過(guò)注解標(biāo)示一個(gè)函數(shù)為測(cè)試方法,并且對(duì)它們進(jìn)行配置
Annotation | Description |
---|---|
@Test public void method() | The @Test annotation identifies a method as a test method. |
@Test (expected = Exception.class) | 如果函數(shù)沒(méi)有拋出該異常燎潮,測(cè)試失敗 |
@Test(timeout=100) | 如果函數(shù)執(zhí)行時(shí)間大于100ms喻鳄,測(cè)試失敗 |
@Before public void method() | 在每個(gè)測(cè)試開(kāi)始之前都會(huì)執(zhí)行,用于初始化測(cè)試環(huán)境 |
@After public void method() | 在每個(gè)測(cè)試結(jié)束后都會(huì)執(zhí)行 |
@BeforeClass public static void method() | 在所有測(cè)試開(kāi)始之前只執(zhí)行一次 |
@AfterClass public static void method() | 在所有測(cè)試執(zhí)行結(jié)束之后只執(zhí)行一次 |
@Ignore or @Ignore("Why disabled") | 忽略該測(cè)試函數(shù) |
- Assert語(yǔ)句
Statement | Description |
---|---|
fail(message) | 直接使測(cè)試失敗 |
assertTrue([message,] boolean condition) | 檢查boolean condition是否為true |
assertFalse([message,] boolean condition) | 檢查boolean condition是否為false |
assertEquals([message,] expected, actual) | 測(cè)試兩個(gè)值是否相同 |
assertEquals([message,] expected, actual, tolerance) | 測(cè)試float或double值是否相同 |
assertNull([message,] object) | 檢查對(duì)象為空 |
assertNotNull([message,] object) | 檢查對(duì)象不為空 |
assertSame([message,] expected, actual) | 檢查兩個(gè)變量引用同一個(gè)對(duì)象 |
assertNotSame([message,] expected, actual) | 檢查兩個(gè)變量引用不同的對(duì)象 |
JUnit假設(shè)所有測(cè)試函數(shù)可以任意的順序執(zhí)行确封,所以在寫(xiě)測(cè)試代碼時(shí)不應(yīng)該依賴(lài)其他測(cè)試的執(zhí)行順序
在執(zhí)行時(shí)動(dòng)態(tài)忽略一個(gè)測(cè)試:
Assume.assumeFalse(System.getProperty("os.name").contains("Linux"));
10.JUnit高級(jí)
- Parameterized test
- JUnit Rules
通過(guò)JUnit Rules可以為測(cè)試類(lèi)中的每個(gè)測(cè)試添加行為除呵。可以為類(lèi)型為TestRule
的屬性標(biāo)示@Rule
注解爪喘。創(chuàng)建的對(duì)象可以在測(cè)試函數(shù)中使用和配置颜曾。
public class RuleTester {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void testUsingTempFolder() throws IOException {
File createdFolder = folder.newFolder("newfolder");
File createdFile = folder.newFile("myfilefile.txt");
assertTrue(createdFile.exists());
}
}
- Writing custom JUnit rules
- Categories