TestNG First Example

TestNG is an automated test framework which can be used to make all kinds of test includes unit, integration and functional test etc. It is inspired by JUnit but more easier and powerful than JUnit. It require 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 developer and QA engineer.

TestNG ( Test Next Generation) Benefits

  1. Open source and totally free.
  2. Supports java annotations.
  3. Each test case can be implemented by one method.
  4. One java class can implement multiple test cases.
  5. Easy runtime configuration.
  6. Support multi-thread then can achieve parallel test.
  7. Java code and test data is separated using testng.xml or data provider.
  8. Support Java Object Oriented features.
  9. Support group test. You can divide test cases into different groups in testng.xml configuration file and just run the specified group at runtime. Such as smoke, acceptance and regression test etc.
  10. You can configure and change the test methods’s order, dependence at runtime.

Steps To Write The First TestNG Example

  1. Make sure your installed Jdk version is higher than 5.0.
    Run ‘java -version’ in command line and make sure the result is something like below ‘java version “1.7.0” ‘
  2. Setup JAVA_HOME environment variable.
  3. You can refer How To Install Jdk to learn how to correctly do step1 and step2 if you do not know.
  4. Go to http://testng.org/doc/download.html to get the latest library jars.
    testng maven repository and dependency xml element
    From above picture, you can see that you should install it using maven. So you had better read 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
  5. After execute 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
    testng jar file in maven repository folder
  6. Create folder C:\WorkSpace\TestNGExample.
  7. Write your example file TestNGExample.java. Save the file in above folder.
    /* Import Test annotation. */
    import org.testng.annotations.Test;
    /* Import Assert util class.*/
    import org.testng.Assert;
    public class TestNGExample {
      /* 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);
  8. Create testng.xml. Save it in same folder with above file.
    <suite name="Example Suite">
      <test name="First Test">
           <class name="TestNGExample"/>
  9. Open command window, run 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

    compile testng example java file
    example java class file

  10. Execute 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 class path, it tells JVM to find the classes in the after jar file or file path. In our example, there has 3 class path value.
    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.

    You can see below output when the execution complete.
    example test output

  11. After execute you will find a test-output folder under C:\WorkSpace\TestNGExample.
    test output folder
  12. Click the index.html file in above folder, the report page will be displayed. The left panel is navigation menu list. Click each link in left menu, right panel will show detail report information.
    testng report page

[download id=”1633″]

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x