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
 
origin:http://www.yiibai.com/html/testng/2013/0916310.html
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值