TestNG First Example

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.

  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 are 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 the 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’ order, dependence at runtime.

2. Steps To Write The First TestNG Example.

  1. 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”
  2. Setup JAVA_HOME environment variable.
  3. You can refer to 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.
  5. 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.
  6. 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
  7. Create folder C:\WorkSpace\TestNGExample.
  8. 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 {
      /* 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);
  9. Create testng.xml. Save it in the same folder as the above file.
    <suite name="Example Suite">
      <test name="First Test">
           <class name="TestNGExample"/>
  10. 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
  11. 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
  12. 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.
  13. After execute you will find a test-output folder under C:\WorkSpace\TestNGExample.
  14. Click the index.html file in the above folder, the report page will be displayed.
  15. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

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