2007年10月16日星期二

使用Ant和JUnit自动构建测试


一个简化的自动化构建过程包括:
  1. 从存储库取得最新的代码
  2. 构建代码
  3. 记录构建版本号
  4. 运行单元测试
  5. 上传部署版本
  6. 发送电子邮件
以上步骤简化了部署前单元测试、部署及部署后单元测试三个过程。
  1. 从存储库取得最新的代码
     <cvs cvsRoot=":pserver:anoncvs@cvs.apache.org:/home/cvspublic"
    package="moduleName"
    dest="${ws.dir}"
    /> <!-- 执行cvs check out-->
    <cvs dest="${ws.dir}" command="update"/> <!-- 执行cvs update -->

  2. 构建代码
    执行自定义Ant构建任务。

  3. 记录构建版本号
     <buildnumber file="mybuild.number"/>

    <target name="baseline" description="Record build information"> 
    <!-- The name of the file that holds the build information. If no such file exists, a new
    one gets created. -->
    <propertyfile file="${destination.dir}/build.info">
    <!-- Initial build number is 0001. Then, any subsequent build increments
    this number by one each time. -->

    <entry default="0001" key="build.number" operation="+" pattern="0000" type="int" />
    <!-- Records the current time to the same file. -->
    <entry default="now" key="build.time" pattern=" yyyy.MM.dd-HH:mm" type="date" />

    </propertyfile>

    </target>


  4. 运行单元测试
    <!-- ========================================
                  target: auto test all test case and output report file                     
                  ===================================== -->
        <target name="junit and report" depends="test init, all compile">
            <junit printsummary="on" fork="true" showoutput="true">
                <classpath>
                    <fileset dir="lib" includes="**/*.jar" />
                    <pathelement path="${output.folder}" />
                    <pathelement path="${basedir}/src/resource" />
                </classpath>
                <formatter type="xml" />
                <batchtest todir="${report.folder}">
                    <fileset dir="${output.folder}">
                        <include name="**/*Test.*" />
                    </fileset>
                </batchtest>
            </junit>
            <junitreport todir="${report.folder}">
                <fileset dir="${report.folder}">
                    <include name="TEST-*.xml" />
                </fileset>
                <report format="noframes" todir="${ report.folder}" />
            </junitreport>
        </target>

  5. 上传部署版本
    <target name="uploadbuild" description="Upload build to an FTP server"> 
    <!-- Upload everything under the destination.dir to the FTP server. -->
    <ftp server="${ftp.hostname}" remotedir="/" userid="${ftp.username}"
    password="${ ftp.userpassword}" separator="\" verbose="yes" binary="yes">
    <fileset dir="${destination.dir}">
    <include name="**/*.*" />
    </fileset>
    </ftp>
    </target>

  6. 发送电子邮件
    <target name="notifyteam" description="Notify testing team of the new build"> 
    <!-- Read build information from the build.info file. -->
    <property file="${destination.dir}/build.info" />
    <!-- Send a mail to the testing team. -->
    <mail mailhost="${smtp.hostname}" mailport="${smtp.hostport}"
    user="${mail.user}"
    password="${mail.password}"
    subject="Test build #${ build.number}"
    from="${smtp.from}" tolist="${smtp.tolist}">
    <message>The build #${build.number} is now available for testing.</message>
    </mail>
    </target>
    需要javaMail.jar

没有评论: