【问题标题】:ANT BUILD ERROR : failed to create task or type testngANT BUILD ERROR:未能创建任务或类型 testng
【发布时间】:2015-12-14 00:19:06
【问题描述】:

您好,问题是,我无法在我的 项目,它给了我错误。当我触发 ANT clean 和 ANT compile ,建立成功。

BUILD.XML 代码错误行 在线获取错误:2

<!-- run -->
<target name="run" depends="compile">
<testng classpath="${test.classpath}:${test.dest}" suitename="suite1" outputDir="test-output" >  
<xmlfileset dir="${ws.home}" includes="TestNG.xml" />
</testng>
<!--suite 1 begin -->
<property name="suite.web.CopyRegressionCustomer" value="CopyRegressionCustomer" />
<testng classpath="${test.classpath}:${test.dest}" suitename="CopyRegressionCustomer" outputDir="test-output" >
<xmlfileset dir="${ws.home}" includes="TestNG.xml" />
</testng>
<antcall target="testngReportCopy">
<param name="testngDir" value="${testng.report.dir}${suite.web.CopyRegressionCustomer}"/>
 </antcall> 

错误日志: 问题:未能创建任务或类型 testng 原因:名称未定义。 行动:检查拼写。 行动:检查是否已声明任何自定义任务/类型。 行动:检查任何 / 声明已经发生。

完整的 BUILD.xml 代码

<?xml version="1.0" encoding="UTF-8"?>

<project name="projectName" default="clean" basedir="."> <!-- dot indicates that basedir is pointing towards project root directory -->

 <!-- ========== Initialize Properties =================================== -->
 <property name="ws.home" value="${basedir}" />
 <property name="ws.jars" value="/Users/mac/Downloads/selenium-2.47.1" />
 <property name="test.dest" value="${ws.home}/build"/>
 <property name="test.src" value="${ws.home}/src"/>
 <property name="ng.result" value="test-output"/>

 <echo> value of base dir = ${basedir} </echo>

 <!-- properties for copying the results -->
 <tstamp>
 <format property="year" pattern="yyyy" />
 <format property="DSTAMP" pattern="yyyy-MM-dd" />
 <format property="TSTAMP" pattern="HH:mm:ss" />
 <format property="dateversion" pattern="yyyy.MM.dd.HH:mm:ss" />
 <format property="time.stamp" pattern="yyyy-MM-dd_HH-mm-ss aa_"/>
 </tstamp>

 <property name="testng.report.dir" value="${ws.home}/testngReports/${time.stamp}"/>
 <property name="testngXslt.report.dir" value="${ws.home}/testngXsltReports/${time.stamp}"/>

 <!-- ====== For setting the classpath ==== -->
 <target name="setClassPath" unless="test.classpath">
 <path id="classpath_jars">
 <fileset dir="${ws.jars}" includes = "*.jar"/>
 </path>
 <pathconvert pathsep=":"
 property="test.classpath"
 refid="classpath_jars" />

 </target>

 <!-- ============ Initializing other stuff =========== -->
 <target name="init" depends="setClassPath">
 <tstamp>
 <format property="start.time" pattern="MM-dd-yyyy (HH-mm-ss)"/>
 </tstamp>
 <condition property="ANT"
 value="$(env.ANT_HOME)/bin/ant.bat"
 else="$(env.ANT_HOME)/bin/ant">
 <!-- os family="windows" /-->
 <os family="mac" />
 </condition>



 <!--
 <taskdef name="testng" classpath="/Users/mac/Downloads/selenium-2.47.1" classname="org.testng.TestNGAntTask" /> 
 <!- use direct path for classpath if you don't prefer to use 'lib' directory: classpath="/Users/yash/Documents/Jar Files/testng-6.8.jar" ->
 <taskdef name="testng" classname="org.testng.TestNGAntTask">
 <classpath>
 <pathelement location="./lib/testng.jar"/>
 </classpath>
 </taskdef>
 -->

 </target>

 <target name="all">
 </target>

 <!-- cleaning the destination folders -->
 <target name="clean">
 <echo message="deleting existing build directory"/>
 <delete dir="${test.dest}"/>
 </target>

 <!-- target for compiling the java files -->
 <target name="compile" depends="init, clean">
 <delete includeemptydirs="true" quiet="true">
 <fileset dir="${test.dest}" includes="**/*"/>
 </delete>
 <echo message="making directory....."/>
 <mkdir dir="${test.dest}"/>
 <echo message="classpath-------: ${test.classpath}"/>
 <echo message="compiling....."/>
 <javac
 debug="true"
 destdir="${test.dest}"
 srcdir="${test.src}"
 classpath="${test.classpath}"
 includeantruntime="true"/>
 </target>

 <!-- build -->
 <target name="build" depends="init">
 </target>

 <!-- ========== Test executions & Generating reports using Testng utility for multiple suites ============== -->
 <!-- run -->
 <target name="run" depends="compile">
    <testng classpath="${test.classpath}:${test.dest}" suitename="suite1" outputDir="test-output" >
     <xmlfileset dir="${ws.home}" includes="TestNG.xml" />
     </testng>
 <!--suite 1 begin -->
 <property name="suite.web.CopyRegressionCustomer" value="CopyRegressionCustomer" />
 <testng classpath="${test.classpath}:${test.dest}" suitename="CopyRegressionCustomer" outputDir="test-output" >
 <xmlfileset dir="${ws.home}" includes="TestNG.xml" />
 </testng>
 <antcall target="testngReportCopy">
 <param name="testngDir" value="${testng.report.dir}${suite.web.CopyRegressionCustomer}"/>
 </antcall> 

 <!--suite 2 begin -->
 <property name="suite.web.Copy2RegressionCustomer" value="Copy2RegressionCustomer" />
 <testng classpath="${test.classpath}:${test.dest}" suitename="Copy2RegressionCustomer" outputDir="test-output" >
 <xmlfileset dir="." includes="webSuites/Copy2RegressionCustomer.xml" />
 </testng>
 <antcall target="testngReportCopy">
 <param name="testngDir" value="${testng.report.dir}${suite.web.Copy2RegressionCustomer}"/>
 </antcall> 

 </target>

 <target name="testngReportCopyAndReportParser">
 <!-- Copy to TestNG report directory-->
 <mkdir dir="${testngDir}">
 </mkdir>

 <!-- to copy previous suite result to TestNG report directory -->
 <copy todir="${testngDir}">
 <fileset dir="test-output"/>
 </copy> <!-- end of Testng Report -->
 </target>

 <!-- ========== Test executions & Generating reports using Testng XSLT utility for multiple suites ============== -->
 <!-- run -->
 <target name="runAsTestngXslt" depends="compile">

 <!--suite 1 begin -->
 <property name="suite.web.CopyRegressionCustomer" value="CopyRegressionCustomer" />
 <testng classpath="${test.classpath}:${test.dest}" suitename="CopyRegressionCustomer" outputDir="test-output" >
 <xmlfileset dir="." includes="webSuites/CopyRegressionCustomer.xml" />
 </testng>
 <antcall target="testngXsltReportCopy">
 <param name="testngXsltDir" value="${testngXslt.report.dir}${suite.web.CopyRegressionCustomer}"/>
 </antcall> 

 <!--suite 2 begin -->
 <property name="suite.web.Copy2RegressionCustomer" value="Copy2RegressionCustomer" />
 <testng classpath="${test.classpath}:${test.dest}" suitename="Copy2RegressionCustomer" outputDir="test-output" >
 <xmlfileset dir="." includes="webSuites/Copy2RegressionCustomer.xml" />
 </testng>
 <antcall target="testngXsltReportCopy">
 <param name="testngXsltDir" value="${testngXslt.report.dir}${suite.web.Copy2RegressionCustomer}"/>
 </antcall> 

 </target>

 <target name="testngXsltReportCopyAndReportParser">
 <!-- Copy to TestNG report directory-->
 <mkdir dir="${testngXsltDir}">
 </mkdir>

 <!-- to copy previous suite result to TestngXslt report folder -->
 <xslt in="${ws.home}/test-output/testng-results.xml" style="src/xslt/testng-results.xsl"
 out="${testngXsltDir}/index.html" classpathref="test.c" processor="SaxonLiaison">

 <param name="testNgXslt.outputDir" expression="${testngXsltDir}" />
 <param name="testNgXslt.showRuntimeTotals" expression="true" />
 <param name="testNgXslt.sortTestCaseLinks" expression="true" />
 <param name="testNgXslt.testDetailsFilter" expression="FAIL,SKIP,PASS,CONF,BY_CLASS" />
 </xslt> <!-- end of TestngXslt Report -->
 </target>

 <path id="test.c">
 <fileset dir="${ws.jars}" includes="*.jar">
 <include name="mail.jar"/>
 <include name="activation-1.0.2.jar"/>
 </fileset>
 </path>

 <!-- ========== Generating reports using Testng XSLT utility for single suite only ============== -->
 <target name="report" depends="run">
 <!-- TestngXslt report -->
 <mkdir dir="${testngXslt.report.dir}">
 </mkdir>

 <!-- to copy previous suite result to TestngXslt report folder -->
 <xslt in="${ws.home}/test-output/testng-results.xml" style="src/xslt/testng-results.xsl"
 out="${testngXslt.report.dir}/index.html" classpathref="test.c" processor="SaxonLiaison">

 <param name="testNgXslt.outputDir" expression="${testngXslt.report.dir}" />
 <param name="testNgXslt.showRuntimeTotals" expression="true" />
 <param name="testNgXslt.sortTestCaseLinks" expression="true" />
 <param name="testNgXslt.testDetailsFilter" expression="FAIL,SKIP,PASS,CONF,BY_CLASS" />
 </xslt> <!-- end of TestngXslt Report -->
 </target>

 <target name="RunAndViewReport" depends="report">
 <exec executable="${browser}" spawn="yes">
 <arg line="'file:///${testngXslt.report.dir}/index.html'" />
 </exec>
 </target>

 <target name="sendMail" depends="RunAndViewReport">
 <zip destfile="${testngXslt.report.dir}/Report.zip" basedir="${testngXslt.report.dir}"/>
 <mail mailhost="smtp.gmail.com" mailport="465" subject="Notification of TESTNG build result" ssl="false" user="automationmoolya@gmail.com" password="moolya@universe">
 <from address="automationmoolya@gmail.com"/>
 <to address="yagnesh@moolya.com"/>
 <message>The build has finished. A details report of this build is attached</message>
 <attachments>
 <fileset dir="testngXslt.report.dir">
 <include name="**/*.zip"/>
 </fileset>
 </attachments>
 </mail>
 </target>

 <target name="install-jars" description="Install ANT optional jars">
 <get dest="${ws.home}/lib/mail.jar" src="file:///${ws.home}/lib/mail.jar"/>
 <fileset dir="${ws.jars}" includes="*.jar">
 <include name="mail.jar"/>
 <include name="activation-1.0.2.jar"/>
 </fileset>
 </target>

</project>

【问题讨论】:

    标签: ant selenium-webdriver build.xml


    【解决方案1】:

    仔细查看您的 ant 脚本,它用于将 TestNG ant 任务定义为:

    <taskdef resource="testngtasks" classpath="testng.jar"/>
    

    但在你的情况下,你已经评论了这个定义:

    <!--
    <taskdef name="testng" classpath="/Users/mac/Downloads/selenium-2.47.1" classname="org.testng.TestNGAntTask" /> 
    <!- use direct path for classpath if you don't prefer to use 'lib' directory: classpath="/Users/yash/Documents/Jar Files/testng-6.8.jar" ->
    <taskdef name="testng" classname="org.testng.TestNGAntTask">
    <classpath>
    <pathelement location="./lib/testng.jar"/>
    </classpath>
    </taskdef>
    -->
    

    您需要取消注释或在脚本中添加一个新的定义。

    【讨论】:

    • 它仍然对我不起作用。
    • 在我看来,你需要一个 被取消注释,然后指向一个现有的 testng 库
    • taskdef class org.testng.TestNGAnTask cannot be found using the classloader AntClassLoader[] 现在,出现此错误。
    • 确保你有一个位置为 ./lib/testng.jar 的 testng.jar,或者尝试在没有类名的情况下定义它,就像在
    • @stanisiav 请检查我的回答。
    猜你喜欢
    • 1970-01-01
    • 2015-02-18
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2013-03-09
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多