The Wayback Machine - https://web.archive.org/web/20240914041748/https://www.geeksforgeeks.org/how-to-use-soft-asserts-in-testng/
Open In App

How to Use Soft Asserts in TestNG?

Last Updated : 28 Sep, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

TestNG is an open source automation framework that is used to perform software testing more easily, it provides various annotations and is used to perform parallel testing and produce the test reports. TestNG provides asserts to check the actual results with the expected results, Asserts are necessary to validate a test case, whether it is a pass or a fail, There are two types of asserts provided by TestNG, Hard assert and Soft Assert. Hard asserts stop the execution of code when the asserts fail.

Soft Asserts

Soft asserts are the asserts which continue the execution even after the Assert condition fails. Soft asserts are mainly used to verify certain test conditions in the code. It is used in a case when the passing of one test condition is not necessary to execute the upcoming tests. It is not included by default in TestNG, In order to use the soft assert, we have to import package from TestNG.

Creating Instance for Soft Assert:

softAssert softAssert = new SoftAssert();

After creating the instance for the soft assert, import the package

import org.testng.asserts.SoftAssert;

In the end, we have to call the assertAll() method that is used to throw the exceptions and results at the end of the test.

softAssert.assertAll();

Soft Assert also supports all the methods used by Hard Asserts such as,

  • AssertEquals
  • AssertTrue
  • AssertFalse
  • AssertNotEquals

Let’s see the usage and working of the SoftAssert by executing a test case,

  • Open the browser
  • Navigate to Geeksforgeeks Home page
  • Get the Title of the page
  • Check the title with the expected result using Assert.
  • Click on Java from the menu.

In this code, we are using the method assertEqual(String actualResult, String expectedResult, message).

Java




import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
  
public class Geeks {
    
    @Test
    public void geekforgeeks() throws InterruptedException {
    
        ChromeDriver driver = new ChromeDriver();
        SoftAssert softAssert = new SoftAssert();
        driver.manage().window().maximize();
        driver.get("https://www.geeksforgeeks.org/");
        String actualRersult=driver.getTitle();
      
        String expectedResult="GeeksforGeeks ";
          
        softAssert.assertEquals(actualRersult,expectedResult,"Title is not Matching");
        Thread.sleep(2000);
        driver.findElement(By.linkText("Java")).click();
        Thread.sleep(2000);
        softAssert.assertAll();
        driver.close();
                
    }
}


In this code, the actual and expected value are not matching, even if the exception is not thrown during the execution, and all the below lines are also executed. After calling the assertAll() method, the assert throws the exception and the test is marked as Failed. The output of the above code is,

 



Similar Reads

How to Use Hard Asserts in TestNG?
TestNG is an open source automation framework that is used to perform software testing more easily, it provides various annotations and is used to perform parallel testing and produce the test reports. What are Asserts? To check whether the test failed or passed during the testing, the TestNG provides an Assert method in which we can compare the Ac
2 min read
How to create TestNG XML file and Execute TestNG.XML File?
TestNG is a powerful testing framework for Java developers, inspired by JUnit and NUnit. It supports a wide range of testing needs, from unit testing to integration testing. One of the key features of TestNG is the ability to control the execution of tests using an XML file, known as the TestNG XML file. This file defines which classes and methods
4 min read
How to use Regex in TestNG?
In this article, we will see how to use Regex in TestNG. In TestNG, we can use Regular Expressions for two purposes: To Include Test CasesTo Exclude Test Cases To include test cases we use the include the keyword in the testng.xml configuration file.To Exclude test cases we use the exclude keyword in the testng.xml configuration file.Table of Conte
5 min read
How to Use DataProvider in TestNG Selenium?
DataProvider in TestNG is a powerful feature that facilitates data-driven testing in Selenium. It allows you to run the same test method multiple times with different sets of data, enhancing test coverage and efficiency. By using DataProvider, you can easily parameterize your tests and ensure that your web application is robust across various data
5 min read
How to use TestNG SkipException?
In TestNG, test management is crucial for handling different scenarios and conditions during testing. Sometimes, it is necessary to skip certain tests based on specific conditions or requirements. The SkipException class in TestNG provides a mechanism to skip tests programmatically. This feature allows you to handle situations where executing a tes
2 min read
How to programmatically hide Android soft keyboard
In this article, we will learn about how to hide soft keyboard programmatically. The keyboard generally hides but there are certain instances when it does not hide. So for better user experience, the keyboard is hidden programmatically. Without using below approach, the default response of the app is shown below:- [video width="440" height="520" mp
2 min read
Hard Assert vs Soft Assert
Asserts are used in Selenium WebDriver for verifying and validating the scenario under test. A test scenario is considered "passed" if the achieved test result matches the expected test result. In this article, we will explore the difference between hard assertions and soft assertions, as well as when and why each of these techniques should be used
5 min read
Assertion in Selenium WebDriver using TestNg
Prerequisite - Selenium Actual result is compared with expected result with the help of Assertion. It means verification is done to check if the state of the application is the same to what we are expecting or not. For creating assertion we are going to use the Assert class provided by TestNG. There are two types of Assertion:- Hard Assertions. Sof
2 min read
How to Test Java Application using TestNG?
TestNG is an automation testing framework widely getting used across many projects. NG means “Next Generation” and it is influenced by JUnit and it follows the annotations (@). End-to-end testing is easily handled by TestNG. As a sample, let us see the testing as well as the necessity to do it via a maven project. Sample Maven Project Project Struc
4 min read
Unit Testing, Integration Testing, Priority Testing using TestNG in Java
TestNG is an automated testing framework. In this tutorial, let us explore more about how it can be used in a software lifecycle. Unit Testing Instead of testing the whole program, testing the code at the class level, method level, etc., is called Unit Testing The code has to be split into separate classes and methods so that testing can be carried
6 min read
TestNG @AfterClass Annotations
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn this article, we will learn about @AfterClass
3 min read
TestNG @BeforeClass Annotations
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn this article, we will learn about @BeforeClas
3 min read
TestNG @BeforeMethod Annotations
The Concept Annotations is introduced in Java 1.5. The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations: @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn this article, we will learn about @BeforeMethod. Wh
4 min read
TestNG @AfterMethod Annotations
The Concept Annotations is introduced in Java 1.5. The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations: @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn this article, we will learn about @AfterMethod. Wha
3 min read
TestNG @BeforeGroups Annotations
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn this article, we will learn about @BeforeGrou
2 min read
How to Create Test Suite in TestNG?
In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups. Table of Content What is a Test Suite?Steps to Create Test Suite in TestNGUnder
11 min read
Difference between BeforeTest and BeforeMethod in TestNG
TestNG Framework is an open-source test automation framework based on JUnit and NUnit. It is considered an improved version of these two frameworks since it adds new features such as test annotations, test prioritization in TestNG, and grouping, parameterization, and sequencing techniques in the code that were not before feasible. The Concept Annot
7 min read
Data Driven Testing With TestNG
Data-Driven Testing with TestNG is a powerful approach that allows you to run the same test case with multiple sets of data. This methodology helps in achieving comprehensive test coverage and ensures that your application works correctly with various input values. By using external data sources like Excel or CSV files, you can easily manage and ma
4 min read
Difference between Selenium and TestNG
Selenium and TestNG are two powerful tools widely used in the field of software testing. Selenium is an open-source toolset specifically designed for automating web browsers, enabling testers to write scripts in various programming languages like Java, C#, Python, and Ruby. It supports multiple browsers, including Chrome, Firefox, Safari, and Edge,
5 min read
How to Download the Latest TestNG Jar File?
TestNG is a widely used testing framework in Java that helps you organize and run your tests efficiently. Whether you're a developer or a tester, keeping TestNG up to date ensures you have access to the latest features and improvements. You'll need to download the latest TestNG jar file to get started or update your project. This guide will show yo
2 min read
How to call testng.xml file from pom.xml in Maven?
Maven is a tool that is widely used for automation whenever developers work on Java projects. apart from the Java language, the Maven software supports other projects that are written in C#, Ruby, etc. The Maven project is controlled and hosted by the famous Apache software foundation. Table of Content What is a testng.xml file?What is a pom.xml fi
4 min read
How to Execute Tests Sequentially in TestNG?
In TestNG, parallel execution is a powerful feature that allows for running multiple test methods, classes, or suites simultaneously, which can significantly reduce test execution time. However, there are scenarios where you need to ensure that certain test classes execute in a specific order even while leveraging parallel execution for efficiency.
3 min read
How to Control Test Execution Order in TestNG?
Controlling the order in which tests run is crucial, especially in larger software projects. By controlling the test execution order, you ensure that tests run logically. This helps in identifying real issues in the code, rather than false negatives caused by running tests out of sequence. It also makes debugging easier because failures are more li
3 min read
How to run TestNG from command line?
Running TestNG from the command line is a powerful technique for managing your automated tests efficiently. TestNG is a popular testing framework that provides various features for running and configuring tests. While IDE-based execution is common, using the command line offers greater flexibility and is essential for integrating tests into CI/CD p
4 min read
Advantages of using TestNG with Cucumber
If you are working in the sphere of software testing, you probably heard about Cucumber and TestNG – these tools are often used in combination to reach high effectiveness and maintainability of the tests. Cucumber is one of the most popular tools for behaviour-driven development (BDD) that allows expressing tests in a simple language comprehensible
4 min read
How to disable a TestNG test based on a condition?
TestNG stands for the Test Next Generation, it is an open-source and automated framework that is used for testing, it is used for the testing of codes in Java programming language, Cedric Beust. TestNG includes lots of test categories such as integration, functional and unit testing, etc. and it allows the users to write the test cases and also org
3 min read
How to disable an entire unit test in TestNG?
TestNG is an integrated testing framework for Java, which provides several features to support automated testing. As with most test management solutions, a clear need has been seen to be able to comment out selected unit tests so that the code containing them is not removed entirely. In this article, I’ll explain to you how you can deactivate an en
5 min read
How to Ignore a Class in TestNG?
In TestNG, ignoring a class can be done using various methods like the @Test(enabled = false) annotation, excluding it in the testng.xml file, or using groups to control test execution. These techniques give you flexibility in managing your test suite, ensuring that only relevant tests are executed. By mastering these methods, you can maintain an e
4 min read
How to retrieve all test methods name in TestNG suite?
TestNG is a powerful testing framework that provides easy configuration and management of test cases. One useful feature of TestNG is the ability to retrieve and display all test methods in a test suite. In this example, we demonstrate how to access and print the names of all test methods defined in a suite after the execution of the test cases. Ta
4 min read
What is the order of test execution with priority in TestNG?
In TestNG, controlling the order of test execution is crucial for ensuring that your tests run in a logical sequence, especially when certain tests depend on others. The priority attribute in TestNG allows developers to define the order of execution explicitly. A lower priority value means the test will execute earlier, while a higher value will pu
4 min read
Article Tags :
Practice Tags :