Showing posts with label Assertion in TestNG. Show all posts
Showing posts with label Assertion in TestNG. Show all posts

Asserts in TestNG

  



What Are TestNG Asserts?


An Assertion in TestNG is a mechanism used to validate the expected outcome of a test case. They are crucial for determining whether a test has passed or failed. When an assertion fails, TestNG marks the test method as a failure and usually stops its execution.

TestNG relies on the standard org.testng.Assert class, which provides a variety of static methods for making assertions. It is used to validate test outcomes by comparing the actual result with the expected result. They help in automating validations within test methods. If an assertion fails, TestNG will mark the test as failed and stop execution (for hard asserts).


Category in TestNG Assertions

TestNG primarily supports two types of assertions:

1. Hard Assert (Default) 

This is the default type of assertion used when you call methods from the org.testng.Assert class directly.
  • Behavior: If a Hard Assert fails, TestNG immediately throws an AssertionError, marking the test method as failed and stopping the execution of the remaining code within that test method.
  • Use Case: Ideal when subsequent test steps depend on the successful completion of the current step.
2. Soft Assert

Soft Assertions allow the test execution to continue even after an assertion fails.
  • Behavior: It does not throw an AssertionError immediately. Instead, it collects all assertion failures during the test method execution. The test is marked as failed only when the assertAll() method is called.
  • Implementation: You must use the org.testng.asserts.SoftAssert class.
  • Use Case: Useful when you need to perform multiple checks on a single page or component and want to see all failures in one go, without stopping the test execution at the first failure.










Types of Asserts in TestNG

Assertion TypeDescription
assertEquals()Checks if two values are equal
assertNotEquals()Checks if two values are not equal
assertTrue()Checks if a condition is true
assertFalse()Checks if a condition is false
assertNull()Checks if an object is null
assertNotNull()Checks if an object is not null
fail()Fails the test with a custom message (useful to explicitly fail a test)



All assert methods are available in org.testng.Assert class:


import org.testng.Assert;




Example Java Code for TestNG Asserts

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

public class AssertExampleTest {

    @Test
    public void testAssertEquals() {
        String expected = "TestNG";
        String actual = "TestNG";
        Assert.assertEquals(actual, expected, "Strings are not equal!");
    }

    @Test
    public void testAssertNotEquals() {
        int actual = 5;
        int expected = 10;
        Assert.assertNotEquals(actual, expected, "Values should not be equal");
    }

    @Test
    public void testAssertTrue() {
        boolean condition = (10 > 5);
        Assert.assertTrue(condition, "Condition is not true");
    }

    @Test
    public void testAssertFalse() {
        boolean condition = (10 < 5);
        Assert.assertFalse(condition, "Condition is not false");
    }

    @Test
    public void testAssertNull() {
        String str = null;
        Assert.assertNull(str, "Object is not null");
    }

    @Test
    public void testAssertNotNull() {
        String str = "Hello";
        Assert.assertNotNull(str, "Object is null");
    }

    @Test
    public void testFail() {
        // Use fail to force a failure
        Assert.fail("This test is failed intentionally");
    }
}


Code Explanation

Above is the TestNG class where there are 7 test methods, each method has different assertion. assertion will return response as per assertion will be passed or failed.


Important Points

  • Assertions are mostly used in unit testing and validation.
  • If an assertion fails, the remaining code inside the test method is skipped.
  • TestNG assertions help make tests deterministic and reliable.



Suggested Posts:

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