1

我想使用 ANT 和 eclipse 为我的单元测试准备 jcoverage 报告。我在用着

  • Apache Ant(TM) 版本 1.8.3 于 2012 年 2 月 26 日编译
  • selenium-server-standalone-2.20.0
  • junit-4.8.1.jar
  • 日食 SDK 3.6.1

我试过这里链接中提到的。

但我无法成功。请提供是否有可用的文件或让我清楚如何准备报告。

我的蚂蚁文件:

 <?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>

我的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();
}
}

控制台错误:

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

请提供解决方案。

4

1 回答 1

0

该错误通知您 javac 编译器找不到 junit jar。尽管您将 指定为ant 目标junit.class.path中的类路径,但该类路径包含对未在 build.xml 中的任何位置定义的属性的引用compiling${libs}

lib.dir此外,您应该在使用它们之前移动属性定义(例如)(例如,在jcoverage.path引用的定义之前${lib.dir}

注意:您可以看到使用-verbose选项调用 ant 或在<echo>${junit.class.path}</echo>任务之前调用<javac>任务使用的类路径

于 2012-05-16T14:30:49.673 回答