Introduction and Installation - rohitkrbhardwaj09/TestNG_ByRSA GitHub Wiki
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.
-
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.
-
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.
-
Open Eclipse.
-
Navigate to:
Help
>Eclipse Marketplace
. -
In the Find search box, type
TestNG
and press Enter. -
Locate "TestNG for Eclipse" from the results.
-
Click Install.
-
Follow the on-screen instructions: Click Next > Accept the agreement > Click Finish.
-
Installation will begin (progress will be shown).
-
If prompted with a warning, click Install Anyway.
-
Once done, Eclipse will prompt to restart. Click Restart Now.
-
Go to:
Window
>Preferences
. -
In the Type Filter Text box, type
TestNG
. -
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.
-
Java programs typically require a
public static void main(String[] args)
method. -
Without this method, Java applications won't compile or run.
-
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.
-
Create a New Java Project:
-
Example:
TestNGTutorial
-
-
Create a Package and Class:
-
Example package:
test
-
Example class:
ClassOne
-
-
Write a Test Method:
import org.testng.annotations.Test;
public class ClassOne {
@Test
public void demo() {
System.out.println("hello");
}
}
-
Add TestNG Library:
-
Hover over
@Test
annotation. -
Select "Add TestNG library to build path".
-
Import:
import org.testng.annotations.Test;
-
-
Run the Test:
-
Right-click the class >
Run As
>TestNG Test
-
Output will include:
-
Test result summary
-
Method name and pass/fail status
-
-
-
Clean and structured output.
-
No need for boilerplate
main
method. -
Enables better integration with automation frameworks.
-
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.
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");
}
}
package com.testngdemo;
import org.testng.annotations.Test;
public class Day2 {
@Test
public void Demo3() {
System.out.println("Good");
}
}
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");
}
}
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");
}
}
<?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>
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 |
-
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.