Raible's Wiki
Raible Designs AppFuseHomepage- Korean - Chinese - Italian - Japanese QuickStart Guide User Guide Tutorials Other ApplicationsStruts ResumeSecurity Example Struts Menu
Set your name in
UserPreferences
Referenced by
JSPWiki v2.2.33
Hide Menu |
Coverage testing with Appfuse and CoberturaThis articel will give a short overview how to integrate Cobertura in AppFuse. I assume that you have a running AppFuse project:
Download the latest version of Cobertura [#1]
Integrate the libraries in AppFuse [#2]
So for example the dao.compile.classpath would look like <path id="dao.test.classpath"> <path refid="dao.compile.classpath"/> <pathelement location="${dbunit.jar}"/> <pathelement location="${junit.jar}"/> <pathelement location="${commons-collections.jar}"/> <pathelement location="${log4j.jar}"/> <pathelement location="${cobertura.jar}"/> </path> Integrate new Cobertura ANT-Tasks in the build.xml [#3]In order to use cobertura with your junit tests, you have to add the cobertura ANT-tasks to your build.xml
<path id="cobertura_classpath"> <fileset dir="${cobertura.dir}"> <include name="cobertura.jar" /> </fileset> <fileset dir="${cobertura.dir}/lib"> <include name="**/*.jar" /> </fileset> </path> <taskdef classpathref="cobertura_classpath" resource="tasks.properties"/> Define new macros for Junit testing with Cobertura [#4]Cobertura needs to instrument the code in order to calculate the coverage during the tests. To do this, cobertura provides a macro definition called cobertura-instrument. Copy the following macro in your build.xml:<macrodef name="instrument"> <attribute name="module"/> <attribute name="additional.src.dirs" default=""/> <element name="options" optional="yes"/> <sequential> <echo level="info">Instrumenting classes...</echo> <!-- Instrument the application classes, writing the instrumented classes into ${build.instrumented.dir}. --> <cobertura-instrument todir="${instrumented.dir}"> <!-- The following line causes instrument to ignore any source line containing a reference to log4j, for the purposes of coverage reporting. --> <ignore regex="org.apache.log4j.*" /> <fileset dir="${build.dir}"> <!-- Instrument all the application classes, but don't instrument the test classes. --> <include name="**/*.class" /> <exclude name="**/*Test.class" /> </fileset> </cobertura-instrument> </sequential> </macrodef> Now you have to specify, where cobertura should store the instrumented classes (You have to define the instrumented.dir variable). Add the following line to your build.properties: instrumented.dir=${build.dir}/instrumtented Change test targets for Junit testing with Cobertura [#5]Finally, we have to make sure that our code is instrumented before running the Junit tests and that the Junit test cases use the instrumented code. In order to do this, we need to change the macro test-module.Add the line to the macrodef. Finally it should look something linke this: <macrodef name="test-module"> <attribute name="module"/> <attribute name="additional.src.dirs" default=""/> <element name="options" optional="yes"/> <sequential> <echo level="info">Testing @{module}...</echo> <mkdir dir="${test.dir}/data"/> <propertycopy name="testcase" from="@{module}-testcase" silent="true"/> <!-- Replace tokens in test properties files --> <copy todir="${test.dir}/@{module}/classes"> <fileset dir="test/@{module}" excludes="**/*.java"/> <filterset refid="variables.to.replace"/> </copy> <instrument module="@{module}"/> <!-- Thats the new line --> ...... Additional we need a sysproperty in order to tell cobertura where to store the coverage results and a new classpath reference to the instrumented code: .... <junit printsummary="no" forkmode="once" errorProperty="test.failed" failureProperty="test.failed" fork="${junit.fork}"> <sysproperty key="net.sourceforge.cobertura.datafile" file="${basedir}/cobertura.ser" /> <!-- Thats a new line --> <classpath location="${instrumented.dir}" /> <!-- Thats a new line --> ... Define new targets for generating the coverage reports [#6]Add the following target to your build.xml.<target name="test-reports-coverage" depends="init"> <!-- Generate a series of HTML files containing the coverage data in a user-readable form using nested source filesets. --> <mkdir dir="${test.dir}/coverage"/> <cobertura-report destdir="${test.dir}/coverage"> <fileset dir="${src.dir}/dao"> <include name="**/*.java"/> </fileset> <fileset dir="${src.dir}/service"> <include name="**/*.java"/> </fileset> <fileset dir="${src.dir}/web"> <include name="**/*.java"/> </fileset> </cobertura-report> </target> and define in your build.properties, where cobertura can find the sources by adding the line src.dir=${basedir}/src Test it! [#7]Now you can test your tests by running ant test-dao and ant test-reports-coverage.Like Matt would say: Yeah Baby, Yeah: BUILD SUCCESSFUL
|