Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

TestNG Framework Overview

In this article I shall discuss about the Testing framework called TestNG, a testing framework inspired from JUnit and NUnit.

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use.

TestNG is designed to cover all categories of tests: Unit, functional, end-to-end, integration, etc. and it requires JDK 5 or higher.

TestNG was created by Cedric Beust. It is an open source testing framework. NG of TestNG stands for Next Generation. If you are aware of Junit test framework, then TestNG is similar to JUnit especially JUnit 4; but not an extension of JUnit.

TestNG Features

  • Annotations.
  • TestNG uses more Java and OO features.
  • Supports testing integrated classes (e.g., by default, no need to create a new test class instance for every test method).
  • Separate compile-time test code from run-time configuration/data info.
  • Flexible runtime configuration.
  • Introduces ‘test groups’. Once you have compiled your tests, you can just ask TestNG to run all the "front-end" tests, or "fast", "slow", "database", etc...
  • Supports Dependent test methods, parallel testing, load testing, partial failure.
  • Flexible plug-in API.
  • Support for multi threaded testing.

Environment Set Up

To start using TestNG to test your code, first you need to set it up. First of all download the latest version of TestNG jar from the link : http://testng.org/doc/download.html

Pre-Requisites to use TestNG:

  • JDK 1.5 and above
  • TestNG jar

Make sure you set the JAVA_HOME (I.e path of your JDK on your system) in the classpath. Also set the path of your TestNG jar in the classpath.

In my case the JAVA_HOME = C:\Program Files\Java\jdk1.7.0_25
TESTNG_HOME = C:\TESTNG
And classpath is set as: %CLASSPATH%;%TESTNG_HOME%\testng-6.8.jar;%JAVA_HOME%\bin;

Example

Now let us check an example on how to use TestNG to test our Java code. We would be testing from command line and no IDE is used.

Listing 1:Create a Java Class which needs to be tested. Let's create a class PrintMessage whose contents are as below:

public class PrintMessage {
	String message;
	public PrintMessage(String message) {
		this.message = message;
	}
	public String outputMessage() {
		System.out.println(message);
		return message;
	}
}

This is a simple class with a method called outputMessage() which prints data from message.

Let's create a Java class SampleTest.java under the directory C:\TestNGExercise.

Listing 2: Create Test class.

import org.testng.Assert;
import org.testng.annotations.*;

public class SampleTest {
	String message = "welcome to mrbool";
	PrintMessage printMessage = new PrintMessage(message);
	@Test
	public void testMethod() {
		
		System.out.println("Hi I'm testMethod1");
		Assert.assertEquals("welcome to mrbool", 
printMessage.outputMessage());
	}

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("Hi I'm beforeMethod");
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("Hi I'm afterMethod");
	}
	@BeforeClass
	public void beforeClass() {
		System.out.println("Hi I'm beforeClass");
	}
	@AfterClass
	public void afterClass() {
		System.out.println("Hi I'm afterClass");
	}
	@BeforeTest
	public void beforeTest() {
		System.out.println("Hi I'm beforeTest");
	}
	@AfterTest
	public void afterTest() {
		System.out.println("Hi I'm afterTest");
	}
	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Hi I'm beforeSuite");
	}
	@AfterSuite
	public void afterSuite() {
		System.out.println("Hi I'm afterSuite");
	}
}

Details of the above file are:

  • This is typical testNG test class. It a test method called testMethod(). This class tests the method printMessage.outputMessage().
  • The other methods are useful in real time testing. Right now I'm only printing a text from these methods, which will give you an idea as to when these methods are invoked during the test execution.

Lisitng 3: Write testng.xml

The TestNG test cases can be invoked by any of the following means:

  • With a testng.xml file
  • With ant
  • From the command line

Here I would be using the testng.xml option. Below Is the file testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Sample Test Suite">
   <test name="Sample test">
    <classes>
      <class name="SampleTest" />
    </classes>
  </test>
</suite>

Details of the above file are as below:

  • A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
  • Tag <test> represents one test and can contain one or more TestNG classes.
  • <class> tag represents a TestNG class is a Java class that contains at least one TestNG annotation. It can contain one or more test methods.
  • The <classes> tag can include any number of class tags.

Now let us execute the above code. First compile the java classes PrintMessage.java SampleTest.java using the javac compiler.

Listing 4: Execute the test case

javac  PrintMessage.java SampleTest.java

Note:Its assumed that you have set the JDK and TestNG jar in your classpath.

Next run the testng.xml using the TestNG class as below:

java -cp "C:\TestNGExercise" org.testng.TestNG testng.xml
You will the following output on your screen:

Listing 5: Verify the ouptut

[TestNG] Running: 
  C:\TestNGExercise\testng.xml 

Hi I'm beforeSuite 
Hi I'm beforeTest 
Hi I'm beforeClass 
Hi I'm beforeMethod 
Hi I'm testMethod1 
welcome to mrbool 
Hi I'm afterMethod 
Hi I'm afterClass 
Hi I'm afterTest 
Hi I'm afterSuite 

=============================================== 
Sample Test Suite 
Total tests run: 1, Failures: 0, Skips: 0 
=============================================== 

Here you can see the sequence in which the methods are being called.

Along with this output TestNG also creates a nice HTML report. Once the test executes, you will see a folder by name test-output created in your C:\TestNGExercise folder. Open the index.html under the test-output folder which should look like as below:

Test output

Figure 1: Test output

Open the Sample test.html in the folder test-output/Sample Test Suite. This page should look like as below:

Sample test.html

Figure 2: Sample test.html

Here you can see the method names and their success and fail status. For success the color is green and fail the color is red. If the test is skipped it show some other color.

Thats all for now!!

Conclusion

In this article we saw the basics of TestNG . We also saw how to set up the environment and tried and tested an example code. This article just helps you to kick start with the TestNG framework.

There are many more features which I shall discuss in the forthcoming articles.



I''m a full stack developer with around 10+ yrs of experience. I enjoy writing technical articles on upcoming technical trends.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login