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:

    .body("[0]", 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]", 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.

Wed, 09/02/2020 - 01:00
Tarun Goswami is a highly experienced professional, currently serving as Technical Lead at a well-established IT services company. With over 12 years of experience across multiple domains, he demonstrates his expertise and passion for the industry through his engaging and informative blog writing in his spare time.