0% found this document useful (0 votes)
5 views5 pages

TestNG Suite FileDemo

The document outlines the creation of a basic test suite using TestNG, including multiple test classes for login and dashboard functionalities, as well as a payment process with dependencies. It also demonstrates the use of lifecycle annotations such as @BeforeClass and @AfterClass to manage setup and teardown processes for tests. Expected outputs for each test scenario are provided to illustrate the results of the executed tests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

TestNG Suite FileDemo

The document outlines the creation of a basic test suite using TestNG, including multiple test classes for login and dashboard functionalities, as well as a payment process with dependencies. It also demonstrates the use of lifecycle annotations such as @BeforeClass and @AfterClass to manage setup and teardown processes for tests. Expected outputs for each test scenario are provided to illustrate the results of the executed tests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

🎯 1.

Create a Basic Test Suite with Multiple Test Classes

🔧 Step 1: Test Class - LoginTest.java

import org.testng.annotations.Test;

public class LoginTest {

@Test

public void validLogin() {


System.out.println("Login successful with valid credentials.");
}

@Test

public void invalidLogin() {


System.out.println("Login failed with invalid credentials.");
}
}

🔧 Step 2: Test Class - DashboardTest.java

import org.testng.annotations.Test;

public class DashboardTest {

@Test

public void viewDashboard() {


System.out.println("Dashboard viewed successfully.");
}

@Test

public void logout() {


System.out.println("Logout successful.");
}
}
📄 Step 3: testng.xml (Test Suite Configuration)

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite name="BasicTestSuite">
<test name="LoginTests">
<classes>
<class name="LoginTest"/>
</classes>
</test>

<test name="DashboardTests">
<classes>
<class name="DashboardTest"/>
</classes>
</test>
</suite>

💡 Expected Output:

Login successful with valid credentials.


Login failed with invalid credentials.
Dashboard viewed successfully.
Logout successful.

🧩 2. Dependency Management in Tests

🔧 Test Class - PaymentTest.java

import org.testng.annotations.Test;

public class PaymentTest {

@Test

public void addPaymentDetails() {


System.out.println("Payment details added.");
}

@Test(dependsOnMethods = {"addPaymentDetails"})

public void makePayment() {


System.out.println("Payment completed successfully.");
}

@Test(dependsOnMethods = {"makePayment"})

public void confirmOrder() {


System.out.println("Order confirmed after successful payment.");
}
}

📄 testng.xml (Test Suite for Dependencies)

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite name="PaymentSuite">
<test name="PaymentTests">
<classes>
<class name="PaymentTest"/>
</classes>
</test>
</suite>

💡 Expected Output (Tests follow the dependency chain):

Payment details added.


Payment completed successfully.
Order confirmed after successful payment.

3. Using @BeforeClass, @AfterClass, @BeforeMethod, and @AfterMethod

Set up pre-conditions and post-conditions for your tests.

🔧 Test Class - LifecycleTest.java

import org.testng.annotations.*;

public class LifecycleTest {


@BeforeClass
public void beforeClass() {
System.out.println("Before Class: Setup database connection.");
}

@AfterClass
public void afterClass() {
System.out.println("After Class: Close database connection.");
}

@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method: Open browser.");
}

@AfterMethod
public void afterMethod() {
System.out.println("After Method: Close browser.");
}

@Test

public void test1() {


System.out.println("Executing Test 1.");
}

@Test

public void test2() {


System.out.println("Executing Test 2.");
}
}

📄 testng.xml

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite name="LifecycleSuite">
<test name="LifecycleTestExecution">
<classes>
<class name="LifecycleTest"/>
</classes>
</test>
</suite>
💡 Expected Output:

Before Class: Setup database connection.


Before Method: Open browser.
Executing Test 1.
After Method: Close browser.
Before Method: Open browser.
Executing Test 2.
After Method: Close browser.
After Class: Close database connection.

You might also like