top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Basics of TestNG with IntelliJ IDEA

Introduction of TestNG:

  • TestNG is a testing framework inspired by JUnit and NUnit. It contains some new features that make it more powerful and easier to use.

  • TestNG is designed to cover testing categories like unit, functional, integration, end-to-end, etc.


Advantages of TestNG:

  • Manages test suites and test cases

  • Helps in prioritizing test cases

  • Helps in a grouping of tests

  • Parallel execution

  • Reporting


Download and install IntelliJIDEA:



Steps to create a project:

  1. Open IntelliJ IDEA

  2. Create a new Java Project

  3. Download the TestNG dependency .jar file from the MVN Repository website

  4. Save the TestNG dependency .jar file into the driver folder under the C drive

  5. Select your project in IntelliJ click on File, go to Project Structure

  6. Under Project Structure click on Modules, go to the Dependencies tab, click on the + button

  7. Select Jars and Directories and select a path of the TestNG.jar file click Ok

  8. Click on added testng.jar file select compile, Click on Apply, and Ok.

  9. Now TestNG.jar has been added to your project's external libraries.




Now create a sample test case:

  1. Right-click on the src folder to create a new Java Class named FirstTestCase

  2. Add needed dependencies,


    1. slf4j simple

    2. slf4j API

    3. jcommander


import org.testng.reporters.XMLConstants;
public class FirstTestCase {    
@Test(priority = 1)    
void setup(){ 
   System.out.println("This is setup test");    
}    
@Test(priority = 2)   
 void login(){
        System.out.println("This is login test");    
}    
@Test(priority = 3)    
void tearDown(){        
System.out.println("This is tearDown test");    
}
}

  • TestNG Project does not have a main method it executes based on the priority of the methods

  • TestNG test cases execute with the @Test annotation

  • By default they are executed in alphabetical order, if you want to execute them in any particular order then you need to give priority to each test case




Create one more test case for upcoming topics:

import org.testng.annotations.Test;
public class SecondTestCase {
    @Test(priority = 1)
    void setup() {
        System.out.println("Opening Browser");
    }
    @Test(priority = 3)
    void searchCustomer() {
        System.out.println("This is searchCustomer test");
    }
    @Test(priority = 2)
    void addCustomer() {
        System.out.println("This is addCustomer test");
    }
    @Test(priority = 4)
    void tearDown() {
        System.out.println("Closing Browser");
    }
}

Steps to convert your project into testng and Run through testng.xml: 

  1. file-->Settings-->Pluggins--> Search Create TestNG XML--> Install-->Accept-->Ok

  2. Restart IntelliJIDEA

  3. After installation of the plugin, you can see the Creat testNG XML option if you right-click on your project and select that it will show you the path of the testng.xml file

  4. Refresh and Open testng.xml --> Right click on project -->reformat code

  5. Under <test> tag --> add <classes> tag --> create <class>

  6. In the class tag, you need to add the test case names that you want to execute

(At a time multiple test cases can be executed with testng.xml)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="C:/Users/sagar/IdeaProjects/Demo">
        <classes>
            <class name="FirstTestCase"/>
            <class name="SecondTestCase"/>
        </classes>
    </test>
</suite>

:testng.xml


Save testng.xml, Rightclick and select run testng.xml




@Note: To see the testng report in IntelliJIDEA you need to do some configurations,

  • Select the project folder --> right-click on the Run menu button from the top --> edit configurations -->


Select the testng.xml file --> go to Listeners --> click on + button --> search and select org.testng.reporters.EmailableReporter2 and org.testng.reporters.FailedReporter --> Apply --> Ok--> select right-click on project --> Reload from disc --> Run testng.xml --> your testng report is generated under the test-output folder


Annotations in TestNG:

  • An annotation is a tag that provides additional information about the class or method. It is represented by the ‘@’ prefix.

  • TestNG uses these annotations to help in making a robust framework.

  • With the TestNG Annotations, we can control the sequence and priority of the methods which allows us to execute Java code before and after a certain point

  • Annotations are placed over the methods with the symbol @.

  • All methods of test cases must be tagged with @Test annotation


Workflow of annotations:

To understand the detailed flow of the annotations follow the example given below,


Example: TC1.java

import org.testng.annotations.*;
public class TC1 {
    @BeforeClass
    void beforeClass(){
        System.out.println("This will execute before the class...");
    }
    @AfterClass
    void afterClass(){
        System.out.println("This will execute After the class...");
    }
    @BeforeMethod
    void beforeMethod(){
        System.out.println("This will execute before each method...");
    }
    @AfterMethod
    void afterMethod(){
        System.out.println("This will execute After each method...");
    }
    @Test
    void test1(){
        System.out.println("This is test1...");
    }
    @Test
    void test2(){
        System.out.println("This is test2...");
    }
    @BeforeTest
    void beforeTest(){
        System.out.println("This will execute before the Test...");
    }
    @AfterTest
    void afterTest(){
        System.out.println("This will execute after the Test...");
    }
}

TC2.java


import org.testng.annotations.*;
public class TC2 {
    @BeforeClass
    void beforeClass(){
        System.out.println("This will execute before the class...");
    }
    @AfterClass
    void afterClass(){
        System.out.println("This will execute After the class...");
    }
    @BeforeMethod
    void beforeMethod(){
        System.out.println("This will execute before each method...");
    }
    @AfterMethod
    void afterMethod(){
        System.out.println("This will execute After each method...");
    }
    @Test
    void test3(){
        System.out.println("This is test3...");
    }
    @Test
    void test4(){
        System.out.println("This is test4...");
    }
    @BeforeSuite
    void beforeSuite(){ System.out.println("This will execute Before the Test Suite..."); }
    @AfterSuite
    void afterSuite(){ System.out.println("This will execute After the Test Suite..."); }
}

testng.xml:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test name="annotationdemo">
        <classes>
            <class name="TC1">
                <methods>
                    <include name="test1"/>
                    <include name="test2"/>
                </methods>
            </class>
            <class name="TC2">
                <methods>
                    <include name="test3"/>
                    <include name="test4"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>


22 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page