Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 16, 2016

TestNG Assertions





TestNG Assertions will discuss about the Assertions available in the testNG in detail. When you write a test script there should be a verification point in order to mark that test script is passed or failed or any other state. To achieve this in testNG we will user Assertions. Assertion helps you to check or verify the success of conditions in your test. If the conditions don’t satisfy, it will stop the

test execution of the said test and mark it as failing.

TestNG supports assertion of a test using the Assert class which is part of the testNG library. The following table describes few of the methods and their usage that are available with the Assert class in testng:

Frequently Used Assertions:

AssertionDescription


Assert.assertTrue(condition, message)


It will check the condition is true or not. If the condition is false then it fails and prints the message. Here the message is optional.
Assert.assertFalse(condition, message)It will check the condition is false or not.If the condition is true then it fails and prints the message. Here the message is optional.


Assert.assertNotNull(object, message)
It will check the object is NOT Null or not. If the object is null then it fails and prints the message. Here the message is optional.


Assert.assertNull(object, message)
It will check the object is Null or not. If the object is NOT Null then it fails and prints the message. Here the message is optional.


Assert.assertEquals(actual, expected,message)


It will check the actual and expected or Equal or not. If both are NOT Equal then it fails and prints the message. Here the message is optional.


Assert.assertNotEquals(actual, expected,message)


It will check the actual and expected or NOT Equal or not. If both are Equal then it fails and prints the message. Here the message is optional.

In the above table we have seen some of the assertions which are very frequently used. If the condition meets as expected then the assert passed otherwise it will fails the test and stops the execution of that test.

Below is the sample program where all the assertions will PASS:

import org.testng.Assert;
import org.testng.annotations.Test;
public class PassedAssertions
{
    @Test
    public void testOne()
    {
        Assert.assertTrue(true, "Assert Failed as the actual condition is False");
    }
    @Test
    public void testTwo()
    {
        Assert.assertFalse(false, "Assert Failed as the actual condition is True");
    }
    
    @Test
    public void testThree()
    {
        String s = "Krishna";
        Assert.assertNotNull(s,"Assert Failed as the String is Null");
    }
    
    @Test
    public void testFour()
    {
        String s = null;
        Assert.assertNull(s, "Assert Failed as the String is NOT NULL");
    }
    
    @Test
    public void testFive()
    {
        String s1 = "Krishna";
        String s2 = "Krishna";
        Assert.assertEquals(s1, s2, "Assert Failed as the two string are NOT EQUAL");
    }
    
    @Test
    public void testSix()
    {
        String s1 = "Krishna";
        String s2 = "Sakinala";
        Assert.assertNotEquals(s1, s2, "Assert Failed as the two string are EQUAL");
    }
}

Below is the output for the above Program:

1

Below is the sample program where all the assertions will FAIL:

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


public class FailedAssertions
{
    @Test
    public void testOne()
    {
        Assert.assertTrue(false, "Assert Failed as the actual condition is False");
    }
    @Test
    public void testTwo()
    {
        Assert.assertFalse(true, "Assert Failed as the actual condition is True");
    }
    
    @Test
    public void testThree()
    {
        String s = null;
        Assert.assertNotNull(s,"Assert Failed as the String is Null");
    }
    
    @Test
    public void testFour()
    {
        String s = "Krishna";
        Assert.assertNull(s, "Assert Failed as the String is NOT NULL");
    }
    
    @Test
    public void testFive()
    {
        String s1 = "Krishna";
        String s2 = "Sakinala";
        Assert.assertEquals(s1, s2, "Assert Failed as the two string are NOT EQUAL");
    }
    
    @Test
    public void testSix()
    {
        String s1 = "Krishna";
        String s2 = "Krishna";
        Assert.assertNotEquals(s1, s2, "Assert Failed as the two string are EQUAL");
    }
}


Below is the output for the above Program:

2

This way we will use the Assertions in testNG to know the status of the tests which are executed.

Please watch the you tube video for better understanding.



Share this post: on Twitter on Facebook

Parallel Execution of Classes In TestNG Custom Logger in TestNG

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

PARALLEL EXECUTION OF classes

TestNG

Parallel Execution of Classes In TestNG

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025