ANT学习之marcodef&junit

2017-09-30 14:27:36来源:网络收集作者:管理员人点击

分享



上一篇转载的ant学习指南主要讲了ant的命令,讲到的地方比较细。


但是实际运用中我们除了做打包(类似c中的makefile)外,也在junit测试中使用到ant,并且用marcodef宏定义的方法会让脚本更加简洁,效率更高。


下面就讲解一下marcodef和junit命令:


1.Marcodef


MacroDef——“宏”,宏是一种规则或模式,或称语法替换 ,当我们target任务不多的时候,我们可以一个个写作。但是实际工作中,常常会有大量的任务需要执行。例如100个目录需要编译,那么我的脚本中就要重复100次相同的代码;所以自然的就想到要和写其他脚本或者程序中那样构造复用,使用MacroDef的情况下,只用写一个通用的,再在其他地方调用它就可以了,既减少了代码量,又提高了工作效率。
    在官网使用手册(http://ant.apache.org/manual/)我们可以找到MacroDef的具体书写格式和参数。





//macrodef的定义,定义了name属性
//参数定义
//参数定义
//实际宏执行的部分















有了宏定义之后,我们下面的任务只需要添加一条语句即可进行junit测试啦!






//调用宏...



注意的是:
1、可以调用其他的,但不可以调用可用调用,也可用调用其他



2、所有任务的特性通过@{}展开而不是${},它们可以随宏的每一次调用而改变,且只在宏定义程序块内部可用。



3、在build文件里,是和平级的,上例中定义的<<、testModule >若不将其放在里面,执行Ant时,脚本会直接执行这部分的宏代码,而不会去执行中定义的‘depands="compile"的依赖关系

2.Junit&Junitreport


我们先来看Junit,从官网(http://ant.apache.org/manual/index.html)上我们可以看到api文档


Attribute
Description
Requiredprintsummary
Print one-line statistics for each testcase. Can take the values on, off, and withOutAndErr. withOutAndErr is the same as on but also
includes the output of the test as written to System.out and System.err.
No; default is off.fork
Run the tests in a separate VM.
No; default is off.forkmode
Controls how many Java Virtual Machines get created if you want to fork some tests. Possible values are "perTest" (the default), "perBatch" and "once". "once" creates only a single Java VM for all tests
while "perTest" creates a new VM for each TestCase class. "perBatch" creates a VM for each nested and one collecting all nested s. Note that only tests with the same settings of filtertrace, haltonerror, haltonfailure, errorproperty and failureproperty can
share a VM, so even if you setforkmode to "once", Ant may have to create more than a single Java VM. This attribute is ignored for tests that don't get forked into a new Java VM. since Ant 1.6.2
No; default is perTest.haltonerror
Stop the build process if an error occurs during the test run.
No; default is off.errorproperty
The name of a property to set in the event of an error.
Nohaltonfailure
Stop the build process if a test fails (errors are considered failures as well).
No; default is off.failureproperty
The name of a property to set in the event of a failure (errors are considered failures as well).
No.filtertrace
Filter out Junit and Ant stack frames from error and failure stack traces.
No; default is on.timeout
Cancel the individual tests if they don't finish in the given time (measured in milliseconds). Ignored if fork is disabled. When running multiple tests inside the same Java VM (see forkMode),
timeout applies to the time that all tests use together, not to an individual test.
Nomaxmemory
Maximum amount of memory to allocate to the forked VM. Ignored if fork is disabled. Note: If you get java.lang.OutOfMemoryError: Java heap space in some of
your tests then you need to raise the size likemaxmemory="128m"
Nojvm
The command used to invoke the Java Virtual Machine, default is 'java'. The command is resolved by java.lang.Runtime.exec(). Ignored if fork is disabled.
No; default is java.dir
The directory in which to invoke the VM. Ignored if fork is disabled.
Nonewenvironment
Do not propagate the old environment when new environment variables are specified. Ignored if fork is disabled.
No; default is false.includeantruntime
Implicitly add the Ant classes required to run the tests and JUnit to the classpath in forked mode.
No; default is true.showoutput
Send any output generated by tests to Ant's logging system as well as to the formatters. By default only the formatters receive the output.
No

(1)Junit



这里就是,1.开启case结果的输出,执行结果输出为System.out ,err输出为System.err

  2.运行tests在一个单独的vm虚拟机上


  3.tests结果同时输出都ant log系统和formatters记录中,默认值输出到formatters的记录中


  4.当出现fail的时候停止build进程,(出现error也认为是failures)


  5.给fork出的vm分配的最大内存为512M(同时,如果出现java.lang.OutOfMemoryError: Java heap space可能就是这里的内存不够了,但是512m目前看in应该是足够  的)

tclasspath


junit指定junit测试使用的编译后的字节码文件的classpath


The results of the tests can be printed in different formats. Output will always be sent to a file, unless you set the usefile attribute to false. The name of the file isdetermined
by the name of the test and can be set by the outfile attribute of .
这里查看api得知就是输出tests结果,分别为 xml和plain格式。


微信扫一扫

第七城市微信公众平台