?????? TestNG是一個(gè)不錯(cuò)的測試框架,尤其是用于模塊測試,以及大范圍的測試。相對(duì)于JUnit來說,更為靈活。隨著JUnit4的推出,很多功能都與TestNG相似,但相對(duì)于JUnit4,TestNG還是有很多部分是有區(qū)別的。
????? TestNG的IDE支持也不錯(cuò),對(duì)于Eclipse,Idea,Ant都有很好的支持。
????? 先來看一看怎么使用TestNG,當(dāng)然首先需要下載TestNG包。目前的版本為5.1,下載地址如下:
? ? ?
http://testng.org/doc/download.html ,也可以下載相應(yīng)的Eclipse插件。
????? 運(yùn)行TestNG,可以從命令行或者IDE,或者Ant中運(yùn)行。
????? 命令行:
?????
java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest
????? 對(duì)于大型的測試,需要定義一個(gè)xml文件,一般為testng.xml。
???
|
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > ? <suitename="Suite1"?? verbose="1" > <testname="Nopackage" > <classes> ???????<classname="NoPackageTest"? /> </classes> </test>
<testname="Regression1"?? > <classes> <classname="test.sample.ParameterSample"? /> <classname="test.sample.ParameterTest" /> </classes> </test> </suite> |
|
???? java org.testng.TestNG testng.xml
???? 當(dāng)然如果使用Eclipse插件,就簡單多了。
????? 下面來看一下,如何來實(shí)現(xiàn)測試的,與JUnit4 差不多(懷疑,JUnit4是不是有抄襲TestNG的成分)。
??? ? 聲明測試方法如下:
|
@Test public void testMethod1() { System.out.println("in testMethod1"); }
@Test public void testMethod2() { System.out.println("in testMethod2"); }
|
|
???? 基本上都是采用java5的注釋實(shí)現(xiàn)的。
???? 與JUnit4 不同在于,測試方法可以分組,它可以根據(jù)諸如運(yùn)行時(shí)間這樣的特征來對(duì)測試分類。
@Test(groups={"fun1","fun2"}) public void testMethod1() { System.out.println("in testMethod1"); }
@Test(groups={"fun1"}) public void testMethod2() { System.out.println("in testMethod2"); } |
????? 同JUnit4 一樣,同樣支持Before,After方法,如同setUp 和tearDown,不過TestNG更為靈活,支持各種簽名方式,如private,protected。
??? @BeforeMethod ??? protected void beforeMethod() { ??????? System.out.println("in beforeMethod"); ??? }
??? @AfterMethod ??? protected void afterMethod() { ??????? System.out.println("in afterMethod"); ??? }
|
???? 同樣也支持BeforeClass 和AfterClass,只執(zhí)行一次的方法,但是可以不需要使用static簽名
??? @BeforeClass ??? protected void beforeClassMethod() { ??????? System.out.println("in beforeClassMethod"); ??? }
??? @AfterClass ??? protected void afterClassMethod() { ??????? System.out.println("in afterClassMethod"); ??? }
|
???? 不同于JUnit4,TestNG提供了以下的特性:
????
依賴性測試
???? JUnit 框架想達(dá)到的一個(gè)目標(biāo)就是測試隔離。它的缺點(diǎn)是:人們很難確定測試用例執(zhí)行的順序,而這對(duì)于任何類型的依賴性測試都非常重要。開發(fā)者們使用了多種技術(shù)來解決這個(gè)問題,例如,按字母順序指定測試用例,或是更多地依靠 fixture 來適當(dāng)?shù)亟鉀Q問題。
????? 與 JUnit 不同,TestNG 利用
Test
注釋的
dependsOnMethods
屬性來應(yīng)對(duì)測試的依賴性問題。有了這個(gè)便利的特性,就可以輕松指定依賴方法。如以下定義:testMethod2依賴于testMethod1。
???
??? @Test ??? public void testMethod1() { ??????? System.out.println("in testMethod1"); ??? }
??? @Test(dependsOnMethods="testMethod1") ??? public void testMethod2() { ??????? System.out.println("in testMethod2"); ??? }
|
當(dāng)然如果testMethod1失敗的話,默認(rèn)testMethod2也不會(huì)執(zhí)行,不過只需要設(shè)置alwaysRun = true,則可以跳過testMethod1
??? @Test
??? public void testMethod1() {
??????? System.out.println("in testMethod1"); ??????? throw new RuntimeException("failed");
??? }
??? @Test(dependsOnMethods="testMethod1",alwaysRun = true)
??? public void testMethod2() {
??????? System.out.println("in testMethod2");
??? } |
????
失敗和重運(yùn)行
???? 在大型測試套件中,這種重新運(yùn)行失敗測試的能力顯得尤為方便。這是 TestNG 獨(dú)有的一個(gè)特性。在 JUnit 4 中,如果測試套件包括
1000 項(xiàng)測試,其中 3 項(xiàng)失敗,很可能就會(huì)迫使您重新運(yùn)行整個(gè)測試套件(修改錯(cuò)誤以后)。不用說,這樣的工作可能會(huì)耗費(fèi)幾個(gè)小時(shí)。
一旦 TestNG 中出現(xiàn)失敗,它就會(huì)創(chuàng)建一個(gè) XML 配置文件,對(duì)失敗的測試加以說明。如果利用這個(gè)文件執(zhí)行 TestNG 運(yùn)行程序,TestNG 就只 運(yùn)行失敗的測試。所以,在前面的例子里,您只需重新運(yùn)行那三個(gè)失敗的測試,而不是整個(gè)測試套件。可以看到以下的失敗文件,一般命名為testng-failed.xml,以后只需要運(yùn)行此文件就可以了。
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite thread-count="5" verbose="1" name="Failed suite [testng]" parallel="false" annotations="JDK5"> ? <test name="demo.testng.Test2(failed)" junit="false" parallel="false" annotations="JDK5"> ??? <classes> ????? <class name="demo.testng.Test2"> ??????? <methods> ????????? <include name="testMethod1"/> ????????? <include name="testMethod2"/> ????????? <include name="beforeClassMethod"/> ????????? <include name="afterClassMethod"/> ????????? <include name="beforeMethod"/> ????????? <include name="afterMethod"/> ??????? </methods> ????? </class> ??? </classes> ? </test> </suite>
|
???
參數(shù)化測試
??? TestNG 中另一個(gè)有趣的特性是
參數(shù)化測試。在 JUnit 中,如果您想改變某個(gè)受測方法的參數(shù)組,就只能給
每個(gè) 不同的參數(shù)組編寫一個(gè)測試用例。多數(shù)情況下,這不會(huì)帶來太多麻煩。然而,我們有時(shí)會(huì)碰到一些情況,對(duì)其中的業(yè)務(wù)邏輯,需要運(yùn)行的測試數(shù)目變化范圍很大。
??? 在這樣的情況下,使用 JUnit 的測試人員往往會(huì)轉(zhuǎn)而使用 FIT 這樣的框架,因?yàn)檫@樣就可以用表格數(shù)據(jù)驅(qū)動(dòng)測試。但是 TestNG
提供了開箱即用的類似特性。通過在 TestNG 的 XML
配置文件中放入?yún)?shù)化數(shù)據(jù),就可以對(duì)不同的數(shù)據(jù)集重用同一個(gè)測試用例,甚至有可能會(huì)得到不同的結(jié)果。這種技術(shù)完美地避免了
只能 假定一切正常的測試,或是沒有對(duì)邊界進(jìn)行有效驗(yàn)證的情況。
?? @Parameters( { "first-name" ??? }) ??? @Test(groups = { "param" ??? }) ??? public void testParm(String firstName) { ??????? System.out.println("invoked testString:" + firstName); ??????? assertEquals(firstName, "Test"); ??? }
|
在xml中設(shè)置相應(yīng)的參數(shù)值,可以放入suite下面或者test下面,如果同名,一般test下面的定義覆蓋suite定義。
<parameter name="first-name" value="Test"/>
|
??? 高級(jí)參數(shù)化測試
??? 盡管從一個(gè) XML 文件中抽取數(shù)據(jù)會(huì)很方便,但偶爾會(huì)有些測試需要有復(fù)雜類型,這些類型無法用
String
或原語值來表示。TestNG 可以通過它的
@DataProvider
注釋處理這樣的情況。
@DataProvider
注釋可以方便地把復(fù)雜參數(shù)類型映射到某個(gè)測試方法。例如,清單 7 中的
verifyHierarchy
測試中,我采用了重載的
buildHierarchy
方法,它可接收一個(gè)
Class
類型的數(shù)據(jù), 它斷言(asserting)
Hierarchy
的
getHierarchyClassNames()
方法應(yīng)該返回一個(gè)適當(dāng)?shù)淖址當(dāng)?shù)組:
package test.com.acme.da.ng;
import java.util.Vector;
import static org.testng.Assert.assertEquals; import org.testng.annotations.DataProvider; import org.testng.annotations.Test;
import com.acme.da.hierarchy.Hierarchy; import com.acme.da.hierarchy.HierarchyBuilder;
public class HierarchyTest {
@DataProvider(name = "class-hierarchies") public Object[][] dataValues(){ return new Object[][]{ {Vector.class, new String[] {"java.util.AbstractList", "java.util.AbstractCollection"}}, {String.class, new String[] {}} }; }
@Test(dataProvider = "class-hierarchies") public void verifyHierarchy(Class clzz, String[] names) throws Exception{ Hierarchy hier = HierarchyBuilder.buildHierarchy(clzz); assertEquals(hier.getHierarchyClassNames(), names, "values were not equal"); } } |
???? 當(dāng)然還有一些其他的特性,就不一一詳細(xì)說明了,有興趣可以參考相應(yīng)的testNG文檔。
????? JUnit 4 和 TestNG 在表面上是相似的。然而,設(shè)計(jì) JUnit 的目的是為了分析代碼單元,而 TestNG
的預(yù)期用途則針對(duì)高級(jí)測試。對(duì)于大型測試套件,我們不希望在某一項(xiàng)測試失敗時(shí)就得重新運(yùn)行數(shù)千項(xiàng)測試,TestNG
的靈活性在這里尤為有用。這兩個(gè)框架都有自己的優(yōu)勢,您可以隨意同時(shí)使用它們。
posted on 2006-10-05 15:44
布衣郎 閱讀(5793)
評(píng)論(5) 編輯 收藏 所屬分類:
單元測試