Sky's blog

          我和我追逐的夢

          常用鏈接

          統計

          其他鏈接

          友情鏈接

          最新評論

          TestNG官方文檔中文版(9)-重復失敗測試,junit測試,jdk1.4支持,編程式調用和Beanshell

          5.10 - Rerunning failed tests

          套件中的測試失敗時,每次testNG都會在輸出目錄中創建一個名為testng-failed.xml的文件。這個xml文件包含只重新運行這些失敗的測試方法的必要信息,容許只運行這些失敗的測試而不必運行全部測試。因此,一種典型的情況將是這樣:

          java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs testng.xml
          java 
          -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs test-outputs\testng-failed.xml

          注意testng-failed.xml將包含所有必要的依賴方法,所以可以保證運行失敗的方法而不運行任何被跳過的(失敗)方法。

          5.11 - JUnit tests

          TestNG可以運行junit測試。所需要的只是在testng.classNames屬性中指定junit測試類,并設置testng.junit屬性為true。

          <test name="Test1"   junit="true">
            
          <classes>
              
          <!--  -->

          這種情況下TestNG的行為類似jnit:

              * 類中所有以test*開頭的方法將被運行。
              * 如果測試類中有方法setUp(), 將在每次測試方法調用前被執行。
              * 如果測試類中有方法tearDown(),將在每次測試方法調用后被執行。

          5.12 - JDK 1.4

          TestNG也可以在JDK1.4下工作。在這種情況下,需要使用發布的jdk1.4的jar文件(名為testng-...-jdk14.jar)。唯一的差別是在于注解,jdk1.4下使用流行的XDoclet javadoc注解語法:

          public class SimpleTest {
           
          /**
            * @testng.before-class = "true"
            
          */
            
          public void setUp() {
              
          // code that will be invoked when this test is instantiated
           }
           
          /**
            * @testng.test groups = "functest" dependsOnGroups = "group1,group2"
            
          */
            
          public void testItWorks() {
              
          // your test code
           }
          }

          javadoc語法的規則非常簡潔,和jdk1.5注解的唯一差別是數組串數組需要特別寫成單獨的,逗號或空格分隔的字符串。雖然值周圍的雙引號是可選的,但還是建議在任何情況下都使用雙引號,以保證將來遷移到jdk1.5時可以比較容易。

          同樣需要在<testng>的ant任務中指明sourcedir屬性(或者在命令行中使用-sourcedir),以便testNG可以找到你的測試文件的源代碼來解析javadoc注解。

          這里是jdk1.4和jdk5注解的語法對照表:

              (表格在blog中不好排版,不在這里發了,詳細內容請參考官方文檔的原文:http://testng.org/doc/documentation-main.html#jdk-14。)

          更多jdk1.4的支持范例,請參考發行包中的test-14文件夾,這里包含全部的JDK 1.5測試對應的使用javadoc注解的內容。


          5.13 - Running TestNG programmatically

          在自己的程序中調用testNG也很簡單:

          TestListenerAdapter tla = new TestListenerAdapter();
          TestNG testng 
          = new TestNG();
          testng.setTestClasses(
          new Class[] { Run2.class });
          testng.addListener(tla);
          testng.run();

          這個范例創建了一個TestNG對象并運行測試類Run2。還增加了一個TestListener。你可以使用適配器類org.testng.TestListenerAdapter或自己實現org.testng.ITestListener。這個接口包含多個回調方法,使得可以追蹤測試的開始,成功,失敗等等。

          類似的,可以使用testng.xml文件調用TestNG或者自己創建一個虛擬的testng.xml文件。為了做到這點,需要使用org.testng.xml包的類:XmlClass, XmlTest, 等等。每個類對應他們xml標簽。

          例如,假設你想創建下面的虛擬文件:

          <suite name="TmpSuite" >
            
          <test name="TmpTest" >
              
          <classes>
                
          <class name="test.failures.Child"  />
              
          <classes>
              
          </test>
          </suite>

          你將使用下面的代碼:

          XmlSuite suite = new XmlSuite();
          suite.setName(
          "TmpSuite");

          XmlTest test 
          = new XmlTest(suite);
          test.setName(
          "TmpTest");
          List
          <XmlClass> classes = new ArrayList<XmlClass>();
          classes.add(
          new XmlClass("test.failures.Child"));
          test.setXmlClasses(classes) ;

          然后你可以將XmlSuite傳遞給TestNG:

          List<XmlSuite> suites = new ArrayList<XmlSuite>();
          suites.add(suite);
          TestNG tng 
          = new TestNG();
          tng.setXmlSuites(suites);
          tng.run();

          完整的API請參考javadoc。


          5.14 - BeanShell and advanced group selection

          如果testng.xml中的<include>和<exclude>標簽還不足夠滿足你的需要,你可以使用BeanShell表達式來決定是否需要將一個特定的測試方法包含在測試操作中。只需要在<test>標簽下指定這個表達式:

            <test name="BeanShell test">
             
          <method-selectors>
               
          <method-selector>
                 
          <script language="beanshell"><![CDATA[
                   groups.containsKey("test1")
                 
          ]]></script>
               
          </method-selector>
             
          </method-selectors>
            
          <!--  -->

          當發現testng.xml中有<script>標簽,TestNG將忽略當前<test>標簽中的以后的組和方法的<include>和<exclude>標簽:BeanShell表達式將是決定一個測試方法是否包含的唯一方法。

          這里有一些BeanShell腳本的額外信息:

              * 必須返回boolean值。除了這個約束,任何有效的BeanShell代碼都被容許.(例如,你可能想在工作日返回true而在周末返回false,這將容許你更加日期不同差異性的運行測試。

              * TestNG為了便利定義了以下變量:

                  java.lang.reflect.Method method:  當前測試方法
                  org.testng.ITestNGMethod testngMethod:  當前測試方法的描述
                  java.util.Map<String, String> groups:  當前測試方法所屬組的Map

              * 你可能需要在你的表達式前后增加CDATA聲明(如上面所示)以避免討厭的xml轉義字符。

          posted on 2008-09-05 15:00 sky ao 閱讀(2275) 評論(1)  編輯  收藏 所屬分類: software test

          評論

          # re: TestNG官方文檔中文版(9)-重復失敗測試,junit測試,jdk1.4支持,編程式調用和Beanshell 2008-12-09 11:09 sagittarius

          你的文章翻譯的很棒,一直在看支持你。。  回復  更多評論   

          主站蜘蛛池模板: 永仁县| 琼中| 三都| 广河县| 中西区| 阳谷县| 奎屯市| 乐昌市| 南投县| 阿尔山市| 开封市| 若羌县| 普陀区| 通辽市| 平原县| 塔河县| 新疆| 新干县| 化德县| 山西省| 和田县| 当雄县| 仪征市| 旬邑县| 遂平县| 五峰| 上杭县| 乌拉特中旗| 凤翔县| 济阳县| 黔江区| 庆城县| 太湖县| 阿坝县| 景泰县| 霍山县| 虹口区| 南城县| 原阳县| 资兴市| 汝州市|