概述
以最新版的junit和testng進行一些橫向對比總結。兩種框架在功能上非常相似且使用範圍都很廣泛,哪一個更好?
功能比較
註解 | 異常測試 | 跳過測試 | 超時測試 | 套件 | 分組 | 參數化單值 | 參數化對象 | 依賴測試 | |
---|---|---|---|---|---|---|---|---|---|
testNG | Y | Y | Y | Y | Y | Y | Y | Y | Y |
junit4 | Y | Y | Y | Y | Y | N | Y | N | N |
註解支持
描述 | JUnit 4 | TestNG |
---|---|---|
測試註解 | @Test | @Test |
在套件中的所有測試運行之前運行 | 未實現 | @BeforeSuite |
在套件中的所有測試運行之後運行 | 未實現 | @AfterSuite |
測試之前運行 | 未實現 | @BeforeTest |
測試之後運行 | 未實現 | @AfterTest |
在調用屬於任何這些組的第一個測試方法之前運行 | 未實現 | @BeforeGroups |
在調用屬於任何這些組的第一個測試方法之後運行 | 未實現 | @AfterGroups |
在調用當前類的第一個測試方法之前運行 | @BeforeClass | @BeforeClass |
在調用當前類的第一個測試方法之後運行 | @AfterClass | @AfterClass |
在每個測試方法之前運行 | @Before | @BeforeMethod |
在每個測試方法之後運行 | @After | @AfterMethod |
忽略測試 | @ignore | @Test(enbale=false) |
預期的異常 | @Test(expected = ArithmeticException.class) | @Test(expectedExceptions = ArithmeticException.class) |
超時測試 | @Test(timeout = 1000) | @Test(timeout = 1000) |
JUnit 4中,必須聲明“@BeforeClass”和“@AfterClass”方法作爲靜態方法,TestNG沒有這個約束
簡單示例
TestNG獨特的“分組”概念,每種方法都可以與一個組合相結合,可以根據功能對測試進行分類(分組)
public class TestNGTestGroups{
@Test(groups="method1")
public void testingMethod1() {
System.out.println("Method - testingMethod1()");
}
@Test(groups="method2")
public void testingMethod2() {
System.out.println("Method - testingMethod2()");
}
@Test(groups="method1")
public void testingMethod1_1() {
System.out.println("Method - testingMethod1_1()");
}
}
使用以下XML文件,可以僅使用組“method1”執行單元測試
<suite name="MySuite">
<test name="testing">
<groups>
<run>
<include name="method1"/>
</run>
</groups>
<classes>
<class name="demo.testng.TestNGTestGroups" />
</classes>
</test>
</suite>
參數化測試是指參數值的變化。JUnit4 “@RunWith”和“@Parameter”用於提供單元測試的參數值,@Parameters必須返回List [],參數將作爲參數傳入類構造函數。
@RunWith(value = Parameterized.class)
public class JunitTest{
private int number;
public JunitTest(int number) {
this.number = number;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized Number is : " + number);
}
}
testNG實現:
public class TestNGTestParameter {
@Test
@Parameters(value="number")
public void parameterIntTest(int number) {
System.out.println("Parameterized Number is : " + number);
}
}
XML文件的內容如下
<suite name="My test suite">
<test name="testing">
<parameter name="number" value="2"/>
<classes>
<class name="demo.testng.TestNGTestParameter" />
</classes>
</test>
</suite>
參數爲複雜的類型,TestNG使用@DataProvider註解來處理這種情況
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(Class clzz, String[] number) {
System.out.println("Parameterized Number is : " + number[0]);
System.out.println("Parameterized Number is : " + number[1]);
}
//This function will provide the patameter data
@DataProvider(name = "Data-Provider-Function")
public Object[][] parameterIntTestProvider() {
return new Object[][]{
{Vector.class, new String[] {"java.util.AbstractList",
"java.util.AbstractCollection"}},
{String.class, new String[] {"1", "2"}},
{Integer.class, new String[] {"1", "2"}}
};
}
依賴性測試表示方法是依賴性測試,它將在所需方法之前執行。 如果依賴方法失敗,則所有後續測試將會被跳過,不會被標記爲失敗。JUnit 4JUnit框架着重於測試隔離; 目前它不支持此功能。TestNG它使用“dependOnMethods”來實現依賴測試
@Test
public void method1() {
System.out.println("This is method 1");
}
@Test(dependsOnMethods={"method1"})
public void method2() {
System.out.println("This is method 2");
}
“method2()”只有在“method1()”運行成功的情況下才會執行,否則“method2()”將跳過測試
咳咳~ 基本就這些,根據自己的需求選擇一個合適的框架吧~