尝试使用Ant运行TestNG测试时出现“Build Failed”错误

时间:2015-09-08 07:42:49

标签: java xml ant

这里我在build.xml文件中集成了testng.xml。但是当我尝试运行build.xml时,出现“Build failed”错误。我不知道为什么。如果你有什么想法,请建议我。另外,请查看我的执行步骤以便您更好地理解。如果有任何错误,请告诉我。

执行步骤:

  1. 删除bin,docs,jar文件夹(如果已经存在)
  2. 为bin,docs,jar
  3. 创建新文件夹
  4. 设置外部jar的类路径。
  5. 在设置classpath之前,我将“testng-6.8.5.jar”放在lib文件夹中(目前只使用一个jar文件)
  6. 编译源代码并将.class文件复制到bin文件夹
  7. 运行特定的“目标”以从TestNG
  8. 获取测试报告

    在build.xml

    <?xml version="1.0"?>
    <project name="AntBasics" default="main" basedir=".">
        <description>Running TestNG with Ant</description>
    
        <!-- Properties Declaration -->
    
        <property name="src.dir" location="src" />
        <property name="test.dir" location="src" />
        <property name="lib.dir" location="lib" />
        <property name="build.dir" location="bin" />
        <property name="dist.dir" location="dist" />
        <property name="docs.dir" location="docs" />
        <property name="docs.dir" location="docs" />
        <property name="test.report.dir" location="test-output" />
        <property name="resources.dir" location="testng.xml" />
    
    
        <!-- set classpath -->
        <path id="build.classpath">
            <fileset dir="${lib.dir}">
                <include name="**/*.jar" />
            </fileset>
        </path>
    
        <!-- Clean bin,dist,docs,test-output -->
        <target name="clean">
            <delete dir="${build.dir}" />
            <delete dir="${dist.dir}" />
            <delete dir="${docs.dir}" />
            <delete dir="${test.report.dir}" />
        </target>
    
        <!-- Create build,dist,docs,test-output-->
        <target name="makedir">
            <mkdir dir="${build.dir}" />
            <mkdir dir="${dist.dir}" />
            <mkdir dir="${docs.dir}" />
        </target>
    
        <!-- Compile Java code-->
        <target name="compile" depends="clean,makedir">
            <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />
        </target>
    
        <!-- Create Javadocs-->
        <target name="docs" depends="compile">
            <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
                <fileset dir="${src.dir}">
                    <include name="**/*.java" />
                </fileset>
            </javadoc>
        </target>
    
        <!-- Make jar using Java class files-->
        <target name="jar" depends="compile">
            <jar destfile="${dist.dir}\myna.ant.test.jar" basedir="${build.dir}">
                <manifest>
                    <attribute name="Main-class" value="myPack2.TestNGMain" />
                </manifest>
            </jar>
        </target>
    
        <!-- Define task for TestNG-->
        <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="build.classpath">
            <classpath location="lib/testng-6.8.5.jar" />
        </taskdef>
    
        <!-- Mention the TestNG Task and Make Test Reports-->
        <target name="runTest" depends="compile,jar,docs">
            <mkdir dir="${test.report.dir}" />
            <testng outputdir="${test.report.dir}" haltOnFailure="true" classpathref="build.classpath">
                <xmlfileset dir="." includes="testng.xml" />
            </testng>
        </target>
    
        <!-- Default Main Target-->
        <target name="main" depends="runTest">
            <description>Main Application</description>
        </target>
    
    </project>
    

    在控制台中

    Buildfile: /home/vaav/workspace/AntBasics/build.xml
    clean:
       [delete] Deleting directory /home/vaav/workspace/AntBasics/bin
       [delete] Deleting directory /home/vaav/workspace/AntBasics/dist
       [delete] Deleting directory /home/vaav/workspace/AntBasics/docs
       [delete] Deleting directory /home/vaav/workspace/AntBasics/test-output
    makedir:
        [mkdir] Created dir: /home/vaav/workspace/AntBasics/bin
        [mkdir] Created dir: /home/vaav/workspace/AntBasics/dist
        [mkdir] Created dir: /home/vaav/workspace/AntBasics/docs
    compile:
        [javac] Compiling 3 source files to /home/vaav/workspace/AntBasics/bin
    jar:
          [jar] Building jar: /home/vaav/workspace/AntBasics/dist/myna.ant.test.jar
    docs:
      [javadoc] Generating Javadoc
      [javadoc] Javadoc execution
      [javadoc] Loading source file /home/vaav/workspace/AntBasics/src/myPack/Test1.java...
      [javadoc] Loading source file /home/vaav/workspace/AntBasics/src/myPack2/TestMain.java...
      [javadoc] Loading source file /home/vaav/workspace/AntBasics/src/myPack2/TestNGMain.java...
      [javadoc] Constructing Javadoc information...
      [javadoc] /home/vaav/workspace/AntBasics/src/myPack2/TestNGMain.java:3: error: package org.testng.annotations does not exist
      [javadoc] import org.testng.annotations.Test;
      [javadoc]                              ^
      [javadoc] /home/vaav/workspace/AntBasics/src/myPack2/TestNGMain.java:8: error: cannot find symbol
      [javadoc]     @Test
      [javadoc]      ^
      [javadoc]   symbol:   class Test
      [javadoc]   location: class TestNGMain
      [javadoc] Standard Doclet version 1.8.0_45
      [javadoc] Building tree for all the packages and classes...
      [javadoc] javadoc: warning - Class Test not found.
      [javadoc] javadoc: warning - Class Test not found.
      [javadoc] javadoc: warning - Class Test not found.
      [javadoc] javadoc: warning - Class Test not found.
      [javadoc] javadoc: warning - Class Test not found.
      [javadoc] Building index for all the packages and classes...
      [javadoc] Building index for all classes...
      [javadoc] Generating /home/vaav/workspace/AntBasics/docs/help-doc.html...
      [javadoc] 8 warnings
      [javadoc] javadoc: warning - Class Test not found.
    runTest:
        [mkdir] Created dir: /home/vaav/workspace/AntBasics/test-output
       [testng] Error: A JNI error has occurred, please check your installation and try again
       [testng] Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
       [testng]     at java.lang.Class.getDeclaredMethods0(Native Method)
       [testng]     at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
       [testng]     at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
       [testng]     at java.lang.Class.getMethod0(Class.java:3018)
       [testng]     at java.lang.Class.getMethod(Class.java:1784)
       [testng]     at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
       [testng]     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
       [testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
       [testng]     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
       [testng]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
       [testng]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
       [testng]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
       [testng]     ... 7 more
    
    BUILD FAILED
    /home/vaav/workspace/AntBasics/build.xml:71: The tests failed.
    
    Total time: 1 second
    

    以下是我的java代码

    package myPack2;
    import org.testng.annotations.Test;
    import myPack.Test1;
    public class TestNGMain {
    
        @Test
        public void getTest(){
            Test1 test = new Test1();
            System.out.println("Result===========> "+test.division(20, 10));
        }
    
    }
    

    的testng.xml

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="AntBasics">
        <test name="order">
            <classes>
                <class name="myPack2.TestNGMain" />
            </classes>
        </test> 
    </suite>
    

1 个答案:

答案 0 :(得分:1)

TestNG JAR中没有JCommander文件。这是TestNG蚂蚁发布的一个错误。 您可以使用此ZIP http://testng.org/testng-6.0.1.zip

中的JAR

有人可能会参考此主题以获取更多信息 https://github.com/cbeust/testng/issues/255