come utilizzare lo strumento java jcoverage utilizzando ANT in eclipse


Voglio preparare il rapporto jcoverage per i miei test unitari usando ANT ed eclipse. Sto usando

  • Apache Ant (TM) versione 1.8.3 compilato il 26 febbraio 2012
  • selenium-server-standalone-2.20.0
  • junit-4.8.1.vaso
  • eclipse sdk 3.6.1

Ho provato come menzionato in questo link qui .

Ma non sono in grado di avere successo.si prega di fornire se tutti i documenti sono disponibili o mi fanno chiaro come preparare rapporto.

La mia formica file:

 <?xml version="1.0"?>

<!DOCTYPE project [
   <!ENTITY start_stop_selenium SYSTEM "start_stop_selenium.xml">
   <!ENTITY properties_build_clean_compile_report SYSTEM  "properties_build_clean_compile_report.xml">
]>

<project name="Run Selenium Tests" default="all_selenium_tests" basedir=".">

        <property name="libs" location="lib" />
        <property name="build.dir" value="${basedir}/build/"/> 
        <property name="build.classes.dir" value="${build.dir}/classes"/> 
        <property name="build.test-classes.dir" value="${build.dir}/test-classes"/> 
        <property name="build.instrumented-classes.dir"  value="${build.dir}/instrumented-classes"/> 
        <property name="build.coverage.dir" value="${build.dir}/coverage"/> 
        <property name="build.reports.dir" value="${build.dir}/reports"/> 
        <property name="lib.dir" value="lib"/> 
        <property name="src" value="src"/> 
        <property name="src.dir" value="${basedir}/src/java"/>  
        <property name="test.dir" value="${basedir}/src/test"/>


<!-- Corresponding Jar file for the Jcoverage.-->
        <path id="jcoverage.path"> 
            <fileset dir="${lib.dir}"> 
                <include name="jcoverage-1.0.5.jar"/> 
                <include name="log4j-1.2.9.jar"/> 
                <include name="bcel-5.1.jar"/> 
                <include name="jakarta-oro-2.0.7.jar"/> 
                <include name="java-getopt-1.0.9.jar"/> 
            </fileset> 
        </path> 

   <path id="junit.class.path">
     <fileset dir="${lib.dir}">
        <include name="ant-junit.jar"/>
        <include name="jcoverage-1.0.5.jar"/> 
        <include name="log4j-1.2.9.jar"/> 
        <include name="bcel-5.1.jar"/> 
        <include name="jakarta-oro-2.0.7.jar"/> 
        <include name="java-getopt-1.0.9.jar"/> 
        <include name="junit-4.8.1.jar"/>
        <include name="selenium-server-standalone-2.20.0.jar"/> 
        <include name="jetty-repacked-7.6.1.jar"/> 
        <include name="org.mortbay.jetty-6.0.0alpha2.jar"/> 
        <include name="httpclient-4.1.2.jar"/>
        <include name="httpcore-4.1.3.jar"/>
        <include name="httpmime-4.1.2.jar"/>
        <include name="selenium-java-2.20.0.jar"/>
        <include name="selenium-java-2.20.0-srcs.jar"/>
        <include name="logging-selenium-1.2.jar"/>
        <include name="poi-3.7-20101029.jar"/>
        <include name="robotframework-2.5.4.1.jar"/>
        <include name="saxon-8.7.jar"/>
        <include name="jxl.jar"/>
    </fileset>
  </path>


        <target name="init"> 

            <!-- <delete dir="${build.dir}"/>
             <delete dir="${build.classes.dir}"/>
             <delete dir="{build.test-classes.dir}"/>
             <delete dir="${build.coverage.dir}"/>
             <delete dir="${build.instrumented-classes.dir}"/>
             <delete dir="${build.reports.dir}"/> -->

            <delete dir="${build.dir}"/>
            <mkdir dir="${build.dir}"/> 
            <mkdir dir="${build.classes.dir}"/> 
            <mkdir dir="${build.test-classes.dir}"/> 
            <mkdir dir="${build.coverage.dir}"/> 
            <mkdir dir="${build.instrumented-classes.dir}"/>
            <mkdir dir="${build.reports.dir}"/> 
        </target>

        <target name="compile" description="compile all classes"> 
                <javac srcdir="${src.dir}" destdir="${build.classes.dir}" failonerror="yes" debug="yes"> 
                </javac>
        </target>

        <target name="instrument" description="Add jcoverage instrumentation"> 
            <instrument todir = "${build.instrumented-classes.dir}"> 
            <ignore regex="org.apache.log4j.*"/> 
            <fileset dir="${build.classes.dir}"> 
            <include name="**/*.class"/> 
            </fileset> 
            </instrument>
        </target>


            <target name="test" description="Unit test the application"> 
                <javac srcdir="${test.dir}" destdir="${build.test-classes.dir}" failonerror="yes" debug="yes"> 
                <classpath refid="junit.class.path"/> 

                <classpath location="${build.classes.dir}"/> 
                </javac>
                <junit fork="yes" dir="${basedir}" errorProperty="test.failed" failureProperty="test.failed"> 

          <!-- note the classpath order, instrumented classes are before the original (uninstrumented) classes. -->

                <classpath refid="junit.class.path"/>
                <classpath location="${build.instrumented-classes.dir}"/> 
                <classpath location="${build.classes.dir}"/> 
                <classpath location="${build.test-classes.dir}"/> 

           <!-- the instrumented classes reference classes used by the jcoverage runtime. --> 

           <classpath refid="jcoverage.path"/> 
           <formatter type="xml"/>  

           <batchtest todir="${build.reports.dir}" > 
           <fileset dir="${build.test-classes.dir}"> 
           <include name="**/*Test.class"/> 
           </fileset> 
           </batchtest> 
           </junit> 
           </target>


    <taskdef classpathref="jcoverage.path" resource="tasks.properties"/>

          <target name="coverage" description="HTML and XML coverage reports can be found in build/coverage"> 
                <report srcdir="${src.dir}" destdir="${build.coverage.dir}"/> 
                <report srcdir="${src.dir}" destdir="${build.coverage.dir}" format="xml"/>  
                <echo> jcoverage reports have been generated. The HTML report is ${build.coverage.dir}/index.html The XML report is ${build.coverage.dir}/coverage.xml </echo> 
          </target>


  <target name="all_selenium_tests" description="The Main Target for running all tests">
        <antcall target="init"/>
        <antcall target="compile"/>
        <antcall target="instrument"/> 
        <antcall target="test"/>
        <antcall target="coverage"/>
 </target> 
</project>

La mia classe junit:

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;


public class sample extends SeleneseTestCase {
static Selenium selenium;
@Before
public void setUp() throws Exception {

      SeleniumServer seleniumserver=new SeleniumServer();
       seleniumserver.boot();
       seleniumserver.start();
       selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
       selenium.start();        

}

@Test
public void testjcoverage() throws Exception{

selenium.windowMaximize();
selenium.windowFocus();

selenium.setSpeed("1000");

selenium.open("/results-web/results/authentication/public/login?");


selenium.type("id=USER","labcorp" );
selenium.type("id=PASSWORD","Labcorp" );
selenium.click("css=input.loginButton");

selenium.stop();
}
}

Errore della console:

Buildfile: E:\jcoverage\jcoverage\refactored-param-build.xml
[taskdef] Could not load definitions from resource $(lib.dir). It could not be found.
all_selenium_tests:
[taskdef] Could not load definitions from resource $(lib.dir). It could not be found.
init:
[taskdef] Could not load definitions from resource $(lib.dir). It could not be found.
compiling:
[taskdef] Could not load definitions from resource $(lib.dir). It could not be found.
instrument:

BUILD FAILED
E:\jcoverage\jcoverage\refactored-param-build.xml:25: The following error occurred while executing this line:
E:\jcoverage\jcoverage\refactored-param-build.xml:96: Problem: failed to create task or type instrument
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.


Total time: 125 milliseconds

Si prega di fornire una soluzione.

Author: Manigandan, 2012-05-16

1 answers

L'errore informa che il compilatore javac non riesce a trovare i jar junit. Sebbene si specifichi junit.class.path come percorso di classe nella destinazione formica compiling, tale percorso di classe contiene riferimenti alla proprietà ${libs} che non è definita in nessuna parte della build.xml

Inoltre, è necessario spostare le definizioni delle proprietà (come lib.dir) prima di utilizzarle (ad esempio prima della definizione di jcoverage.path che fa riferimento a ${lib.dir})

Nota: puoi vedere il classpath usato da ant con l'opzione -verbose o la chiamata dell'attività <echo>${junit.class.path}</echo> prima dell'attività <javac>

 0
Author: Attila, 2012-05-16 14:30:49