Reading:  

Working with TestNG - Starters Guide


Plugin testNG with ANT

To run TestNG using ANT follow the below steps:

We need to download the latest version of Apache Ant, follow the link http://ant.apache.org/bindownload.cgi to download ant.

To Set Ant Environment:

To set the ANT_HOME environment variable give the path of directory location of ANT libraries is saved.

  • For windows environmentset ANT_HOME to C:\user\Apache Software Foundation\apache-ant-1.9.4
  • For Linux export ANT_HOME=/user/apache-ant-1.9.4
  • For Mac export ANT_HOME=/Library/apache-ant-1.9.4 

To add Ant compiler location to System Path for different operating system:

  • For windows add path %ANT_HOME\bin to the end of the system variable path.
  • For Linux export PATH=$PATH:$ANT_HOME/bin/
  • For Mac not required

Create New Project:

  • Create folder TestNGWithAnt in C:\>Users\testng\workspace
  • Create folder src in C:\>Users\testng\workspace >TestNGWithAnt
  • Create folder test in C:\>Users\testng\workspace >TestNGWithAnt
  • Create folder lib in C:\>Users\testng\workspace >TestNGWithAnt
  • Create ListofColor.java class in C:\>Users\testng\workspace >TestNGWithAnt>src

Create ListofColor.java:

package testNG;

import java.util.Arrays;

public class ListofColor {
	public String[] colorContainer() {
		String[] container = {
			"Green", "Red", "Yellow"
		};
		System.out.println("The List of colors: " + Arrays.toString(container));
		return container;
	}

	public String[] addThings() {
		String[] container = {
			"Zen", "Swift", "Maruthi", "Nano"
		};
		System.out.println("Now the container: " + Arrays.toString(container));
		return container;
	}
}

 

Create TestA class:

package testNG;

import org.testng.Assert.*;
import org.testng.Test;

public class TestA {

	ListofColor list = new ListofColor();
	String[] bagA = {
		"Green", "Red", "Yellow"
	};
	String[] bagB = {
		"Holden", "Swift", "Ford", "Nissan"
	};

	@Test
	public void testContainer() {
		System.out.println("Inside testComtainer()");
		assertArrayEquals(bagA, list.colorContainer());
	}

	@Test
	public void testAddThings() {
		System.out.println("Inside testAddThings()");
		assertArrayEquals(bagB, list.addThings());
	}
}

 

Add JUnit.jar to C:\>Users\testng\workspace>TestNGWithAnt>lib

<taskdef name="tesngtest" classname="org.testng.TestNGAntTest">
<classpath>
<pathelement location="lib/testng-6.8.jar"/>
</classpath>
</taskdef>

Use  <testng> task in Ant to execute our TestNG test cases.

The build.xml file is as follows: 

Create Build.xml of Ant:

First define TestNG Ant task as follows:

<project name="TestNGAntTest" default="test" basedir=".">
    <!-- Define <testng> task -->
    <taskdef name="testngtest" classname="org.testng.TestNGAntTest">
        <classpath>
            <pathelement location="lib/testng-6.8.jar"/>
        </classpath>
    </taskdef>
    <property name="testdir" location="test" />
    <property name="srcdir" location="src" />
    <property name="libdir" location="lib" />
    <property name="full-compile" value="true" />
    <path id="classpath.base"/>
    <path id="classpath.test">
        <filesetdir="${libdir}">
            <include name="**/*.jar" />
        </fileset>
        <pathelement location="${testdir}" />
        <pathelement location="${srcdir}" />
        <path refid="classpath.base" />
	</path>
    <target name="clean" >
        <delete verbose="${full-compile}">
            <filesetdir="${testdir}" includes="**/*.class" />
        </delete>
    </target>
    <target name="compile" depends="clean">
        <javacsrcdir="${srcdir}" destdir="${testdir}" verbose="${full-compile}">
            <classpathrefid="classpath.test"/>
        </javac>
    </target>
    <target name="test" depends="compile">
        <testngoutputdir="${testdir}" classpathref="classpath.test">
            <xmlfilesetdir="${srcdir}" includes="testng.xml"/>
        </testng>
    </target>
</project>

 

After execution of Build.xml file the output is:

test:
[testng] [TestNG] Running:
[testng] C:\>Users\testng\Workspace\TestNGWithAnt\src\testng.xml
[testng]
[testng] Inside testContainer()
[testng] The List of colors:[Green, Red, Yellow]
[testng] Inside testAddThings()
[testng] Now the container:[Holder, Swift, Ford, Nissan]
[testng]
[testng] ==============================================
[testng] test Suite
[testng] Total tests run: 2, Failures: 0, Skips: 0
[testng] ===============================================
[testng]
BUILD SUCCESSFUL
Total time: 0 second


This ends our coverage on testNG. Share your feedback and experiences with testNG using our commenting section. Get help and help others

 

Description

This tutorial is focused on getting you started on TestNG, the testing framework inspired from JUnit and NUnit. Here is a quick table of contents

  1. What is TestNG?
  2. Environment Set-up
  3. Writing Tests
  4. Basic Annotations
  5. Execution Procedure
  6. Executing Tests
  7. Suite Test
  8. Ignore Test
  9. Group Test
  10. Exception Test
  11. Dependency Test
  12. Parametrized Test
  13. JUnit Tests
  14. Test Reports
  15. Running tests without Eclipse
  16. Plugin with ANT

 

 



Environment

A computer capable of running Java. IntelliJ Community IDE is also required for this course.

Prerequisites

Good knowledge of Java programming language and eclipse is essential.

Audience

Students looking to get started with TestNG

Learning Objectives

This tutorial will get you started with TestNG.

Author: Subject Coach
Added on: 12th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!