Akshay
Akshay

Reputation: 135

Execute TestNG.xml file from Build.xml(ANT)

Below is my Build.xml file. I am trying to execute my TestNG.xml through Build.xml but I am getting the below error everytime.

[testng] Cannot find class in classpath:

The below file is working fine till the compile target. I guess there are some issues with the code written in exec target.

If I try executing my TestNG.xml file alone, it works perfectly fine. No issues. But the issue comes when I execute my TestNG.xml from Build.xml.

I have tried resolving the issue by a number of methods that I found on stackoverflow such as:

Can anybody help me on this ?

<property name="lib" value="./lib" />

<property name="bin" value="./bin" />

<path id="Sample Ant build.classpath">
    <pathelement location="${output}" />
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
</path>

<target name="init">
    <delete dir="${bin}">
    </delete>
    <mkdir dir="${bin}" />
</target>

<target name="compile" depends="init">
    <javac srcdir="${src}" 
         destdir="${bin}"
        includeantruntime="false"   
         debug="true">
        <classpath refid="Sample Ant build.classpath"/>
    </javac>
</target>

<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="Sample Ant build.classpath" />

<target name="exec" depends="compile">
    <mkdir dir="${testng_report_dir}" />
    <testng outputdir="${testng_report_dir}" classpathref="Sample Ant build.classpath" useDefaultListeners="true">
        <xmlfileset dir="${basedir}" includes="Testng.xml" />
    </testng>

</target>

Upvotes: 2

Views: 1495

Answers (1)

Akshay
Akshay

Reputation: 135

The issue was in the classpath that I had set in the build.xml. Below is the correct classpath.

<path id="classpath">
    <pathelement location="${bin}" />
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
</path>

${bin} is the directory where your .class file resides after the compilation of your .java file.

Upvotes: 1

Related Questions