Ant应用

xiaoxiao2022-06-12  48

官方网站手册: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> 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-4933865.html

最新回复(0)