Introduction and Installation - rohitkrbhardwaj09/TestNG_ByRSA GitHub Wiki

TestNG - Introduction and Installation

Why TestNG?

TestNG (Test Next Generation) is a powerful testing framework inspired by JUnit and NUnit but introduces new functionalities to make test automation more efficient and flexible.

Importance in Framework Development:

  • TestNG is a key component when building a framework from scratch.

  • It provides complete control over test case execution.

  • Helps in grouping, prioritizing, and managing test cases efficiently.

  • Enables parameterization, data-driven testing, and dependent test execution.

  • It is often referred to as a testing framework because of its wide range of features.

Key Features:

  • Define pre-requisite test cases using dependencies.

  • Selectively run specific test groups (e.g., only smoke tests out of 500 total).

  • Organize and group test cases easily.

  • Execute tests in desired order and configure pre/post conditions.

  • Run a test with multiple sets of data via parameterization.

  • Integration with external data sources for test inputs.


Installation and Configuration in Eclipse

Step-by-Step Guide to Install TestNG Plugin:

  1. Open Eclipse.

  2. Navigate to: Help > Eclipse Marketplace.

  3. In the Find search box, type TestNG and press Enter.

  4. Locate "TestNG for Eclipse" from the results.

  5. Click Install.

  6. Follow the on-screen instructions: Click Next > Accept the agreement > Click Finish.

  7. Installation will begin (progress will be shown).

  8. If prompted with a warning, click Install Anyway.

  9. Once done, Eclipse will prompt to restart. Click Restart Now.

Verifying Installation:

  1. Go to: Window > Preferences.

  2. In the Type Filter Text box, type TestNG.

  3. If TestNG appears in the list, the plugin is installed successfully.

If TestNG is not visible after restarting, the installation may have failed. Repeat the steps to reinstall.


Running Test Cases without Java Compiler (Using TestNG)

Traditional Java Execution:

  • Java programs typically require a public static void main(String[] args) method.

  • Without this method, Java applications won't compile or run.

TestNG Approach:

  • In TestNG, you don’t need a main method to execute tests.

  • Test methods are executed using the @Test annotation.

  • TestNG acts like a test runner that internally uses Java but simplifies test execution.

Steps to Run a TestNG Test Case:

  1. Create a New Java Project:

    • Example: TestNGTutorial

  2. Create a Package and Class:

    • Example package: test

    • Example class: ClassOne

  3. Write a Test Method:

import org.testng.annotations.Test;

public class ClassOne {

@Test
public void demo() {
    System.out.println("hello");
}

}

  1. Add TestNG Library:

    • Hover over @Test annotation.

    • Select "Add TestNG library to build path".

    • Import: import org.testng.annotations.Test;

  2. Run the Test:

    • Right-click the class > Run As > TestNG Test

    • Output will include:

      • Test result summary

      • Method name and pass/fail status

Advantages:

  • Clean and structured output.

  • No need for boilerplate main method.

  • Enables better integration with automation frameworks.


Multiple Test Cases in a Single Class

Key Concepts:

  • You can define multiple @Test methods in a single class.

  • Each method annotated with @Test is treated as a separate test case.

  • This avoids creating multiple Java classes for separate tests.

Example (Day1.java - Personal Loan Module):

package com.testngdemo;

import org.testng.annotations.Test;

public class Day1 {

@Test
public void Demo() {
    System.out.println("Hello");
}

@Test
public void Demo2() {
    System.out.println("Bye");
}

}


Day2.java Example (Personal Loan Test):

package com.testngdemo;

import org.testng.annotations.Test;

public class Day2 {

@Test
public void Demo3() {
    System.out.println("Good");
}

}


Day3.java Example (Car Loan Module):

package com.testngdemo;

import org.testng.annotations.Test;

public class Day3 {

@Test
public void WebLoginCarLoan() {
    // selenium
    System.out.println("Web Login Car Loan");
}

@Test
public void MobileLoginCarLoan() {
    // appium
    System.out.println("Mobile Login Car Loan");
}

@Test
public void LoginAPICarLoan() {
    // Rest API
    System.out.println("Rest API Car Loan");
}

}


Day4.java Example (Home Loan Module):

package com.testngdemo;

import org.testng.annotations.Test;

public class Day4 {

@Test
public void WebLoginHomeLoan() {
    // selenium
    System.out.println("Web Login Home Loan");
}

@Test
public void MobileLoginHomeLoan() {
    // appium
    System.out.println("Mobile Login Home Loan");
}

@Test
public void LoginAPIHomeLoan() {
    // Rest API
    System.out.println("Rest API Home Loan");
}

}


testng.xml (Include & Exclude Methods by Scenario)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">

<!-- Scenario: Run Personal Loan module but exclude 'Demo2' from Day1 and run only 'LoginAPIHomeLoan' in Day4 --> <test thread-count="5" name="Personal Loan"> <classes> <class name="com.testngdemo.Day1"> <methods> <exclude name="Demo2"/> </methods> </class> <class name="com.testngdemo.Day2"/> <class name="com.testngdemo.Day4"> <methods> <include name="LoginAPIHomeLoan"/> </methods> </class> </classes> </test>

<!-- Scenario: Run Car Loan module but exclude 'MobileLoginCarLoan' from execution --> <test name="Car Loan"> <classes> <class name="com.testngdemo.Day3"> <methods> <exclude name="MobileLoginCarLoan"/> </methods> </class> </classes> </test>

</suite>


Benefits of Using Include/Exclude Mechanism

Use Cases & Their Solutions:

Scenario Objective XML Strategy
Exclude a test method Client doesn't want "MobileLoginCarLoan" Use inside tag
Include only one method Client wants only "LoginAPIHomeLoan" to run Use tag, skip others
Skip 98 of 100 tests Avoid listing 98 excludes Use for required 2 only

Best Practices:

  • Use <exclude> for 1–2 exclusions.

  • Use <include> when most tests need to be skipped.

  • Avoid cluttering XML with redundant includes/excludes.

  • Create separate XMLs for different test suites (modules).


Next Step: Learn how to group tests using @Test(groups=...) and control execution using XML-defined groups.

⚠️ **GitHub.com Fallback** ⚠️