ANT编译工作正常,但ANT运行不正常构建失败"无法创建任务或键入testng"

时间:2017-09-29 10:49:44

标签: ant

下面是我的Build.xml文件。当我编译我的项目时,它会显示Build Success,但是当我运行它时,Build Failed"无法创建任务或输入testng"原因:名称未定义。 行动:检查拼写。 操作:检查是否已声明任何自定义任务/类型。 操作:检查是否已发生任何/声明。

    <?xml version="1.0" encoding="UTF-8"?>
<project name="projectName" default="clean" basedir="C:\Users\saad bin usman\workspace\testNG"> <!-- dot indicates that basedir is pointing towards project root directory -->

 <!-- ========== Initialize Properties =================================== -->
 <property name="ws.home" value="${basedir}" />
 <property name="ws.jars" value="D:\jars" />
 <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>



 <!--
 <!- 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">

    <taskdef resource="testngtasks" classpath="D:\saad bin usman\saad's doc\softwares\eclipse\plugins\org.testng.eclipse_6.9.8.201510130443\lib\*.jar" />

    <testng classpath="classpath.test" suitename="smsweb" outputDir="test-output" >
        <xmlfileset dir="${ws.home}" includes="testng.xml" />
    </testng>


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

2 个答案:

答案 0 :(得分:0)

使用如下运行

     <!-- run -->
<target name="run" depends="compile">
    <testng classpath="${test.classpath}:${test.dest}"> 
        <xmlfileset dir="${ws.home}" includes="testng.xml"/>
    </testng>
</target>

答案 1 :(得分:0)

您的taskdef嵌套在您尝试使用的任务中。现在它是如何运作的。首先定义任务,然后运行它。

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

<testng classpathref="classpath.test" suitename="smsweb" outputDir="test-output" >
    <xmlfileset dir="${ws.home}" includes="testng.xml" />
</testng>