現在位置:首頁 > Java技術 > TestNG > TestNG插件與ANT

TestNG插件與ANT

來源:原創文章    由 極客書 更新版本    瀏覽:人次

在這個例子中,我們將演示如何使用ANT運行TestNG。讓我們遵循的步驟:

步驟1:下載Apache Ant

下載 Apache Ant

OS 壓縮文件名
Windows apache-ant-1.8.4-bin.zip
Linux apache-ant-1.8.4-bin.tar.gz
Mac apache-ant-1.8.4-bin.tar.gz

步驟2:設置Ant環境

設置ANT_HOME環境變量指向參考基本目錄的位置,ANT庫存儲在您的機器上。例如,我們已經存儲了Ant庫apache-ant-1.8.4,各種操作係統上的文件夾如下:

OS 輸出
Windows 設置環境變量 ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4
Linux export ANT_HOME=/usr/local/\apache-ant-1.8.4
Mac export ANT_HOME=/Library/\apache-ant-1.8.4

 

附加的Ant編譯係統路徑位置,在不同的操作係統如下:

OS 輸出
Windows 追加字符串;%ANT_HOME\bin 係統變量的結尾
Linux export PATH=$PATH:$ANT_HOME/bin/
Mac not required

第3步:下載TestNG

下載http://www.testng.org.

OS Archive name
Windows testng-6.8.jar
Linux testng-6.8.jar
Mac testng-6.8.jar

第4步:創建項目結構

  • 創建文件夾 TestNGWithAnt 在C:\ > TestNG_WORKSPACE

  • 創建文件夾 src 在 C:\ > TestNG_WORKSPACE > TestNGWithAnt

  • 創建文件夾 test 在 C:\ > TestNG_WORKSPACE > TestNGWithAnt

  • 創建文件夾 lib 在 C:\ > TestNG_WORKSPACE > TestNGWithAnt

  • 創建MessageUtil 類在 C:\ > TestNG_WORKSPACE > TestNGWithAnt > src 文件夾.

/*
* This class prints the given message on console.
*/
public class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   public MessageUtil(String message){
      this.message = message; 
   }

   // prints the message
   public void printMessage(){
      System.out.println(message);
      return message;
   }   

   // add "Hi!" to the message
   public String salutationMessage(){
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }   
}  	
  • 創建TestMessageUtil 類在 C:\ > TestNG_WORKSPACE > TestNGWithAnt > src 目錄.

import org.testng.Assert;
import org.testng.annotations.Test;


public class TestMessageUtil {
    String message = "Manisha";	
    MessageUtil messageUtil = new MessageUtil(message);
	   
    @Test
    public void testPrintMessage() {	
        System.out.println("Inside testPrintMessage()");     
        Assert.assertEquals(message,messageUtil.printMessage());
    }

    @Test
    public void testSalutationMessage() {
        System.out.println("Inside testSalutationMessage()");
        message = "Hi!" + "Manisha";
        Assert.assertEquals(message,messageUtil.salutationMessage());
    }
}
  • 拷貝 testng-6.8.jar 到 C:\ > TestNG_WORKSPACE > TestNGWithAnt > lib 文件夾

創建 ANT build.xml

首先,我們需要定義TestNG的ant任務如下:

<taskdef name="testng" classname="org.testng.TestNGAntTask">
    <classpath>
      <pathelement location="lib/testng-6.8.jar"/>
    </classpath>
  </taskdef>

然後我們使用 <testng> TestNG的測試案例Ant來執行任務。

C:\ > TestNG_WORKSPACE > TestNGWithAnt >\ build.xml 內容如下:

<project name="TestNGTest" default="test" basedir=".">
<!-- Define <testng> task -->
  <taskdef name="testng" classname="org.testng.TestNGAntTask">
    <classpath>
      <pathelement location="lib/testng-6.8.jar"/>
    </classpath>
  </taskdef>
   <property name="testdir" location="test" />
   <property name="srcdir" location="src" />
   <property name="libdir" location="lib" />
   <property name="full-compile" value="true" />
   <path id="classpath.base"/>
   <path id="classpath.test">
       <fileset dir="${libdir}">
         <include name="**/*.jar" />
      </fileset>
      <pathelement location="${testdir}" />
      <pathelement location="${srcdir}" />
      <path refid="classpath.base" />
   </path>
   <target name="clean" >
      <delete verbose="${full-compile}">
         <fileset dir="${testdir}" includes="**/*.class" />
      </delete>
   </target>
   <target name="compile" depends="clean">
      <javac srcdir="${srcdir}" destdir="${testdir}" 
         verbose="${full-compile}">
         <classpath refid="classpath.test"/>
      </javac>
   </target>
   <target name="test" depends="compile">
	<testng outputdir="${testdir}" classpathref="classpath.test"> 
      <xmlfileset dir="${srcdir}" includes="testng.xml"/> 
    </testng>
   </target>
</project>

執行下列ant命令。

C:\TestNG_WORKSPACE\TestNGWithAnt>ant

驗證輸出

test:
   [testng] [TestNG] Running:
   [testng]   C:\TestNG_WORKSPACE\TestNGWithAnt\src\testng.xml
   [testng] 
   [testng] Inside testPrintMessage()
   [testng] Manisha
   [testng] Inside testSalutationMessage()
   [testng] Hi!Manisha
   [testng] 
   [testng] ===============================================
   [testng] Plug ANT test Suite
   [testng] Total tests run: 2, Failures: 0, Skips: 0
   [testng] ===============================================
   [testng] 

BUILD SUCCESSFUL
Total time: 1 second

本站文章除注明轉載外,均為本站原創或編譯
歡迎任何形式的轉載,但請務必注明出處,尊重他人勞動,傳播學習教學;
轉載請注明:文章轉載自:極客書 [http://www.gitbook.net]
本文標題:TestNG插件與ANT
轉載請保留原文鏈接:http://www.gitbook.net/html/testng/2013/0916310.html
上一篇:TestNG Junit報告      下一篇:TestNG Eclipse插件