/ Vaadin Framework


Using Emma with Vaadin addons

Since we can now write testable addons for Vaadin and use Emma to monitor the test coverage the next logical step would be to get test coverage reports over our Vaadin addon builds.

I am going to assume you are familiar with my two previous blog posts about Creating Extendable and Testable addons for Vaadin and Test coverage analysis with Emma. You will need that information to understand this article, so if you haven't read them please do it before continuing.

SpinButton - An example cont.

To demonstrate creating Emma test coverage reports I am going to use the SpinButton example shown in the previous blogpost. It can be downloaded as a zip archive here.

The first thing we need to do is add the Emma dependency jars to the project. The jars can be downloaded at http://emma.sourceforge.net/downloads.html. Add the jars under WebContent/WEB-INF/lib where all the other project jars also are located.

Next we will need to modify our test.xml to generate our Emma test reports. Just as we done with the normal Java project we will need to implement the four Ant targets to generate the Emma reports. However, Vaadin addons might also have some GWT client side code, so we will need to modify the script a bit to take that into consideration. Here is what the remaining test.xml will look like.

<?xml version="1.0" encoding="UTF-8"?>  
<project name="ExampleComponent tests" basedir="." default="tests-all">  

    <!-- Basic build properties -->  
    <property name="src.dir" value="src" />  
    <property name="test.dir" value="test" />  
    <property name="test.classes.dir" value="build/test/classes" />  
    <property name="gwt.test.output.dir" value="build/test/gwt" />  
    <property name="lib" value="WebContent/WEB-INF/lib" />  

    <!-- Target directory for instrumentation files -->    
    <property name="emma.instr.dir" value="build/test/instrumentation" />    

    <!-- Target directory for coverage files -->    
    <property name="emma.coverage.dir" value="build/test/coverage" />    

    <!-- Classpath needed for JUnit -->  
    <path id="test.classpath">  
        <pathelement path="${src.dir}" />  
        <pathelement path="${test.dir}" />  
        <pathelement path="${emma.instr.dir}" />  
        <pathelement path="${test.classes.dir}" />  
        <fileset dir="${lib}">  
            <include name="**/*.jar"/>  
         </fileset>  
    </path>  

    <taskdef resource="emma_ant.properties" classpathref="test.classpath" />  


    <!-- Cleans up testing directories -->  
    <target name="tests-clean">  
        <delete dir="${test.classes.dir}" />  
        <delete dir="${gwt.test.output.dir}" />  
    </target>  


    <!-- Compile all classes for unit tests -->  
    <target name="compile-classes" depends="tests-clean">  
        <mkdir dir="${test.classes.dir}" />  
        <javac srcdir="${src.dir};${test.dir}" destdir="${test.classes.dir}" >  
            <classpath refid="test.classpath" />  
        </javac>  
    </target>  

     <!-- Analyzes the compiled code and adds instrumentation details for testing -->    
    <target name="emma-instrument" depends="compile-classes">    
        <mkdir dir="${emma.instr.dir}" />    
        <mkdir dir="${emma.coverage.dir}" />    
        <emma enabled="true">    
            <instr instrpath="${test.classes.dir}" destdir="${emma.instr.dir}"    
                metadatafile="${emma.coverage.dir}/coverage.emma" merge="true" />    
        </emma>    
    </target>    


    <!-- Server side JUnit tests -->  
    <target name="serverside-tests" depends="emma-instrument">  
        <junit fork="yes" haltonfailure="yes">  
                <classpath refid="test.classpath" />  
                <formatter type="plain" usefile="false" />  
                <batchtest>  
                    <fileset dir="${test.classes.dir}">  
                        <include name="**/**Test.class" />  
                        <exclude name="**/client/**" />  
                    </fileset>  
                </batchtest>  
                <jvmarg value="-Demma.coverage.out.file=${emma.coverage.dir}/coverage.emma" />    
                <jvmarg value="-Demma.coverage.out.merge=true" />    
         </junit>  
    </target>  


    <!-- GWT Unit tests for the client side -->  
    <target name="clientside-tests" depends="emma-instrument">  
        <mkdir dir="${gwt.test.output.dir}" />  
        <junit fork="yes" haltonfailure="yes" printSummary="yes">  
            <jvmarg value="-Dgwt.args=-out ${gwt.test.output.dir}" />  
            <formatter type="plain" usefile="false" />  
            <classpath refid="test.classpath" />  
             <batchtest>  
                    <fileset dir="${test.classes.dir}">  
                        <include name="**/client/**/**Test.class" />  
                    </fileset>  
             </batchtest>  
             <jvmarg value="-Demma.coverage.out.file=${emma.coverage.dir}/coverage.emma" />    
             <jvmarg value="-Demma.coverage.out.merge=true" />    
        </junit>  
    </target>  

    <!-- Create XML reports from the coverage information -->    
    <target name="emma-report" depends="serverside-tests, clientside-tests">    
        <emma enabled="true">    
            <report sourcepath="${src.dir};${test.dir}">    
                <fileset dir="${emma.coverage.dir}">    
                    <include name="*.emma" />    
                </fileset>    
                <xml outfile="${emma.coverage.dir}/coverage.xml" />    
            </report>    
        </emma>    
    </target>    

    <!-- Execute both server and client side tests -->  
    <target name="tests-all" depends="emma-report" />  

</project>  

What I've basically have done is combined the already existing SpinButton test.xml with the test.xml I showed you in the previous blog post about Emma. Simple as that.

Now that we have our test script we can just call it in Jenkins (or another CI system) and get nice Emma generated reports. Here is what I got when I run it through my Jenkins setup.

Overall Code Coverage Summary
Coverage Breakdown
Coverage Breakdown
Coverage Breakdown