index-1-1.jpg
  • Aurora Solutions

TestNG: Adding Packages, Classes and Methods to test

Today, we are going to start a new series of articles on TestNG. TestNG, where NG stands for new generation, is a test automation framework inspired by JUnit and NUnit. TestNG can be used for unit, functional,integration and end to end testing. It is written in Java and it can be used in Java as well as Java related languages such as groovy.


TestNG provides multiple options for running our test cases. In this article we will focus on that how we can add packages, classes and methods to test. We will go through them one by one. Before going ahead with creation of test with classes packages and methods, we will need a sample project for defining test suites for execution.


Sample Project: a. Open Eclipse. b. create a project MutiTest . c. In MultiTest create a package firstpackage. d. In firstpackage create a class FirstTestClass.java Add the following piece of code in FirstTestClass.java:

public class FirstTestClass {

private final String firstTest = “My First Test”;
private final String secondTest = “My Second Test”;

@Test
public void firstTest(){
   System.out.println(firstTest);
   assertEquals("My First Test", firstTest);
}

@Test
public void secondTest(){
   System.out.println(secondTest);
   assertEquals("My Second Test", secondTest);
 }
}

1. Packages: Using 'Package' configuration option TestNG will execute all the test inside specified Package. In this section we will learn how to add packages to the test suite.

1. Open the package “firstPackage” in “MultiTest” peoject. 2. Add new file TestNG configuration XML by name “testing.xml”to the project. 3. Add the following content to the testng.xml file:

<suite name="Package Suite" verbose="1">
  <test name="Package Test">
     <packages>
           <package name="test.firstpackage" />
    </packages>
  </test>
</suite>

Output: Running the proceeding xml file will give output:

                                “My First Test”
                                “My Second Test”

Now we have successfully executed TestNG test Suite by adding test packages to the suite.


2. Class: Using 'Class' configuration option TestNG enables us to run only specified class name along with its package name. In this section we will learn how to add specified class/classes to the test suite.

1. Open the package “firstPackage” in “MultiTest” peoject. 2. Add new file TestNG configuration XML by name “testing.xml”to the project. 3. Add the following content to the testng.xml file:

<suite name="Class Suite" verbose="1">
    <test name="Test">
       <classes>
             <class name="test.firstpackage.FirstTestClass" />
       </classes>
    </test>
</suite>

Output: running the proceeding Xml file will output:

                                        “My First Test”
                                        “My Second Test”

Now we have successfully executed TestNG test Suite by adding class to a test. Similarly, we can add multiple classes by adding required number of Class tags in xml file.

3. Method: Using this option option TestNG enables us to run only specified method in a test class.

1. Open the package “firstPackage” in “MultiTest” peoject. 2. Add new file TestNG configuration XML by name “testing.xml”to the project. 3. Add the following content to the testng.xml file:

<suite name="Method Suite" verbose="1">
       <test name="Method Test">
           <classes>
             <class name="test.firstpackage.FirstTestClass">
              <methods>
                 <include name="secondTest" />
              </methods>
             </class>
          </classes>
       </test>
</suite>

Output: running the proceeding Xml file will output:

                                  “My Second Test”

Now we have successfully executed TestNG test Suite by adding method to a test.

0 views0 comments

Recent Posts

See All

Aurora Holds Tech Talk At FAST-NU

Industrial seminars and workshops have always been fruitful for academia to understand the importance of specific technology and market trends of what is new and running. Such sessions provide advanta

Facebook Authentication for Spring Security in Grails

image In this article we will use Spring Security Facebook Plugin for Grails to integrate our grails application with facebook. By the end of this article you will be able to login in your grails web