1. 程式人生 > >testNG簡易教程(含安裝,註解,配置檔案,測試報告等)

testNG簡易教程(含安裝,註解,配置檔案,測試報告等)

一、eclipse安裝TestNG外掛

1.線上安裝

點選eclipse頂部選單欄Help-->Install New SoftWare,再點選Add按鈕,Name:testNG,Location:http://beust.com/eclipse,等幾分鐘後出現testNG複選框,勾選安裝即可。

2.離線安裝

a.下載testNG安裝包:http://download.csdn.NET/detail/u012100968/9623613

b.將解壓後的檔案..\eclipse-testng離線包\features\目錄下的資料夾org.testng.eclipse_6.9.8.201510130443放到eclipse安裝路徑下的features目錄下

c.將解壓後的檔案..\eclipse-testng離線包\plugins\目錄下的資料夾org.testng.eclipse_6.9.8.201510130443放到eclipse安裝路徑下的plugins目錄下

d.重啟eclipse,驗證是否安裝成功,new-->other-->TestNG-->TestNG class

二、常見註解及xml配置項

1.註解:新建TestNG class的時候,可以看到有這些註解,百度了一下,發現解釋都非常拗口難懂,我們用程式碼測試下:


a.單個TestNG class中,各個被註解方法的執行順序:

package com.etyero.testcase;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;

/**
 * 各註解方法執行順序
 * 
 * */
public class NewTest01 {
	@Test
	public void f01() {
		System.out.println("this is NewTest01 f01...");
	}
	@Test
	public void f02() {
		System.out.println("this is NewTest01 f02...");
	}

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("this is NewTest01 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("this is NewTest01 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		System.out.println("this is NewTest01 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		System.out.println("this is NewTest01 afterTest...");
	}
	@BeforeClass
	public void beforeClass(){
		System.out.println("this is NewTest01 beforeClass...");
	}
	@AfterClass
	public void afterClass(){
		System.out.println("this is NewTest01 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest01 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest01 beforeSuite...");
	}
}

控制檯輸出:


測試結論:

單個TestNG class中,各註解方法的執行順序為:

beforeSuite->beforeTest->beforeClass->beforeMethod->測試方法->afterMethod->beforeMethod->下一個測試方法->afterMethod->afterClass->afterTest->afterSuite

從此我們大概可以看出各註解方法的作用域:beforeSuite和afterSuite是作用於測試套件 suite;beforeTest和afterTest是作用於test;beforeClass和afterClass是作用於測試類class;beforeMethod和afterMethod是作用於測試類中單個方法;

但是,除了beforeMethod和afterMehtod我們可以確定,是作用於單個class中的被@Test註解的測試方法外,其他的幾個註解,我們並不清楚在多個測試suite,test,class批量執行是什麼樣的順序。

b.多個測試suite,test,class批量執行,各個被註解方法的執行順序

準備四個測試類:

package com.etyero.testcase;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;

public class NewTest01 {
	@Test
	public void f01() {
		System.out.println("this is NewTest01 f01...");
	}
	@Test
	public void f02() {
		System.out.println("this is NewTest01 f02...");
	}

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("this is NewTest01 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("this is NewTest01 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		System.out.println("this is NewTest01 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		System.out.println("this is NewTest01 afterTest...");
	}
	@BeforeClass
	public void beforeClass(){
		System.out.println("this is NewTest01 beforeClass...");
	}
	@AfterClass
	public void afterClass(){
		System.out.println("this is NewTest01 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest01 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest01 beforeSuite...");
	}
}

package com.etyero.testcase;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest02 {
	@Test
	public void f01() {
		System.out.println("this is NewTest02 f01...");
	}

	@Test
	public void f02() {
		System.out.println("this is NewTest02 f02...");
	}

	@Test
	public void f03() {
		System.out.println("this is NewTest02 f03...");
	}

	@Test
	public void f04() {
		System.out.println("this is NewTest02 f04...");
	}
	@Test
	public void f05() {
		System.out.println("this is NewTest02 f05...");
	}
	@Test
	public void f06() {
		System.out.println("this is NewTest02 f06...");
	}
	public void f07() {
		System.out.println("this is NewTest02 f07...");
	}

	@BeforeMethod
	public void beforeMethod() {
		 System.out.println("this is NewTest02 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		 System.out.println("this is NewTest02 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		 System.out.println("this is NewTest02 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		 System.out.println("this is NewTest02 afterTest...");
	}

	@BeforeClass
	public void beforeClass() {
		 System.out.println("this is NewTest02 beforeClass...");
	}

	@AfterClass
	public void afterClass() {
		 System.out.println("this is NewTest02 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest02 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest02 beforeSuite...");
	}
}

package com.etyero.testcase;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;

public class NewTest03 {
	@Test
	public void f01() {
		System.out.println("this is NewTest03 f01...");
	}
	@Test
	public void f02() {
		System.out.println("this is NewTest03 f02...");
	}

	@Test
	public void f03() {
		System.out.println("this is NewTest03 f03...");
	}


	@BeforeMethod
	public void beforeMethod() {
		 System.out.println("this is NewTest03 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		 System.out.println("this is NewTest03 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		 System.out.println("this is NewTest03 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		 System.out.println("this is NewTest03 afterTest...");
	}

	@BeforeClass
	public void beforeClass() {
		 System.out.println("this is NewTest03 beforeClass...");
	}

	@AfterClass
	public void afterClass() {
		 System.out.println("this is NewTest03 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest03 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest03 beforeSuite...");
	}
}

package com.etyero.testcase;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;

public class NewTest04 {
	@Test
	public void f01() {
		System.out.println("this is NewTest04 f01...");
	}
	@Test
	public void f02() {
		System.out.println("this is NewTest04 f02...");
	}

	@Test
	public void f03() {
		System.out.println("this is NewTest04 f03...");
	}


	@BeforeMethod
	public void beforeMethod() {
		 System.out.println("this is NewTest04 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		 System.out.println("this is NewTest04 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		 System.out.println("this is NewTest04 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		 System.out.println("this is NewTest04 afterTest...");
	}

	@BeforeClass
	public void beforeClass() {
		 System.out.println("this is NewTest04 beforeClass...");
	}

	@AfterClass
	public void afterClass() {
		 System.out.println("this is NewTest04 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest04 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest04 beforeSuite...");
	}
}

準備第一個xml檔案,suite1.xml,該xml檔案中,NewTest01和NewTest02屬於同一個test節點;NewTest03為另一個test節點:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1">
	 <test name="test01">
        <classes>
            <class name="com.etyero.testcase.NewTest01"/>
            <class name="com.etyero.testcase.NewTest02"/>
        </classes>
    </test>
	<test name="test02">
        <classes>
            <class name="com.etyero.testcase.NewTest03"/>
        </classes>
    </test>
</suite>

第二個xml檔案,suite2.xml,這樣,就有兩個suite了:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite2">
	<test name="test03">
        <classes>
            <class name="com.etyero.testcase.NewTest04"/>
        </classes>
    </test>
</suite>

批量執行suite的xml檔案,allSuite.xml,該xml檔案執行suite1及suite2兩個xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="allSuites" allow-return-values="true">
  <suite-files>
    <suite-file path="suite1.xml" />
    <suite-file path="suite2.xml" />
  </suite-files>
</suite> 

執行結果:



測試結論:

beforeSuite和afterSuite被suite節點分割,單個suite節點內的所有beforeSuite方法先於其他所有方法執行,afterSuite後於其他所有方法執行;

beforeTest和afterTest被test節點分割,單個test節點內所有beforeTest方法先於其他方法執行,afterTest方法後於其他方法執行;

beforeClass和afterClass被class節點分割,以此形成閉環。

以上,是按我個人理解總結概述的,可能有點難理解,有時間的童鞋可以自己測試下,就很明白了。

c.忽略(禁用)測試,不想被執行的用例,可用@Test(enabled = false)來禁用該用例

package com.etyero.testcase;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest02 {
	
	/**
	 * enabled = false 表示用例尚未準備好,不會被執行
	 * 
	 * */
	 @Test(groups = {"systemTest"},enabled = false)
	public void f01() {
		System.out.println("this is NewTest02 f01...");
	}

	@BeforeMethod
	public void beforeMethod() {
		 System.out.println("this is NewTest02 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		 System.out.println("this is NewTest02 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		 System.out.println("this is NewTest02 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		 System.out.println("this is NewTest02 afterTest...");
	}

	@BeforeClass
	public void beforeClass() {
		 System.out.println("this is NewTest02 beforeClass...");
	}

	@AfterClass
	public void afterClass() {
		 System.out.println("this is NewTest02 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest02 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest02 beforeSuite...");
	}
}
d.依賴測試,某個用例可能依賴於上一個用例的執行結果,我們可以通過@Test(dependsOnMethods="所依賴的用例")來達成依賴測試。
package com.etyero.testcase;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest02 {


	@Test(groups = {"systemTest"})
	public void f02() {
		System.out.println("this is NewTest02 f02...");
	}

	@Test(groups = {"systemTest"},dependsOnMethods="f02")
	/**
	 * dependsOnMethods 測試用例之間存在依賴關係,f03執行之前,會先執行所依賴的f02測試方法
	 * 
	 * */
	public void f03() {
		System.out.println("this is NewTest02 f03...");
	}


	@BeforeMethod
	public void beforeMethod() {
		 System.out.println("this is NewTest02 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		 System.out.println("this is NewTest02 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		 System.out.println("this is NewTest02 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		 System.out.println("this is NewTest02 afterTest...");
	}

	@BeforeClass
	public void beforeClass() {
		 System.out.println("this is NewTest02 beforeClass...");
	}

	@AfterClass
	public void afterClass() {
		 System.out.println("this is NewTest02 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest02 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest02 beforeSuite...");
	}
}


2.配置檔案

上面已經粗略的涉及到xml配置檔案的使用,這裡再細化一點,列舉一些常用的測試配置項,某些xml配置是要結合程式碼註解來使用的

a.通過methods標籤指定要執行的測試方法

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1">

  <test name="test01">
        <classes>
		    <!--不指定執行方法,預設執行該測試類下所有@Test註解方法-->
            <class name="com.etyero.testcase.NewTest01"/>
			
			<!--通過methods標籤指定要執行該測試類中的@Test註解方法-->
            <class name="com.etyero.testcase.NewTest02">
                <methods>
                    <include name="f01" />
                    <include name="f02" />
                    <include name="f03" />
                    <include name="f04" />
                    <include name="f05" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

b.按組測試,測試中可能會將用例分組,這樣,我們就需要按組來測試

在測試類中註解@Test(groups = {"systemTest03"})

NewTest02:

package com.etyero.testcase;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest02 {
	@Test(groups = {"systemTest"})
	public void f01() {
		System.out.println("this is NewTest02 f01...");
	}

	@Test(groups = {"systemTest"})
	public void f02() {
		System.out.println("this is NewTest02 f02...");
	}

	@Test(groups = {"systemTest"})
	public void f03() {
		System.out.println("this is NewTest02 f03...");
	}

	@Test(groups = {"systemTest01"})
	public void f04() {
		System.out.println("this is NewTest02 f04...");
	}
	@Test(groups = {"systemTest01"})
	public void f05() {
		System.out.println("this is NewTest02 f05...");
	}
	@Test(groups = {"systemTest01"})
	public void f06() {
		System.out.println("this is NewTest02 f06...");
	}
	public void f07() {
		System.out.println("this is NewTest02 f07...");
	}

	@BeforeMethod
	public void beforeMethod() {
		 System.out.println("this is NewTest02 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		 System.out.println("this is NewTest02 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		 System.out.println("this is NewTest02 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		 System.out.println("this is NewTest02 afterTest...");
	}

	@BeforeClass
	public void beforeClass() {
		 System.out.println("this is NewTest02 beforeClass...");
	}

	@AfterClass
	public void afterClass() {
		 System.out.println("this is NewTest02 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest02 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest02 beforeSuite...");
	}
}

NewTest03:

package com.etyero.testcase;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;

public class NewTest03 {
	@Test(groups = {"systemTest03"})
	public void f01() {
		System.out.println("this is NewTest03 f01...");
	}
	@Test(groups = {"systemTest03"})
	public void f02() {
		System.out.println("this is NewTest03 f02...");
	}

	@Test
	public void f03() {
		System.out.println("this is NewTest03 f03...");
	}


	@BeforeMethod
	public void beforeMethod() {
		 System.out.println("this is NewTest03 beforeMethod...");
	}

	@AfterMethod
	public void afterMethod() {
		 System.out.println("this is NewTest03 afterMethod...");
	}

	@BeforeTest
	public void beforeTest() {
		 System.out.println("this is NewTest03 beforeTest...");
	}

	@AfterTest
	public void afterTest() {
		 System.out.println("this is NewTest03 afterTest...");
	}

	@BeforeClass
	public void beforeClass() {
		 System.out.println("this is NewTest03 beforeClass...");
	}

	@AfterClass
	public void afterClass() {
		 System.out.println("this is NewTest03 afterClass...");
	}
	@AfterSuite
	public void afterSuite(){
		System.out.println("this is NewTest03 afterSuite...");
	}
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("this is NewTest03 beforeSuite...");
	}
}

xml檔案:以下配置會執行NewTest02,NewTest03中的@Test(groups = {"systemTest03"})及@Test(groups = {"systemTest"})的所有測試方法。
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1">
	<test name="test01">
	    <!--配置分組所在類-->
	    <classes>
		  <class name="com.etyero.testcase.NewTest02"/>
		  <class name="com.etyero.testcase.NewTest03"/>
		</classes>
		<!--配置要執行的測試類中的groups-->
        <groups>
            <run>
              <include name="systemTest" />
	      <include name="systemTest03"/>
            </run>
        </groups>
		
	</test>
</suite>

c.XML實現引數化

package com.etyero.testcase;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestNGDataFromXMl {
	@Test
	@Parameters({"param01","param02"})
	public void xmlDataTest01(String param01,String param02) {
		System.out.println("param01=" + param01 + "----param02=" + param02);
	}
	
	@Test
	@Parameters({"param03","param04"})
	public void xmlDataTest02(String param03,String param04) {
		System.out.println("param03=" + param03 + "----param04=" + param04);
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite2">
    <!--name對應@Paramsters中定義的引數名-->
    <parameter name="param01" value="1" />
	<parameter name="param02" value="2" />
	<parameter name="param03" value="3"/>
	<parameter name="param04" value="4"/>
	<test name="test">
		<classes>
		  <class name="com.etyero.testcase.TestNGDataFromXMl"/>
		</classes>
	</test>
</suite>

另外,xml引數化的優勢在於程式碼和測試資料分離,方便維護,但是缺點是難以定義較複雜的引數,此時我們可能需要另一種引數化的方式:@DataProvider
package com.etyero.testcase;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGDataFromDataProvider {
	@DataProvider(name = "data1")
	// 通過dataProvider引數化測試資料,必須返回Object[][],name用作引用
	public Object[][] createData01() {
		return new Object[][] { { "A", "a1", "a2" }, { "B", "b1", "b2" }, { "C", "c1", "c2" } };
	}

	@DataProvider(name = "data2")
	//可以定義多組引數化資料
	public Object[][] createData02() {
		return new Object[][] { { "D", "d1", "d2" }, { "E", "e1", "e2" }, { "F", "f1", "f2" } };
	}

	@Test(dataProvider = "data1")
	public void dataProviderTest01(String name, String name1, String name2) {
		// 會自動迴圈取出data1所有的引數
		System.out.println("name=" + name + "----name1=" + name1 + "----name2=" + name2);
	}

	@Test(dataProvider = "data1")
	//同一組引數化資料可以被多個測試方法引用
	public void dataProviderTest02(String name, String name1, String name2) {
		System.out.println("name=" + name + "----name1=" + name1 + "----name2=" + name2);
	}
	
	@Test(dataProvider = "data2")
	public void dataProviderTest03(String name, String name1, String name2) {
		System.out.println("name=" + name + "----name1=" + name1 + "----name2=" + name2);
	}
}

三、測試報告

執行完TestNG用例後,會預設在工程的test-output(預設目錄)下生成測試報告,沒有的童鞋執行完重新整理一下就好了。

預設的測試報告如下,樣式較為簡陋,但基本資訊都有,可以自己點吧點吧,看看報告的各種資訊,在此不做贅述


現在我們來用testng-xslt做些美化: 1、下載testng-xslt包,http://testng-xslt.googlecode.com/files/testng-xslt-1.1.zip,也可到我的網盤下載:http://pan.baidu.com/s/1qXUwZUG
2、複製testng-results.xsl(testng-xslt-1.1\src\main\resources)檔案到test-output目錄下
3、複製saxon-8.7.jar(testng-xslt-1.1\lib)檔案到project的lib目錄下(沒有的話,自己新建,一般是用來放project所需的各種jar包)
4、安裝ant並配置好環境變數
5、在project目錄下,新建build.xml檔案,內容如下:
<project name="testNG" basedir="." >
    <property name="lib.dir" value="lib" />
    <path id="test.classpath" >
        <!-- saxon.jar包位置-->
        <fileset dir="${lib.dir}" includes="*.jar" />
    </path>
    <target name="transform" >
    <!--自定義project目錄-->
    <property name="project.dir" value="E:\eclipseWorkSpace\webUITest\test-output" />
      <!--定義測試結果寫入檔案,樣式,樣式轉換之後的檔案-->
    <xslt in="${project.dir}\testng-results.xml" style="${project.dir}\testng-results.xsl" out="${project.dir}/indexAnt.html" >
          <param name="testNgXslt.outputDir" expression="${project.dir}" />
          <classpath refid="test.classpath" />
    </xslt>
    </target>
</project>
4.5兩步前面做過持續整合的童鞋應該很熟悉了。 cmd進入到build.xml所在目錄,ant transform(這個為xml檔案中的target name,要對應),執行完後,開啟對應的html,效果如圖: 以上,為TestNG從安裝到使用的一個簡易教程。