Ant应用

官方网站手册:http://ant.apache.org/manual/index.html

一、ant中使用junit
两种方法来执行测试:

一是使用源路径

<batchtest fork="yes" todir="${reports.tests}"> <fileset dir="${src.tests}"> <include name="**/*Test*.java"/> <exclude name="**/AllTests.java"/> </fileset> </batchtest></junit>
二是使用编译路径

<batchtest> <fileset dir="${build.dir}"> <include name="**/*Test.*" /> <exclude name="**/Jdbc*Test.*" /> </fileset> </batchtest>
Ant自学教程的部分代码:

<property name="report.dir" value="${build.dir}/junitreport"/> ... <target name="junit" depends="jar"> <mkdir dir="${report.dir}"/> <junit printsummary="yes"> <classpath> <path refid="classpath"/> <path refid="application"/> </classpath> <formatter type="xml"/> <batchtest fork="yes" todir="${report.dir}"> <fileset dir="${src.dir}" includes="*Test.java"/> </batchtest> </junit> </target> <target name="junitreport"> <junitreport todir="${report.dir}"> <fileset dir="${report.dir}" includes="TEST-*.xml"/> <report todir="${report.dir}"/> </junitreport> </target>
示例一

<target name="tests" depends="build, buildtests" description="Run tests"> <junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed" showoutput="true"> <!--classpath refid="master-classpath" / --> <classpath refid="test-classpath" /> <formatter type="brief" usefile="false" /> <batchtest> <fileset dir="${build.dir}"> <include name="**/*Tests.*" /> <exclude name="**/Jdbc*Tests.*" /> </fileset> </batchtest> </junit> <fail if="tests.failed"> tests.failed=${tests.failed} **** One or more tests failed! Check the output ... **** </fail>
</target>


示例二

<target name="test-unit" depends="build-unit" description="--> Run all unit tests">

<echo message="Test output can be found in directory ${dist.unitreport.dir}"/>

<delete dir="${dist.unitreport.dir}"/>

<mkdir dir="${dist.unitreport.dir}"/>


<echo message="executing tests [${test.match}.java]"/>


<junit printsummary="${junit.printsunmmary}" showoutput="${junit.showoutput}"

fork="${junit.fork}" forkmode="${junit.forkmode}" failureproperty="${junit.failureproperty}">

<formatter type="plain" usefile="false"/> <!-- just to console -->

<formatter type="xml" usefile="true"/>


<classpath refid="compile.classpath.unit"/>


<batchtest todir="${dist.unitreport.dir}">

<fileset dir="${build.unit.dir}">

<include name="${test.match}.class"/>

</fileset>

</batchtest>

</junit>

<fail if="test.failure">

One or more unit tests failed.

</fail>

</target>


<target name="report-test-unit" depends="test-unit" description="--> generate unit test report">

<junitreport todir="${dist.unitreport.dir}">

<fileset dir="${dist.unitreport.dir}">

<include name="TEST-*.xml"/>

</fileset>

<report format="frames" todir="${dist.unitreport.dir}"/>

</junitreport>

<delete dir="${dist.unitreport.dir}" includes="TEST-*.xml"/>

</target>



二、build.xml文件的编写

1、加载配置文件

<property file="build.properties"/>2、定义全局变量

3、定义公用classpath信息,供编译时调用4、帮助信息5、建立ant具体的target
build、deploy、deploywar、clean、undeploy、

6、建立junit测试target(有时包含测试报告)

buildtests、tests、dbTests

7、建立数据库方面的target

createTables、dropTables、loadData、printData、clearData、shutdownDb

8、Tomcat tasks

先配置其classpath;再配置taskdef;最后配置target

install、reload、start、stop、list


三、hsqldb在测试中的应用

1、hsqldb.jar:放置路径springapp/war/WEB-INF/lib/

2、server.bat:springapp/db/

java -classpath ..\war\WEB-INF\lib\hsqldb.jar org.hsqldb.Server -database test
3、编写sql脚本。如

springapp/db/create_products.sql

springapp/db/load_data.sql

4、编写ant脚本

包含属性文件的连接配置:

db.driver=org.hsqldb.jdbcDriverdb.url=jdbc:hsqldb:hsql://localhostdb.user=sadb.pw=
5、启动数据库

open a command window, change to the 'springapp/db' directory and start the database by running one of these startup scripts

6、执行ant脚本


如:<target name="dbTests" depends="build, buildtests,dropTables,createTables,loadData" description="Run db tests">


四、基于Spring框架的数据库操作测试

1、用到jar包:spring-test.jar;

2、测试类继承AbstractTransactionalDataSourceSpringContextTests;

如:

public class JdbcProductDaoTests extends AbstractTransactionalDataSourceSpringContextTests


3、复写父类方法,加载配置及其初始化资源

private ProductDao productDao; public void setProductDao(ProductDao productDao) { this.productDao = productDao; } @Override protected String[] getConfigLocations() { return new String[] { "classpath:test-context.xml" }; } @Override protected void onSetUpInTransaction() throws Exception { super.deleteFromTables(new String[] { "products" }); super.executeSqlScript("file:db/load_data.sql", true);
}

3、建立测试方法


4、建立配置文件和数据库执行脚本文件


5、在build文件中添加相应建立ant脚本

<path id="test-classpath"> <fileset dir="${web.dir}/WEB-INF/lib"> <include name="*.jar" /> </fileset> <pathelement path="${build.dir}" /> <pathelement path="${test.dir}" /> <pathelement path="${web.dir}/WEB-INF/classes" />
</path>

<target name="dbTests" depends="build, buildtests,dropTables,createTables,loadData" description="Run db tests"> <junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed" showoutput="true"> <classpath refid="test-classpath" /> <formatter type="brief" usefile="false" /> <batchtest> <fileset dir="${build.dir}"> <include name="**/Jdbc*Tests.*" /> </fileset> </batchtest> </junit> <fail if="tests.failed"> One or more tests failed! Check the output ... </fail> </target>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值