ANT+SVN安卓测试框架:ANT部分二

这部分主要介绍bat文件调用的ant

          应该首先阅读部分一:http://assistne.iteye.com/blog/1961243

有了部分一的基础后,直接分析代码:

-------------run.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 本文件应该置于测试工程目录下,与测试工程的build.xml位于同目录 -->
<!-- 由批处理文件调用run方法 -->
<project name="test" default="run">
    <!-- 默认目录结构:files(默认生成)=>被测试工程文件夹=>tests(测试工程文件夹) -->
    <!-- 改变目录结构需要更改所有相对路径 -->
    <!-- .properties文件应该与files同级 -->
    <!-- 本文件应该在测试工程文件夹 -->
  	<property file="../../../email.properties"/>
    <property file="../../../path.properties" />
    <!-- 定义一个PATH元素,告诉ANT 到哪找SVNANT的类库 -->
	<path id="path.svnant">   
          <!-- SVNANT通讯包 -->
          <!-- 若将包放到ANT_HOME下的lib目录下则不需要此定义 -->
          <pathelement location="../../../svnant_lib/svnant.jar"/>  
          <pathelement location="../../../svnant_lib/svnClientAdapter.jar"/>          
          <pathelement location="../../../svnant_lib/svnkit.jar"/>    
          <pathelement location="../../../svnant_lib/svnjavahl.jar"/>     
    </path>
    <!-- 发送邮件 属性在email.properties设置 -->
    <target name="send-email">
	   	<mail 
	   	  from="${send}"
	   	  mailhost="${mailhost}"
	   	  user="${user}"
	   	  password="${password}"
	      tolist="${tolist}"
	      subject="${title}"
	      files="${files}"/>
   	</target>
    <typedef
          resource="org/tigris/subversion/svnant/svnantlib.xml"
          classpathref="path.svnant"/>  
    <!--引用 svnantlib.xml ,其中定义了ANT的SVN命令,见下方详解。--> 
    <!--定义svnSetting ,新的设置SVN属性方式--> 
    <!-- 通过SVNKIT模式(即设置为TRUE),而不是JAVAHL模式-->
    <svnSetting id="svn.settings"    
        svnkit="true"   
        javahl="false"
        username=""
        password="">
    </svnSetting>
    <!-- 更新 比对SVN服务器中的版本,路径属性在path.properties设置-->
 	<target name="update">
 	    <!-- 全局日志开始记录 -->
 	    <record name="${alllog}" action="start"/>
 	    <svn refid="svn.settings">
 	        <update dir="../../../files"></update>
 	    </svn>
 	    <echo message="update success!"></echo>
 	</target>
 	<!-- 复制local.properties,保证项目中的sdk.dir与本机sdk路径一样-->
 	<!-- 与android update project 作用类似 -->
 	<target name="copy_properties" depends="update">
 	    <!-- 复制依赖与固定的文件结构,一旦改变文件结构需要重写这里 -->
 	    <copy file="../../../local.properties" tofile="${local_p1}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p2}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p3}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p4}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p5}" overwrite="true"/>
 	    <echo message="copy success"/>
 	</target>
    <!-- bat文件中调用的target -->
    <!-- 先更新,复制local.properties,运行测试工程的build.xml中的main -->
    <!-- 需要run.xml与build.xml同目录,build.xml才能正常调用main -->
    <target name="run" depends="update,copy_properties">
        <!-- 安卓日志开始 -->
        <record name="${androidlog}" action="start"/>
        <!-- 连接测试工程 -->
        <!-- 需要在测试工程的build.xml定义main方法 -->
        <!-- main简单调用安卓方法,debug,installt,test ,depends实现即可-->
 	    	<ant antfile="build.xml" target="main"></ant>
 	    <record name="${androidlog}" action="stop"/>
 	    <record name="${alllog}" action="stop"/>
 	    <!-- 发送邮件 -->
 	    <!-- 批处理直接调用ant -f run.xml send-email会出错,所以并入此处 -->
 	    <!-- 必须指定antfile否则出错 -->
 	    <ant antfile="run.xml" target="send-email"/>
    </target>
</project>

 由于是安卓测试,默认文件结构是被测试工程目录下有tests的文件夹,测试工程则放在tests下,上述代码run.xml则需要放在tests下。本文件依赖于相对路径,详细阅读注释。

代码分析:

日志记录:

        <record name="${androidlog}" action="start"/>
        <record name="${androidlog}" action="stop"/>

 该标签属于ant自带标签,详细自查ant文档

       name:日志文件名(包括路径,无路径则表示在当前路径,一般是xxxx.txt)

       actino:控制日志开始/结束

更新:

<!-- 更新 比对SVN服务器中的版本,路径属性在path.properties设置-->
 	<target name="update">
 	    <!-- 全局日志开始记录 -->
 	    <record name="${alllog}" action="start"/>
 	    <svn refid="svn.settings">
 	        <update dir="../../../files"></update>
 	    </svn>
 	    <echo message="update success!"></echo>
 	</target>

 与部分一中的checkout.xml类似,svn更新功能,比对本地文件与版本库文件的差异,更新本地文件。

注:<update>中有dir属性,为相对路径,依赖于文件结构

发邮件:

  1. 要实现ant发邮件功能需要下载功能包,在ant文档中查<mail>标签,知道该标签依赖于2个包
mail.jar Mail task with Mime encoding, and the MimeMail task http://www.oracle.com/technetwork/java/index-jsp-139225.html
activation.jar Mail task with Mime encoding, and the MimeMail task http://www.oracle.com/technetwork/java/javase/jaf-135115.html

下载后放到ant下的lib目录下,即可以使用<mail>标签

    <!-- 发送邮件 属性在email.properties设置 -->
    <target name="send-email">
	   	<mail 
	   	  from="${send}"
	   	  mailhost="${mailhost}"
	   	  user="${user}"
	   	  password="${password}"
	      tolist="${tolist}"
	      subject="${title}"
	      files="${files}"/>
    </target>

 详细使用查看文档

  • from:发件人名称,可以不设置;
  • mailhost:使用代理的SMTP,可以理解成使用什么邮箱发邮件,126邮箱的SMTP为smtp.126.com
  • user:邮箱的用户名,相当于它需要先登录mailhost指定的邮箱
  • password:邮箱的密码
  • tolist:接受人,可以是多个
  • subject:邮件标题
  • files:附件,可以是多个

注:上面代码中使用的email.properties中的属性,email.properties代码不再贴出,自行建立,修改。

复制文件:

 	<!-- 复制local.properties,保证项目中的sdk.dir与本机sdk路径一样-->
 	<!-- 与android update project 作用类似 -->
 	<target name="copy_properties" depends="update">
 	    <!-- 复制依赖与固定的文件结构,一旦改变文件结构需要重写这里 -->
 	    <copy file="../../../local.properties" tofile="${local_p1}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p2}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p3}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p4}" overwrite="true"/>
 	    <copy file="../../../local.properties" tofile="${local_p5}" overwrite="true"/>
 	    <echo message="copy success"/>
 	</target>

 针对安卓测试需要在local.properties中指定本机的sdk,而版本库中的sdk不一定与本机一致,所以添加此target。依赖与固定的文件结构,根据不同的测试实例需要修改。

//TODO 此处也是本框架需要改进的地方

运行:

    <target name="run" depends="update,copy_properties">
        <!-- 安卓日志开始 -->
        <record name="${androidlog}" action="start"/>
        <!-- 连接测试工程 -->
        <!-- 需要在测试工程的build.xml定义main方法 -->
        <!-- main简单调用安卓方法,debug,installt,test ,depends实现即可-->
 	    	<ant antfile="build.xml" target="main"></ant>
 	    <record name="${androidlog}" action="stop"/>
 	    <record name="${alllog}" action="stop"/>
 	    <!-- 发送邮件 -->
 	    <!-- 批处理直接调用ant -f run.xml send-email会出错,所以并入此处 -->
 	    <!-- 必须指定antfile否则出错 -->
 	    <ant antfile="run.xml" target="send-email"/>
    </target>

 

此target为调用安卓测试工程的build.xml运行测试,具体build.xml中如何测试本文不作讨论。

猜你喜欢

转载自assistne.iteye.com/blog/1961251
ANT