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

项目结构如下图:

build.properties代码如下:

# The source code for the examples can be found in this directory
src.dir=src/main/java
test.dir=src/test/java # The path of the jar
jar.dir=lib # Classes generated by the javac compiler are deposited in this directory
target.dir=target
src.class=${target.dir}/classes
test.class=${target.dir}/test-classes
# Instrumented classes are deposited into this directory
generated.dir=${target.dir}/generated-classes
instrument.class=${generated.dir}/cobertura
report.dir=${target.dir}/surefire-reports
testng.report=${report.dir}/testng
junit.report=${report.dir}/junit
junit.report.html=${report.dir}/junit-html
coverage.xml.report=${report.dir}/cobertura-xml
coverage.summaryxml.report=${report.dir}/cobertura-summary-xml
site.dir=${target.dir}/site
cobertura.dir=${site.dir}/cobertura #sonar configration
sonar.projectName=testFrameWork
sonar.projectKey=com.zghome.mvndemo
sonar.projectVersion=1.0
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.language=java

build.xml代码如下:

<project name="test" default="test" basedir="." >
<!-- Define <testng> task -->
<property file="build.properties" /> <taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="${jar.dir}/testng-6.9.9.jar"/>
</classpath>
</taskdef> <path id="jar.classpath">
<fileset dir="${jar.dir}">
<include name="*.jar" />
</fileset>
</path>
<path id="class.classpath">
<pathelement location="${src.class}"/>
</path> <taskdef classpathref="jar.classpath" resource="tasks.properties" /> <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="${cobertura.dir}/sonar-ant-task-2.3.jar"/>
</taskdef> <target name="init">
<delete dir="${target.dir}" /> <mkdir dir="${target.dir}" />
<mkdir dir="${src.class}" />
<mkdir dir="${test.class}" />
<mkdir dir="${generated.dir}" />
<mkdir dir="${instrument.class}" />
<mkdir dir="${report.dir}" />
<mkdir dir="${testng.report}" />
<mkdir dir="${junit.report}" />
<mkdir dir="${junit.report.html}" />
<mkdir dir="${coverage.xml.report}" />
<mkdir dir="${coverage.summaryxml.report}" />
<mkdir dir="${site.dir}" />
<mkdir dir="${cobertura.dir}" />
</target> <target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${src.class}" debug="on" failonerror="false" includeAntRuntime="false" >
<classpath refid="jar.classpath"/>
</javac>
<javac srcdir="${test.dir}" destdir="${test.class}" debug="on" includeAntRuntime="false">
<classpath refid="jar.classpath"/>
<classpath refid="class.classpath"/>
</javac>
</target> <target name="compile.test" >
<delete dir="${test.class}" />
<mkdir dir="${test.class}" />
<javac srcdir="${test.dir}" destdir="${test.class}" debug="on" includeAntRuntime="false">
<classpath refid="jar.classpath"/>
<classpath refid="class.classpath"/>
</javac>
</target> <target name="instrument" depends="compile">
<!--
Remove the coverage data file and any old instrumentation.
-->
<delete file="cobertura.ser"/>
<delete dir="${instrument.class}" /> <!--
Instrument the application classes, writing the
instrumented classes into ${build.instrumented.dir}.
-->
<cobertura-instrument todir="${instrument.class}" ignoreTrivial="true" >
<!--
The following line causes instrument to ignore any
source line containing a reference to slf4j/logback, for the
purposes of coverage reporting.
-->
<ignore regex="org.slf4j.*" />
<classpath refid="jar.classpath"/>
<fileset dir="${src.class}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
-->
<include name="**/*.class" />
<exclude name="**/*Test.class" />
<exclude name="**/*testCase.class" />
</fileset>
<auxClasspath refid="class.classpath" />
</cobertura-instrument>
</target> <target name="test" depends="compile">
<testng outputdir="${testng.report}">
<xmlfileset dir="${basedir}" includes="testng*.xml"/>
<classpath location="${src.class}" />
<classpath location="${test.class}" />
<classpath refid="jar.classpath"/>
</testng>
<junitreport todir="${junit.report}">
<fileset dir="${testng.report}/junitreports">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${junit.report.html}" />
</junitreport>
</target> <target name="testfail" depends="compile.test">
<testng outputdir="${testng.report}">
<xmlfileset dir="${testng.report}" includes="testng-failed.xml"/>
<classpath location="${src.class}" />
<classpath location="${test.class}" />
<classpath refid="jar.classpath"/>
</testng>
</target> <target name="test-covery">
<testng outputdir="${testng.report}">
<xmlfileset dir="${basedir}" includes="testng*.xml"/>
<classpath location="${instrument.class}" />
<classpath location="${src.class}" />
<classpath location="${test.class}" />
<classpath refid="jar.classpath"/>
</testng>
<junitreport todir="${junit.report}">
<fileset dir="${testng.report}/junitreports">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${junit.report.html}" />
</junitreport>
</target> <target name="coverage-check">
<cobertura-check branchrate="34" totallinerate="100" />
</target> <target name="coverage-report">
<!--
Generate an XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.report}" format="xml" />
</target> <target name="summary-coverage-report">
<!--
Generate an summary XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.report}" format="summaryXml" />
</target> <target name="alternate-coverage-report">
<!--
Generate a series of HTML files containing the coverage
data in a user-readable form using nested source filesets.
-->
<cobertura-report destdir="${cobertura.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
</cobertura-report>
</target> <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."/>
<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."/> <target name="sonar" >
<!-- list of mandatories Sonar properties -->
<mkdir dir="sonar-src"/>
<copy todir="sonar-src">
<fileset dir="${src.dir}" includes="**/*.java"/>
</copy>
<property name="sonar.sources" value="sonar-src"/> <!-- list of optional Sonar properties -->
<property name="sonar.projectName" value="${sonar.projectName}"/>
<property name="sonar.projectKey" value="${sonar.projectKey}" />
<property name="sonar.projectVersion" value="${sonar.projectVersion}" />
<property name="sonar.tests" value="${test.dir}"/>
<property name="sonar.cobertura.reportPath" value="${coverage.xml.report}/coverage.xml"/>
<property name="sonar.junit.reportsPath" value="${junit.report}" />
<property name="sonar.host.url" value="${sonar.host.url}"/>
<property name="sonar.sourceEncoding" value="${sonar.sourceEncoding}" />
<property name="sonar.language" value="${sonar.language}" />
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant"/>
<delete dir="sonar-src"/>
</target> </project>

简单说明以上的内容:

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

compile:编译源码及测试用例

compile.test:仅编译测试用例

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

test:运行测试用例

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

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

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

sonar:上传sonar服务器

sonar服务器的配置请参照:http://docs.sonarqube.org/display/SONAR/Documentation/

测试覆盖率截图:

sonar服务器截图:

——

用ant组建测试框架的更多相关文章

  1. TestNG测试框架在基于Selenium进行的web自动化测试中的应用

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ TestNG+Selenium+Ant TestNG这个测试框架可以很好的和基于Selenium的 ...

  2. hadoop 测试框架

    hadoop 0.21以前的版本中(这里拿0.20为例,其他版本可能有少许不同),所有的测试相关代码都是放置在${HADOOP_HOME}/src/test下,在该目录下,是按照不同的目录来区分针对不 ...

  3. Java高级特性 第11节 JUnit 3.x和JUnit 4.x测试框架

    一.软件测试 1.软件测试的概念及分类 软件测试是使用人工或者自动手段来运行或测试某个系统的过程,其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别.它是帮助识别开发完成(中间或最终 ...

  4. python测试框架&&数据生成&&工具最全资源汇总

    xUnit frameworks 单元测试框架frameworks 框架unittest - python自带的单元测试库,开箱即用unittest2 - 加强版的单元测试框架,适用于Python 2 ...

  5. Spring MVC测试框架

    原文链接:http://jinnianshilongnian.iteye.com/blog/2004660 Spring MVC测试框架详解——服务端测试 博客分类: springmvc杂谈 spri ...

  6. JUnit - 测试框架

    什么是 Junit 测试框架? JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量.JUnit 测试框架能够轻松完成以下任意两种结合: Ecli ...

  7. Spring MVC测试框架详解——服务端测试

    随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...

  8. Java Junit测试框架

    Java    Junit测试框架 1.相关概念 Ø JUnit:是一个开发源代码的Java测试框架,用于编写和运行可重复的测试.它是用于单元测试框架体系xUnit的一个实例(用于java语言).主要 ...

  9. SpringMvc测试框架详解----服务端测试

    随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...

随机推荐

  1. 复利计算器(4)——jQuery界面美化、自动补全

    一.分工 这次终于可以跟小伙伴合作了,经过讨论,我负责界面的美化和输入框自动补全,小伙伴擅长安卓,于是将复利计算器弄成app的任务就交给了小伙伴.为了我们两人团队,我们都好奋斗哈哈哈!! 二.界面美化 ...

  2. iOS - Localizable 国际化

    1.国际化 开发的移动应用更希望获取更多用户,走向世界,这就需要应用国际化,国际化其实就是多语言,系统会根据当前设备的语言环境来识别 App 中使用中文还是英文. 2.应用内容国际化 1.新建一个名为 ...

  3. 启动受管服务器出现:unable to get file lock, will retry...

    启动受管服务器出现:unable to get file lock, will retry... 解决方法:一.删掉Domain下的*.lok文件1. 删除edit.lok进入到domain_home ...

  4. Android6.0之来电转接号码显示修改

    Android6.0之来电转接号码显示修改 手机来电转接界面是在,点开Dialer-搜索框右边的三个点-设置-通话账户(要先插卡)-中国移动或者中国联通--来电转接--第一行,显示始终转接 将所有来电 ...

  5. python3 实现简单的信用卡还款,取款转账功能V2

    仅实现还款,取款,转账,信息查询功能 程序结构: atm(函数主执行程序): #Author by Andy #_*_ coding:utf-8 _*_ import os,sys Father_pa ...

  6. sphinx全文检索 安装配置和使用

    公司项目刚刚导入大量产品数据,然后发现网站的产品搜索很卡,原本是原生sql的like来做模糊搜索,数据量20W的时候还可以接受,但是上百万就很卡了,所以需要做优化. 经过考虑,打算采用全文检索 sph ...

  7. 第一章 Java的I/O演进之路

    I/O基础入门 Java的I/O演进 第一章 Java的I/O演进之路 1.1 I/O基础入门 1.1.1 Linux网络I/O模型简介 根据UNIX网络编程对I/O模型的分类,UNIX提供了5中I/ ...

  8. find / -type f -name "*fetion*" |xargs rm -rf {}\

    find / -type f -name "*fetion*" |xargs rm -rf {}\

  9. 我需要在Web上完成一个图片上传的功能(+2)

    增加一个页面,用于判断传参是否正确. @{     //判断是否具备会员参数     if (UrlData.Count > 0)     {         Session["Arg ...

  10. redis + spring 集成

    1.pom <modelVersion>4.0.0</modelVersion> <groupId>com.x.redis</groupId> < ...