我在这里集成test .xml在build.xml文件。但是当我尝试运行Build .xml时,我得到"Build failed"错误。我不知道为什么。如果你有什么建议,请告诉我。也请审查我的执行步骤,以便您更好地理解。如果有任何错误,请告诉我。
执行步骤:- 删除bin,docs,jar文件夹如果已经存在
- 为bin,docs,jar创建新文件夹
- 设置外部jar的类路径。在设置类路径之前,我把"testng-6.8.5.jar"放在lib文件夹中(目前我只使用一个jar文件)
- 编译源代码并将。class文件拷贝到bin文件夹
- 运行特定的"target"以从TestNG获得测试报告
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>
这个TestNG JAR里面没有JCommander文件。这是TestNG ant发行版的一个bug。您可以使用这个ZIP中的JAR http://testng.org/testng-6.0.1.zip
一个人可能喜欢参考这个线程以获得更多信息https://github.com/cbeust/testng/issues/255