Introduction to Assertions Libraries

QEKafe
3 min readOct 25, 2020

Hello Readers,

Today we are excited to launch our new series of articles on Assertions. In this series, we will focus on covering different assertion libraries that can be used to add-in assertions points in our test scripts. So let’s get started.

What are Assertions?

An assertion is a feature used to fail the test scripts in case the desired result doesn’t match with the actual result. It is a validation step that states whether the automated test cases got successful or not. To understand its relevance let’s say without this control we won’t be able to decide whether our application under test is working as expected or not. There are different methods for assertion including UI web element assertion, text content assertion, or location assertion. In case the assertion step fails, the test will be aborted and the remaining steps will not be executed.

Benefits of Assertions

  • Detects even minor errors that might otherwise go unnoticed.
  • Detects errors very early after their occurrence.
  • Ensures that the functionality of the code is working as intended.

Hard and Soft Assertions

Hard assertions are the ones that throw an assertion error in case of the condition not being fulfilled during execution and marks the test case as failed.

Soft Assertions carries on to the next step of execution even in case of the assertion condition not being met. They don’t throw an exception automatically for an assertion failure. It comes in handy in scenarios of multiple validations, where some validations have an immediate or direct impact on deciding the test case status. When softAssert is used and an exception is encountered, it continues until we call the method assertAll() to throw all exceptions caught. It is a good idea to use Soft Assertions in our test scripts as this is an effective way of handling test execution

Assertion Libraries

There are multiple flavors of assertions libraries available in the ecosystem, however, we have tried to list down some of the most commonly used in the testing space.

  • Hamcrest — It is a Java-based framework that supports creating customized assertion matches, allowing match rules to be defined declaratively. Refer to http://hamcrest.org/JavaHamcrest/
  • AssertJ — It is a java library providing a rich set of assertions, truly helpful error messages, improves test code readability. Read the documentation for more information. https://assertj.github.io/doc/#assertj-overview
  • TestNG — Although it is a testing framework, it comes with assertions that can be used. A list of assertions can be found at https://www.javadoc.io/doc/org.testng/testng/6.8.17/org/testng/Assert.html
  • JUnit — It is primarily a unit testing framework for Java which has assertions bundled to it. TestNG is an enhancement to JUnit
  • Truth — It is an assertion library maintained by the Guava team and used heavily by Google. https://truth.dev/
  • Chai — It is a BDD driven javascript based assertion library which can be integrated into any javascript based testing framework like Mocha, Jasmine, etc. Read through https://www.chaijs.com/

We believe that assertions are great for effective automation testing however, we have to very cautious in using them as they might create unnecessary white noise with the team and eventually delay the overall timelines. In the upcoming articles, we will talk about these assertion libraries in length.

Till then stay safe and keep writing to us.

Originally published at https://www.qekafe.com on October 25, 2020.

--

--

QEKafe

Our aim is to share cutting-edge Trends, Technologies and Best Practices with QE community