(一)使用 TestNG 的新特性管理實際項目中的大量單元測試

發表于:2009-04-02來源:作者:點擊數: 標簽:testngTestNG管理單元項目
隨著項目的成長, 單元測試 的數量會迅猛增長。這就帶來不少問題。首先數量巨大的單元測試難于管理,運行一遍耗時巨大。其次,有時某個微小改動可能只需要運行某個測試文件中的部分單元測試就可以,這時重新運行全部 測試用例 就沒有必要了。其三,大多數項目
隨著項目的成長,單元測試的數量會迅猛增長。這就帶來不少問題。首先數量巨大的單元測試難于管理,運行一遍耗時巨大。其次,有時某個微小改動可能只需要運行某個測試文件中的部分單元測試就可以,這時重新運行全部測試用例就沒有必要了。其三,大多數項目需要用到多線程特性,為使用了多線程特性的代碼寫測試用例相當麻煩且容易出錯。本文將利用 TestNG 提供的新特性,解決以上提到的問題。
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 腳本

為了運行這組測試用例,構建了如下的 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>
 
 

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97