Showing posts with label TestNG Examples. Show all posts
Showing posts with label TestNG Examples. Show all posts

TestNG Reporter Log

  



What is Reporter.log in TestNG?

Reporter.log() is a method provided by TestNG to log messages during test execution. These logs are then displayed in the TestNG report, particularly in emailable-report.html and index.html. This is useful for debugging and tracking what happens during a test.


Key purpose of Reporter Logs

1. Persistent Logging for Reports

The primary purpose of the Reporter Log is to provide persisting, method-specific log statements that can be viewed later in the test execution reports. Unlike temporary console output, these logs become a permanent part of the test result documentation.


2. Traceability and Debugging

Reporter logs significantly enhance traceability and debugging. By strategically placing Reporter.log() calls throughout a test method, a tester can record key actions, variable values, or intermediate statuses. If a test fails, these logs provide the necessary context and detailed step-by-step information to diagnose the failure without needing to rerun the test or step through the code manually.


3. Separation from Console Output

The Reporter Log offers a distinct advantage over using standard System.out.println():

  • System.out.println() messages go to the console (standard output), which is often cleared or lost in large test suites and is not included in the default TestNG HTML reports.
  • Reporter.log() messages are captured by the TestNG framework and stored, ensuring they appear specifically in the "Reporter Output" section of the index.html and emailable-report.html.


4. Dual Output Capability

While its main role is reporting, the Reporter class is flexible. It allows an optional boolean parameter in its log() method to direct the message to both the report and the standard console output simultaneously. This gives the user control over where the log message is visible.


5. Log Association

TestNG is designed to associate each log entry with the specific test method or configuration method that generated it. This organization makes the reports structured and easy to read, ensuring logs from one test don't clutter the output of another.













Key Features of Reporter.log():


FeatureDescription
Output locationLogs are printed in TestNG's HTML reports (emailable-report.htmlindex.html) and console if configured.
SyntaxReporter.log(String message)
Overloaded methodReporter.log(String messageboolean logToStandardOutput) where second param lets you print to the console.
Use casesDebugging, logging checkpoints, recording data inputs/outputs, etc.



Basic Syntax:

Reporter.log("Your log message here");




Example Java Code Using Reporter.log()

import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class ReporterLogExample {

    @Test
    public void testLogin() {
        Reporter.log("Opening browser", true);
        Reporter.log("Navigating to login page", true);
        
        String actualTitle = "Login - MyApp";
        String expectedTitle = "Login - MyApp";
        
        Reporter.log("Verifying page title", true);
        Assert.assertEquals(actualTitle, expectedTitle, "Page title doesn't match");

        Reporter.log("Login test passed", true);
    }

    @Test
    public void testSearch() {
        Reporter.log("Starting search test", true);

        String keyword = "TestNG";
        Reporter.log("Searching for keyword: " + keyword, true);
        
        // Dummy output
        boolean searchSuccess = true;

        Reporter.log("Search operation completed", true);
        Assert.assertTrue(searchSuccess, "Search failed");

        Reporter.log("Search test passed", true);
    }
}


Code Explanation:


In the class, two test methods are there, in each method: Reporter.log("message", true) logs message to:

  • TestNG report

  • Console output

  • true as the second argument ensures it prints to the console during execution.
    If omitted or set to false, the message will only appear in the report.


Output Location:

After running the test, you’ll find logs in:

Console output when true is passed that TestNG Report Files:

  • test-output/emailable-report.html
  • test-output/index.html


At a glance:

  • Reporter.log() is a handy tool for tracking and debugging in TestNG tests.
  • It helps you monitor steps, values, and flow in reports.
  • Use the second parameter as true to see logs on the console during execution.



Suggested Posts:

1. Read Excel Data by Data provider
2. Annotations in TestNG
3. TestNG Asserts
4. Priority in TestNG
5. Parameterization in TestNG

TestNG Suite




Creating a TestNG Test Suite allows you to group and run multiple test classes, groups, or methods together using an XML configuration file. This is very useful for organizing your tests logically and controlling test execution.













What is a TestNG Test Suite?


The TestNG Suite centers on its role as the highest organizational container for an entire test execution run. It is the control hub that defines the scope, configuration, and execution strategy for all the individual tests beneath it.


1. The Role as the Top Container 

The TestNG Suite is defined by the root element in the testng.xml configuration file.

  • Logical Grouping: Theoretically, the Suite groups one or more Tests (which are themselves groupings of test classes). It allows you to run a complete, predefined set of tests as a single unit. For instance, an organization might define one Suite for "Daily Smoke Tests" and another for "Full Regression Testing."
  • Single Execution Cycle: A Suite defines a single, complete execution cycle. All setup actions defined at the Suite level (@BeforeSuite) run once at the very beginning, and all cleanup actions (@AfterSuite) run once at the very end, regardless of how many individual tests or classes are executed within it.

2. Configuration and Parameter Control

The Suite provides the highest level of external configuration control.

  • Global Parameters: The Suite level is where you define parameters that are meant to be shared across all the tests, classes, and methods within that Suite. This is ideal for global settings like a base URL, a shared database connection string, or a target environment (e.g., "production" or "staging").

  • Execution Strategy: It dictates how the tests within it will be run. For example, the Suite defines whether the tests should run sequentially (one after the other) or in parallel (concurrently). This is crucial for managing test execution time and efficiency.


3. Reporting Scope

The Suite defines the boundary for the overall reporting mechanism.

  • Consolidated Report: When a Suite completes its run, it generates a single, consolidated report detailing the status (pass, fail, skip) of every Test, every class, and every method executed under its definition. This provides a holistic view of the system's quality status from that specific run.


Test Suite is defined in an XML file generally named as 'testng.xml'. It can:

  • Include multiple test classes

  • Run specific methods or groups

  • Control execution order

  • Pass parameters to tests


Below are the steps to create a TestNG Suite


Step 1: Create Your Test Classes


LoginTest.java

Below is the LoginTest class having two tests annotated with @Test and when Test get executed, it prints a statement on console.

import org.testng.annotations.Test;

public class LoginTest {

    @Test
    public void loginWithValidUser() {
        System.out.println("Login with valid user executed.");
    }

    @Test
    public void loginWithInvalidUser() {
        System.out.println("Login with invalid user executed.");
    }
}



HomePageTest.java

Below is the HomePageTest class having two tests annotated with @Test and when Test get executed, it prints a statement on console.

import org.testng.annotations.Test;

public class HomePageTest {

    @Test
    public void checkTitle() {
        System.out.println("Home page title check executed.");
    }

    @Test
    public void checkProfileButton() {
        System.out.println("Profile button check executed.");
    }
}



Step 2: Create the testng.xml Test Suite File

Below is Testng.xml file having name of bot Test classes in class tag. each class tag tag is in classes tag which is in test tag. All the tests are in suite tag.

Tags Hierarchy of below Testng.xml file: suite > test > classes > class

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

    <test name="Login Tests">
        <classes>
            <class name="LoginTest"/>
        </classes>
    </test>

    <test name="Home Page Tests">
        <classes>
            <class name="HomePageTest"/>
        </classes>
    </test>

</suite>


Save testng.xml file in the root of your project (or src/test/resources/ for Maven).

Step 3: Run the Test Suite


In Eclipse or IntelliJ:
  • Right-click on testng.xml
  • Select Run As > TestNG Suite


Suggested Posts:

1. Priority in TestNG
2. TestNG Annotations
3. TestNG Asserts
4. Parameterization in TestNG
5. Read Excel Data by Data provider