51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1487|回复: 0
打印 上一主题 下一主题

[讨论] 用ant组建测试框架

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-4-8 10:50:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

有时候由于公司网络或其它原因,无法采用maven,这时ant是一个比较理想的选择。以下是以ant为例,搭
建一个测试框架

项目结构如下图:



build.properties代码如下:

复制代码
  1. # The source code for the examples can be found in this directory
  2. src.dir=src/main/java
  3. test.dir=src/test/java

  4. # The path of the jar
  5. jar.dir=lib


  6. # Classes generated by the javac compiler are deposited in this directory
  7. target.dir=target
  8. src.class=${target.dir}/classes
  9. test.class=${target.dir}/test-classes
  10. # Instrumented classes are deposited into this directory
  11. generated.dir=${target.dir}/generated-classes
  12. instrument.class=${generated.dir}/cobertura
  13. report.dir=${target.dir}/surefire-reports
  14. testng.report=${report.dir}/testng
  15. junit.report=${report.dir}/junit
  16. junit.report.html=${report.dir}/junit-html
  17. coverage.xml.report=${report.dir}/cobertura-xml
  18. coverage.summaryxml.report=${report.dir}/cobertura-summary-xml
  19. site.dir=${target.dir}/site
  20. cobertura.dir=${site.dir}/cobertura

  21. #sonar configration
  22. sonar.projectName=testFrameWork
  23. sonar.projectKey=com.zghome.mvndemo
  24. sonar.projectVersion=1.0
  25. sonar.host.url=http://localhost:9000
  26. sonar.sourceEncoding=UTF-8
  27. sonar.language=java
  28. 复制代码
  29. build.xml代码如下:

  30. 复制代码
  31. <project name="test" default="test" basedir="." >
  32.     <!-- Define <testng> task -->
  33.     <property file="build.properties" />

  34.     <taskdef name="testng" classname="org.testng.TestNGAntTask">
  35.         <classpath>
  36.             <pathelement location="${jar.dir}/testng-6.9.9.jar"/>
  37.         </classpath>
  38.     </taskdef>

  39.     <path id="jar.classpath">
  40.         <fileset dir="${jar.dir}">
  41.             <include name="*.jar" />
  42.         </fileset>
  43.     </path>
  44.     <path id="class.classpath">
  45.         <pathelement location="${src.class}"/>
  46.     </path>

  47.     <taskdef classpathref="jar.classpath" resource="tasks.properties" />

  48.     <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
  49.         <classpath path="${cobertura.dir}/sonar-ant-task-2.3.jar"/>
  50.     </taskdef>

  51.     <target name="init">
  52.         <delete dir="${target.dir}" />

  53.         <mkdir dir="${target.dir}" />
  54.         <mkdir dir="${src.class}" />
  55.         <mkdir dir="${test.class}" />
  56.         <mkdir dir="${generated.dir}" />
  57.         <mkdir dir="${instrument.class}" />
  58.         <mkdir dir="${report.dir}" />
  59.         <mkdir dir="${testng.report}" />
  60.         <mkdir dir="${junit.report}" />
  61.         <mkdir dir="${junit.report.html}" />
  62.         <mkdir dir="${coverage.xml.report}" />
  63.         <mkdir dir="${coverage.summaryxml.report}" />
  64.         <mkdir dir="${site.dir}" />
  65.         <mkdir dir="${cobertura.dir}" />
  66.     </target>

  67.     <target name="compile" depends="init">
  68.         <javac srcdir="${src.dir}" destdir="${src.class}"  debug="on" failonerror="false"  includeAntRuntime="false" >
  69.             <classpath refid="jar.classpath"/>
  70.         </javac>
  71.         <javac srcdir="${test.dir}" destdir="${test.class}"  debug="on" includeAntRuntime="false">
  72.             <classpath refid="jar.classpath"/>
  73.             <classpath refid="class.classpath"/>
  74.         </javac>
  75.     </target>

  76.     <target name="compile.test" >
  77.         <delete dir="${test.class}" />
  78.         <mkdir dir="${test.class}" />
  79.         <javac srcdir="${test.dir}" destdir="${test.class}"  debug="on" includeAntRuntime="false">
  80.             <classpath refid="jar.classpath"/>
  81.             <classpath refid="class.classpath"/>
  82.         </javac>
  83.     </target>

  84.     <target name="instrument" depends="compile">
  85.         <!--
  86.             Remove the coverage data file and any old instrumentation.
  87.         -->
  88.         <delete file="cobertura.ser"/>
  89.         <delete dir="${instrument.class}" />

  90.         <!--
  91.             Instrument the application classes, writing the
  92.             instrumented classes into ${build.instrumented.dir}.
  93.         -->
  94.         <cobertura-instrument todir="${instrument.class}" ignoreTrivial="true" >
  95.             <!--
  96.                 The following line causes instrument to ignore any
  97.                 source line containing a reference to slf4j/logback, for the
  98.                 purposes of coverage reporting.
  99.             -->
  100.             <ignore regex="org.slf4j.*" />
  101.             <classpath refid="jar.classpath"/>
  102.             <fileset dir="${src.class}">
  103.                 <!--
  104.                     Instrument all the application classes, but
  105.                     don't instrument the test classes.
  106.                 -->
  107.                 <include name="**/*.class" />
  108.                 <exclude name="**/*Test.class" />
  109.                 <exclude name="**/*testCase.class" />
  110.             </fileset>
  111.             <auxClasspath refid="class.classpath" />
  112.         </cobertura-instrument>
  113.     </target>

  114.     <target name="test" depends="compile">
  115.         <testng outputdir="${testng.report}">
  116.             <xmlfileset dir="${basedir}" includes="testng*.xml"/>
  117.             <classpath location="${src.class}" />
  118.             <classpath location="${test.class}" />
  119.             <classpath refid="jar.classpath"/>
  120.         </testng>
  121.         <junitreport todir="${junit.report}">
  122.             <fileset dir="${testng.report}/junitreports">
  123.                 <include name="TEST-*.xml" />
  124.             </fileset>
  125.             <report format="frames" todir="${junit.report.html}" />
  126.         </junitreport>
  127.     </target>

  128.     <target name="testfail"  depends="compile.test">
  129.         <testng outputdir="${testng.report}">
  130.             <xmlfileset dir="${testng.report}" includes="testng-failed.xml"/>
  131.             <classpath location="${src.class}" />
  132.             <classpath location="${test.class}" />
  133.             <classpath refid="jar.classpath"/>
  134.         </testng>
  135.     </target>

  136.     <target name="test-covery">
  137.         <testng outputdir="${testng.report}">
  138.             <xmlfileset dir="${basedir}" includes="testng*.xml"/>
  139.             <classpath location="${instrument.class}" />
  140.             <classpath location="${src.class}" />
  141.             <classpath location="${test.class}" />
  142.             <classpath refid="jar.classpath"/>
  143.         </testng>
  144.         <junitreport todir="${junit.report}">
  145.             <fileset dir="${testng.report}/junitreports">
  146.                 <include name="TEST-*.xml" />
  147.             </fileset>
  148.             <report format="frames" todir="${junit.report.html}" />
  149.         </junitreport>
  150.     </target>

  151.     <target name="coverage-check">
  152.         <cobertura-check branchrate="34" totallinerate="100" />
  153.     </target>

  154.     <target name="coverage-report">
  155.         <!--
  156.                 Generate an XML file containing the coverage data using
  157.                 the "srcdir" attribute.
  158.             -->
  159.         <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.report}" format="xml" />
  160.     </target>

  161.     <target name="summary-coverage-report">
  162.         <!--
  163.                 Generate an summary XML file containing the coverage data using
  164.                 the "srcdir" attribute.
  165.             -->
  166.         <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.report}" format="summaryXml" />
  167.     </target>

  168.     <target name="alternate-coverage-report">
  169.         <!--
  170.                 Generate a series of HTML files containing the coverage
  171.                 data in a user-readable form using nested source filesets.
  172.             -->
  173.         <cobertura-report destdir="${cobertura.dir}">
  174.             <fileset dir="${src.dir}">
  175.                 <include name="**/*.java"/>
  176.             </fileset>
  177.         </cobertura-report>
  178.     </target>

  179.     <target name="coverage" depends="instrument,test-covery,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>
  180.     <target name="coverage-test" depends="compile.test,test-covery,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>

  181.     <target name="sonar" >
  182.         <!-- list of mandatories Sonar properties -->
  183.         <mkdir dir="sonar-src"/>
  184.         <copy todir="sonar-src">
  185.             <fileset dir="${src.dir}" includes="**/*.java"/>
  186.         </copy>
  187.         <property name="sonar.sources" value="sonar-src"/>

  188.         <!-- list of optional Sonar properties -->
  189.         <property name="sonar.projectName" value="${sonar.projectName}"/>
  190.         <property name="sonar.projectKey" value="${sonar.projectKey}" />
  191.         <property name="sonar.projectVersion" value="${sonar.projectVersion}" />
  192.         <property name="sonar.tests" value="${test.dir}"/>
  193.         <property name="sonar.cobertura.reportPath" value="${coverage.xml.report}/coverage.xml"/>
  194.         <property name="sonar.junit.reportsPath" value="${junit.report}" />
  195.         <property name="sonar.host.url" value="${sonar.host.url}"/>
  196.         <property name="sonar.sourceEncoding" value="${sonar.sourceEncoding}" />
  197.         <property name="sonar.language" value="${sonar.language}" />
  198.         <sonar:sonar  xmlns:sonar="antlib:org.sonar.ant"/>
  199.         <delete dir="sonar-src"/>
  200.     </target>

  201. </project>
复制代码

复制代码
简单说明以上的内容:

init:初始化工程的目录结构

compile:编译源码及测试用例

compile.test:仅编译测试用例

instrument:修改源码的class,计算测试用例覆盖率

test:运行测试用例

test-covery:计算测试用例覆盖率

coverage-report,summary-coverage-report,alternate-coverage-report:生成不同格式的测试报告

coverage:调用上述任务,生成测试报告

sonar:上传sonar服务器

sonar服务器的配置请参照:

测试覆盖率截图:



sonar服务器截图:


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-26 12:10 , Processed in 0.063400 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表