`
freshunter
  • 浏览: 15644 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

如何使用ant构建junit测试

阅读更多

 

build.bat:

echo on

set PROJET_HOME=..
set JAVA_HOME=%PROJET_HOME%/../jdk/windows
set ANT_HOME=%PROJET_HOME%/../ant

call %ANT_HOME%/bin/ant -f build.xml build_all -Dkkk=123456

pause
 

 

 

 

 

build.xml:

<project name="MyProject" default="dist" basedir="..">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="testsrc" location="test"/>
  <property name="tmpclass" location="tmp/class"/>
  <property name="tmptest" location="tmp/testclass"/>
  <property name="testrep" location="testrep"/>
  <property name="dist"  location="dist"/>

  <path id="lib">
      <pathelement path="${classpath}"/>
      <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
    </path>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <echo message= "${kkk}"/>
    <delete dir="${tmpclass}"/>
    <mkdir dir="${tmpclass}"/>
    <delete dir="${tmptest}"/>
    <mkdir dir="${tmptest}"/>
    <delete dir="${testrep}"/>
    <mkdir dir="${testrep}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${tmpclass}" includeAntRuntime="false">
    	<classpath>
    		      <path refid="lib"/>
    		</classpath>
    </javac>
  </target>

  <target name="testcompile" depends="compile"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${testsrc}" destdir="${tmptest}" includeAntRuntime="false">
    	<classpath>
    		      <path refid="lib"/>
    		      <pathelement path="${tmpclass}"/>
    		</classpath>
    	</javac>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${tmpclass}"/>
  </target>

  <target name="test" depends="testcompile">
  	
				  	<junit printsummary="true">
				  <classpath>
    		      <path refid="lib"/>
				    <pathelement path="${tmptest}"/>
				    <pathelement path="${tmpclass}"/>
				  </classpath>
				
				  <formatter type="plain"/>
							
				  <batchtest fork="yes" todir="${testrep}">
				    <fileset dir="${testsrc}">
				      <include name="**/*Test.java"/>
				    </fileset>
				  </batchtest>
				</junit>

  	</target>

  <target name="build_all" depends="compile,testcompile,test,dist"/>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
 

 

目录结构:

build 存放构建脚本

lib 存放依赖包

test 存放单元测试源码

src 

 

刚开始只在lib下存放junit.jar会抛找不到类的异常,可能跟版本也有关系:

org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

再下载这个包:

hamcrest-core-1.2.jar

 

参考链接:

http://stackoverflow.com/questions/1171264/ant-junit-noclassdeffounderror

http://ant.apache.org/manual/index.html

 

分享到:
评论

相关推荐

    eclipse下利用ant、junit进行自动化测试例子源码

    eclipse下利用ant、junit进行自动化测试例子源码

    JUnit单元测试自动构建脚本

    基于Ant自动构建JUnit单元测试批量执行测试用例并生成报表。

    Ant+JUnit+EMMA集成实例

    本实例主要展示了在myEclipse或Eclipse中如何集成Ant、Junit和EMMA技术,有三个文件夹,在导入myEclipse或Eclipse中时,要Link Source,把三个都选上。详细地讲解了ant的构建文件build.xml

    Ant自动化测试构建

    一步步教你实现Ant工具和Junit工具的结合

    JUnit实战(第2版)

    《JUnit实战(第2版)》从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度...

    Junit实战第二版 中文完整版 0分下载

    《JUnit实战(第2版)》从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度...

    JUnit实战 第2版 (英文版)

    《JUnit实战(第2版)》从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度...

    Junit实战第二版

    《JUnit实战(第2版)》从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度...

    Junit实战第二版 中文完整版

    JUnit实战(第2版)本书从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度...

    Junit实战(第2版)

    本书从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mock...

    追求代码质量: 通过测试分类实现敏捷构建

    先用JUnit构建测试,且把它作为Ant构建过程的一部分尽可能地运行。设置一个定时任务在夜间运行构建。不断增长的测试套件会在每次签入时运行。开发出了一个庞大的代码库和一个同样庞大的JUnit测试套件。最后得出单元...

    ant学习测试工程源码

    ant学习测试工程源码,基于spring与hiberante与junit的java工程 用于ant的项目构建

    junit-plugin:允许发布JUnit格式的测试结果

    Jenkins理解JUnit测试报告XML格式(TestNG也使用该格式)。 配置此选项后,Jenkins可以提供有关测试结果的有用信息,例如趋势。 该插件还为Jenkins中的其他单元测试发布者插件提供了通用API。 该功能是Jenkins Core...

    ant学习配套电子书教程

    5、集成junit,适合进行xp开发模式所提倡的单元测试. 6、易于使用java进行功能扩展. 7、支持j2ee部署. 8、善于java项目的部署工作. 9、已经成为大多数开源项目的标准构建工具. 10、由于ant支持测试和部署,因此可以...

    自动化测试之idea+gradle+JUnit的使用(参数化测试与非参数化测试)

    Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,目前也增加了基于Kotlin语言的kotlin-based DSL,抛弃了基于XML的各种繁琐配置 ...

    simple-crm-rest-and-spa:使用 Servlets + Hibernate 持久性 + JUnit 测试 + SPA w 的 REST 服务。 AJAX 请求 + MVC 架构 + 更多的库和技术..

    JUnit 测试添加到使用 EasyMock 库模拟的几种方法中。 提供 Ant 构建文件(最初使用 Tomcat 部署的服务器)。 客户端(Web 前端)使用 AJAX 请求和自定义 MVC 架构:使用 Q.js 进行承诺、使用 Sammy.js 路由、...

    测试驱动开发的艺术

    第一部分 TDD入门第1章 综述1.1 挑战:用正确的方法...用Fit创建验收测试第11章 执行验收测试的策略第12章 TDD应用附录A JUnit 4简明教程附录B JUnit 3.8简明教程附录C EasyMock简明教程附录D 通过Ant运行测试相关资源

    JUnit.in.Action中文版

    ·用Ant和Maven进行自动构建(★★★★★) ·在Eclipse内进行测试 ·对如下内容进行单元测试  ◆Java应用程序(★★)  ◆Filter  ◆Servlet (★★★★)   ◆EJB  ◆JSP (★★★)  ◆数据库应用程序...

    EJB设计模式(PDF)

    本书不仅是高级设计模式的最佳指志,而且解决了EJB实现的关键问题。本书包括了多个高级EJB设计模式,还提供了使用EJB设计模式的...用Ant构建系统的最佳实践、JUnit测试策略。如何使用JDO和EJB作为企业bean的替代工具。

    中山大学软件工程中级实训阶段二答案

    Part3:Jumper(GroupActivity)程序说明、程序代码(使用ANT构建,使用Junit测试) Part4:Modified ChameleonCritter、ChameleonKid、RockHound、BlusterCritter、QuickCrab、kingCrab的代码编写

Global site tag (gtag.js) - Google Analytics