您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > TestNG
使用TestNG的新特性管理實際項目中的大量單元測試
作者:網(wǎng)絡轉(zhuǎn)載 發(fā)布時間:[ 2013/3/15 13:59:56 ] 推薦標簽:

TestNG 的示例代碼

TestNG 提供了從命令行運行測試用例的方法。下面將首先從命令行運行測試用例。假設有如下的測試用例組:

列表 1. TestNG 示例代碼

    
 package example1;

 import org.testng.annotations.*;

 public class SimpleTest {

  @Configuration(beforeTestClass = true)
  public void setUp() {
    // code that will be invoked when this test is instantiated
  }

  @Test(groups = { "HelloWorld" })
  public void helloWorldTest() {
    System.out.println("Hello World");
    throw new Error();
  }

  @Test(threadPoolSize = 10, invocationCount = 5, 
  timeOut = 1000, groups = { "multiple" })
  public void multiThreadTest() {
     System.out.println("MultiThread test");
  }

  @Test(groups = { "HelloNature" })
  public void helloNatureTest() {
     System.out.println("Hello Nature");
     throw new Error();
  }
 }

    


運行 TestNG 的 Ant 腳本

為了運行這組測試用例,構(gòu)建了如下的 Ant 運行腳本:

列表 2. 運行測試用例組的 Ant 腳本 build.xml 文件

    
 <project default="test">

  <path id="cp">
    <pathelement location=
    "c:/spark/eclipse/plugins/org.testng.eclipse_4.7.0.0/lib/testng-jdk15.jar"/>
    <pathelement location="c:"/>
  </path>
 
  <taskdef name="testng" classpathref="cp"
           classname="org.testng.TestNGAntTask" />

 <target name="test">
    <testng classpathref="cp" groups="HelloWorld, HelloNature">
      <classfileset dir="./" includes="example1/*.class"/>
    </testng>
  </target>
 
 </project>


運行之后的結(jié)果如下:

圖 1. 命令行運行 TestNG 的結(jié)果

運行完成之后,會在運行目錄下生成一個 test-output 目錄。如圖 2 所示:

圖 2. 生成的 test-output 目錄

該目錄中包含有 html 形式的運行結(jié)果的報告,通過命令 start test-outputindex.html 可以查看生成的測試報告。

上一頁123下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd