????? TestNG的IDE支持也不錯,對于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
????? 對于大型的測試,需要定義一個xml文件,一般為testng.xml。
???
???? java org.testng.TestNG testng.xml
???? 當(dāng)然如果使用Eclipse插件,就簡單多了。
????? 下面來看一下,如何來實(shí)現(xiàn)測試的,與JUnit4 差不多(懷疑,JUnit4是不是有抄襲TestNG的成分)。
??? ? 聲明測試方法如下:
|
???? 基本上都是采用java5的注釋實(shí)現(xiàn)的。
???? 與JUnit4 不同在于,測試方法可以分組,它可以根據(jù)諸如運(yùn)行時間這樣的特征來對測試分類。
@Test(groups={"fun1","fun2"}) |
????? 同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á)到的一個目標(biāo)就是測試隔離。它的缺點(diǎn)是:人們很難確定測試用例執(zhí)行的順序,而這對于任何類型的依賴性測試都非常重要。開發(fā)者們使用了多種技術(shù)來解決這個問題,例如,按字母順序指定測試用例,或是更多地依靠 fixture 來適當(dāng)?shù)亟鉀Q問題。
????? 與 JUnit 不同,TestNG 利用
Test
注釋的 dependsOnMethods
屬性來應(yīng)對測試的依賴性問題。有了這個便利的特性,就可以輕松指定依賴方法。如以下定義:testMethod2依賴于testMethod1。???
??? @Test ??? public void testMethod1() { ??????? System.out.println("in testMethod1"); ??? } ??? @Test(dependsOnMethods="testMethod1") ??? public void testMethod2() { ??????? System.out.println("in testMethod2"); ??? } |
??? @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ú)有的一個特性。在 JUnit 4 中,如果測試套件包括 1000 項(xiàng)測試,其中 3 項(xiàng)失敗,很可能就會迫使您重新運(yùn)行整個測試套件(修改錯誤以后)。不用說,這樣的工作可能會耗費(fèi)幾個小時。
一旦 TestNG 中出現(xiàn)失敗,它就會創(chuàng)建一個 XML 配置文件,對失敗的測試加以說明。如果利用這個文件執(zhí)行 TestNG 運(yùn)行程序,TestNG 就只 運(yùn)行失敗的測試。所以,在前面的例子里,您只需重新運(yùn)行那三個失敗的測試,而不是整個測試套件。可以看到以下的失敗文件,一般命名為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 中另一個有趣的特性是參數(shù)化測試。在 JUnit 中,如果您想改變某個受測方法的參數(shù)組,就只能給每個 不同的參數(shù)組編寫一個測試用例。多數(shù)情況下,這不會帶來太多麻煩。然而,我們有時會碰到一些情況,對其中的業(yè)務(wù)邏輯,需要運(yùn)行的測試數(shù)目變化范圍很大。
??? 在這樣的情況下,使用 JUnit 的測試人員往往會轉(zhuǎn)而使用 FIT 這樣的框架,因?yàn)檫@樣就可以用表格數(shù)據(jù)驅(qū)動測試。但是 TestNG 提供了開箱即用的類似特性。通過在 TestNG 的 XML 配置文件中放入?yún)?shù)化數(shù)據(jù),就可以對不同的數(shù)據(jù)集重用同一個測試用例,甚至有可能會得到不同的結(jié)果。這種技術(shù)完美地避免了只能 假定一切正常的測試,或是沒有對邊界進(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"/> |
??? 高級參數(shù)化測試
??? 盡管從一個 XML 文件中抽取數(shù)據(jù)會很方便,但偶爾會有些測試需要有復(fù)雜類型,這些類型無法用
String
或原語值來表示。TestNG 可以通過它的 @DataProvider
注釋處理這樣的情況。@DataProvider
注釋可以方便地把復(fù)雜參數(shù)類型映射到某個測試方法。例如,清單 7 中的 verifyHierarchy
測試中,我采用了重載的 buildHierarchy
方法,它可接收一個 Class
類型的數(shù)據(jù), 它斷言(asserting)Hierarchy
的 getHierarchyClassNames()
方法應(yīng)該返回一個適當(dāng)?shù)淖址當(dāng)?shù)組:package test.com.acme.da.ng; |
???? 當(dāng)然還有一些其他的特性,就不一一詳細(xì)說明了,有興趣可以參考相應(yīng)的testNG文檔。
????? JUnit 4 和 TestNG 在表面上是相似的。然而,設(shè)計 JUnit 的目的是為了分析代碼單元,而 TestNG 的預(yù)期用途則針對高級測試。對于大型測試套件,我們不希望在某一項(xiàng)測試失敗時就得重新運(yùn)行數(shù)千項(xiàng)測試,TestNG 的靈活性在這里尤為有用。這兩個框架都有自己的優(yōu)勢,您可以隨意同時使用它們。