TestNG is an automated test framework which can be used to make all kinds of tests includes unit test, integration test, and functional test. It is inspired by JUnit but more easier and powerful than JUnit. It requires Jdk5.0 or higher. You can use it to test just a single java method or an enterprise-level application. It can be used by both developers and QA engineers.
1. TestNG ( Test Next Generation) Benefits.
- Open source and totally free.
- Supports java annotations.
- Each test case can be implemented by one method.
- One java class can implement multiple test cases.
- Easy runtime configuration.
- Support multi-thread then can achieve parallel test.
- Java code and test data are separated using testng.xml or data provider.
- Support Java Object-Oriented features.
- Support group test. You can divide test cases into different groups in the testng.xml configuration file and just run the specified group at runtime. Such as smoke, acceptance and regression test, etc.
- You can configure and change the test methods’ order, dependence at runtime.
2. Steps To Write The First TestNG Example.
- Make sure your installed Jdk version is higher than 5.0. Run ‘java -version‘ in the command line and make sure the result is something like ‘java version “1.7.0” ‘
- Setup JAVA_HOME environment variable.
- You can refer to How To Install Jdk to learn how to correctly do step1 and step2 if you do not know.
- Go to http://testng.org/doc/download.html to get the latest library jars.
- You had better install the TestNG library using maven. So you can read the below articles first if you do not know how to do that. How To Create Java Project With Maven, How To Build And Run Java Project With Maven, How To Manage Maven Project Using Eclipse.
- After executing the maven command, you can find the testng-6.11.jar in your local maven repository folder such as C:\WorkSpace\MvnRepository\org\testng\testng\6.11
- Create folder C:\WorkSpace\TestNGExample.
- Write your example file TestNGExample.java. Save the file in the above folder.
/* Import Test annotation. */ import org.testng.annotations.Test; /* Import Assert util class.*/ import org.testng.Assert; public class TestNGExample { @Test /* Above annotation will tell JVM below method should be executed at runtime. */ public void testNGExample() { String str = "Hello Jerry, welcome to Selenium World."; Assert.assertEquals("Hello Jerry, welcome to Selenium World.", str); } }
- Create testng.xml. Save it in the same folder as the above file.
<suite name="Example Suite"> <test name="First Test"> <classes> <class name="TestNGExample"/> </classes> </test> </suite>
- Open the command window, run the below command to generate the class file.
cd C:\WorkSpace\TestNGExample javac -cp C:\WorkSpace\MvnRepository\org\testng\testng\6.11\testng-6.11.jar TestNGExample.java
- Execute the below command to run the example.
java -cp C:\WorkSpace\MvnRepository\org\testng\testng\6.11\testng-6.11.jar;C:\WorkSpace\MvnRepository\com\beust\jcommander\1.64\jcommander-1.64.jar;./ org.testng.TestNG ./testng.xml
- Please note the -cp parameter’s value. -cp is the abbreviation of the classpath, it tells JVM to find the classes in the after jar file or file path. In our example, there has 3 classpath values.
1) C:\WorkSpace\MvnRepository\org\testng\testng\6.11\testng-6.11.jar : This is where the TestNG related classes exist.
2) C:\WorkSpace\MvnRepository\com\beust\jcommander\1.64\jcommander-1.64.jar : This is where the helper class exist.
3) ./ : This is where the test class ( TestNGExample ) exist. - After execute you will find a test-output folder under C:\WorkSpace\TestNGExample.
- Click the index.html file in the above folder, the report page will be displayed.
- The report page left panel is the navigation menu list. Click each link in the left menu, the right panel will show detailed report information.