Skip to main content

Soft Assertions in REST Assured

Assertions are used to perform various kinds of validations in the tests and help us to decide whether the test has passed or failed. There are two types of assertions basically Hard and Soft Assert.

Hard Assertions in REST Assured

As the name suggests, these assertions put a strict restriction on the test script in which it is placed. When using hard assertions in the test scripts, your test script will stop executing when the assertion fails, and the test will be failed. Example of hard assert in REST Assured:

.then()
    .body("[0].user.name", equalTo("Tarun"))
    .body("[0].user.screen_name", equalTo("tgoswami013"))
    .body("[0].user.followers_count", equalTo(19));

In above example if first assertion fails. It will not check for remaining 2 assertions.

Soft Assertions in REST Assured

To deal with the disadvantage of Hard Assertions, we have an approach in REST Assured. Above code can be rewritten for soft assertions as:

.then().body("[0].user.name", equalTo("Tarun1"), "[0].user.screen_name", equalTo("tgoswami013"),"[0].user.followers_count", equalTo(19));

In .body() you need to specify all assertions separated by comma. Above code will validation all 3 assertions and then fail the scrit.

Submitted by tgoswami on September 2, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.