TestNG+Ant自动运行测试

TestNG+Ant自动运行测试
原文地址:http://magustest.com/blog/automationtesting/testng-ant/ 作者:进化的测试
    之前一篇博客分享了如何使用WebDriver+TestNG实现UI自动化,现在就要让自动化测试自己跑起来,不需要人工干预。需要用到Ant,以及一些定时任务工具,例如Linux的crontab。

如何在Ant的build.xml里面正确配置TestNG呢?
1. 在build.xml里面定义testng任务,在classpath里面指定testng.jar
<taskdef resource="testngtasks" classpath="${3rd.lib.dir}/testng.jar"/>
2. 在build.xml里面新建一个 叫regression的target

<project basedir="." default="regression" name="automation test">
    <property name="base.dir" value="/home/maguschen/workspaces/automation"/>
    <property name="testng.output.dir" value="${base.dir}/test-output"/>
    <property name="3rd.lib.dir" value="${base.dir}/libs"/>
    <property name="testng.file" value="testng.xml"/>
 
    <taskdef resource="testngtasks" classpath="${3rd.lib.dir}/testng.jar"/>
 
    <target name="clean">
        <delete dir="${base.dir}/bin"/>
    </target>
    <target name="compile" depends="clean">
        <mkdir dir="${base.dir}/bin"/>
        <javac srcdir="${base.dir}/src" destdir="${base.dir}/bin" classpathref="classes" includeantruntime="off" debug="on" debuglevel="lines,vars,source"/>
    </target>
 
    <path id="classes">
        <fileset dir="${3rd.lib.dir}" includes="*jar"/>
        <fileset dir="${3rd.lib.dir}" includes="*zip"/>
        <pathelement location="${base.dir}/bin"/>
    </path>
 
    <target name="regression" depends="compile">
        <testng outputdir="${testng.output.dir}" classpathref="classes" delegateCommandSystemProperties="true">
            <xmlfileset dir="${base.dir}" includes="${testng.file}"/>
        </testng>
    </target>
</project>
在target里面新建一个testng标签,里面需要设置的属性有:outputdir – 测试结果输出目录;classpathref – 那些自动化测试代码的目标路径,通常就是编译完成以后的那个目标路径,例如xxx/bin;delegateCommandSystemProperties – 接受传递命令行参数作为系统变量,这个设置为true可以在调用Ant的时候通过 -Dfoo=value 把参数传递给TestNG;里面还有一个xmlfileset节点,这个节点就是指定testng.xml文件的目录以及具体文件。
regression 的 target 有一个depends属性,意思就是跑regression之前需要做compile,而跑compile之前需要clean,应该很容易理解。直接在命令行里面运行:
ant -Durl=http://www.google.com -f build.xml regression
这里出现了 -Durl=http://www.google.com ,回到之前的配置,delegateCommandSystemProperties=”true”。如果这个参数为true,那么通过命令行的 -D 参数可以把一些变量传递给TestNG。譬如说TestNG的测试方法里面是有@Parameters({“url”})标签的话,就能通过ant -Durl=xxx 来传递url的值给到TestNG。例如

@Parameters({"url"})
@Test
public void search(String url){
    WebDriver driver = new FirefoxDriver();
    driver.get(url);
    WebElement query = driver.findElement(By.name("q"));
    query.sendKeys("Cheese");
    query.submit();
}


如果这样调用:ant -Durl=http://www.google.com -f build regression 。那么就会进入google的首页搜索,如果是: ant -Durl=http://magustest.com -f build regression ,那么就会找不到叫“q”的元素,呵呵。

接下来只要把cron job配好就完成了
15 * * * * ant -f /home/maguschen/workspaces/automation/build.xml regression

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值