In this tutorial, we will show you how to run a TestNG test in Ant build. 1. Run by Classes build.xml <taskdef name="testng" classname="org.testng.TestNGAntTask"> <classpath location="lib/testng-6.8.14.jar" /> </taskdef> <target name="testng" depends="compile"> <!– Assume test.path contains the project library dependencies –> <testng classpathref="test.path" outputDir="${report.dir}" haltOnFailure="true"> <!– Extra project classpath, which is not included in above "test.path" […]

Read more Ant and TestNG Task example

In this tutorial, we will show you how to test Spring’s components with TestNG. Tools used : TestNG 6.8.7 Spring 3.2.2.RELEASE Maven 3 Eclipse IDE 1. Project Dependencies To integrate Spring with TestNG, you need spring-test.jar, add the following : pom.xml <properties> <spring.version>3.2.2.RELEASE</spring.version> <testng.version>6.8.7</testng.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> […]

Read more TestNG + Spring Integration Example

In this tutorial, we will show you how to do the group testing in TestNG. 1. Groups on Methods Review a test group example. runSelenium() and runSelenium1() are belong to group selenium-test. testConnectOracle() and testConnectMsSQL() are belong to group database. runFinal() will be executed if groups selenium-test and database are passed. TestGroup.java package com.mkyong.testng.examples.group; import […]

Read more TestNG – Groups Test

A classic example, show you how to get started with TestNG unit test framework. Tools used : TestNG 6.8.7 Maven 3 Eclipse IDE 1. TestNG Dependency Add TestNG library in the pom.xml. pom.xml <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.7</version> <scope>test</scope> </dependency> 2. TestNG Example Review a simple class, has a method to return a fixed email “[email protected]”. […]

Read more TestNG Hello World Example

In TestNG, we use dependOnMethods and dependsOnGroups to implement dependency testing. If a dependent method is fail, all the subsequent test methods will be skipped, NOT failed. 1. dependOnMethods Example A simple example, “method2()” is dependent on “method1()”. 1.1 If method1() is passed, method2() will be executed. App.java package com.mkyong.testng.examples.dependency; import org.testng.annotations.Test; public class App […]

Read more TestNG – Dependency Test

In this tutorial, we will show you how to pass parameters into a @Test method, via XML @Parameters or @DataProvider. 1. Passing Parameters with XML In this example, the properties filename is passing from testng.xml, and inject into the method via @Parameters. TestParameterXML.java package com.mkyong.testng.examples.parameter; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; […]

Read more TestNG – Parameter Test (XML and @DataProvider)

In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka suite test. 1. Test Classes Review following three test classes. TestConfig.java package com.mkyong.testng.examples.suite; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; //show the use of @BeforeSuite and @BeforeTest public class TestConfig { @BeforeSuite public void testBeforeSuite() { System.out.println("testBeforeSuite()"); […]

Read more TestNG – Run multiple test classes (suite test)

In this tutorial, we will show you how to ignore a test method with @Test(enabled = false). TestIgnore.java package com.mkyong.testng.examples.ignore; import org.testng.Assert; import org.testng.annotations.Test; public class TestIgnore { @Test //default enable=true public void test1() { Assert.assertEquals(true, true); } @Test(enabled = true) public void test2() { Assert.assertEquals(true, true); } @Test(enabled = false) public void test3() { […]

Read more TestNG – How to ignore a test method

In this tutorial, we will show you how to use the TestNG expectedExceptions to test the expected exception throws in your code. 1. Runtime Exception This example shows you how to test a runtime exception. If the method divisionWithException () throws a runtime exception – ArithmeticException, it will be passed. TestRuntime.java package com.mkyong.testng.examples.exception; import org.testng.annotations.Test; […]

Read more TestNG – Expected Exception Test