An assertion library is a collection of functions that can be used to verify the correctness of your code. Assertions are used to check for specific conditions, such as whether a value is equal to a certain value, or whether an object is in a certain state. Assertion libraries exist for all of the programming languages which you might be writing your automated tests in.
As you’d imagine, each of the assertion libraries that you’ll come across will have their own pros and cons. So let’s take a look into what assertions are, what different types of assertion libraries you can use and then we’ll jump in to looking at some of the most popular assertion libraries for the most common programming languages.
What Are Assertions?
Assertions are statements that are used to verify the correctness of your code. They are typically used to check for specific conditions, such as whether a value is equal to a certain value, or whether an object is in a certain state.
When an assertion fails, it means that the condition that was being checked was not met. This can indicate a bug in your code, or it can indicate that the input data was incorrect.
Assertions are an important part of software testing, as they can help to ensure that your code is correct. They can also help to identify bugs early in the development process, which can save time and money.
Here are some of the benefits of using assertions:
- They can help to ensure that your code is correct.
- They can help to identify bugs early in the development process.
- They can help to improve the quality of your code.
- They can help to make your code more maintainable.
If you are not already using assertions in your code, I encourage you to start using them. They are a simple and effective way to improve the quality of your code.
What is an Assertion Library?
Now we know what assertions are, you may be wondering where they come from. The simple answer is an assertion library. An assertion library is essentially a collection of assertions. This would generally include assertions for checking the values of variables, the state of objects and the results of calculations. They also typically provide ways to customize the messages that are displayed when an assertion fails.
Not all assertions are provided from assertion libraries, though. If you use a testing framework, such as NUnit or JUnit, these frameworks include their own assertion libraries, so you won’t need to install separate libraries to be able to use assertions in your tests.
Built-In Assertions vs Fluent API Assertions
Built-in assertions are a set of pre-defined methods that can be used to assert different conditions. They are typically provided by testing frameworks, such as NUnit or JUnit. Built-in assertions are easy to use, but they can be limited in terms of flexibility and expressiveness.
Fluent API assertions are a more recent approach to assertions. They allow you to chain together different methods to create more complex assertions. Fluent API assertions are typically provided by assertion libraries, such as Chai or Should.js. Fluent API assertions are more flexible and expressive than built-in assertions, but they can be more difficult to learn and use.
The best type of assertion for you depends on your specific needs and preferences. If you need a simple and easy-to-use way to assert, then built-in assertions may be a good choice. However, if you need more flexibility and expressiveness, then fluent API assertions may be a better option.
Here is a table that summarizes the differences between built-in assertions and fluent API assertions:
| Feature | Built-In Assertions | Fluent API Assertions |
| Flexibility | Less flexible | More flexible |
| Control | Less control | More control |
| Conciseness | Less concise | More concise |
| Robustness | Less robust | More robust |
| Ease of use | Easier to use | More difficult to use |
| Documentation | Better documentation | Less documentation |
Best C# Assertion Libraries
Fluent Assertions
This library is known for its expressive and easy-to-read syntax. It also has a wide range of features, including support for collections, exceptions, and asynchronous code.
Check out the Fluent Assertions website.
Shouldly
This library is similar to Fluent Assertions in terms of its syntax and features. However, it is also designed to be more concise and easier to read.
Check out the Shouldly website.
Should
This library is a more lightweight alternative to Fluent Assertions and Shouldly. It is a good choice if you are looking for a simple and easy-to-use assertion library.
Check out the Should website.
NUnit
This library is a popular testing framework for C#. It includes a set of built-in assertions that can be used to verify the results of your tests.
Check out the NUnit website.
MSTest
This library is another popular testing framework for C#. It includes a set of built-in assertions that are similar to the ones in NUnit.
Check out the MSTest website.
Overview of C# Assertion Libraries
| Library | Expressive syntax | Wide range of features | Concise and easy to read | Lightweight | Built-in assertions |
|---|---|---|---|---|---|
| Fluent Assertions | Yes | Yes | Yes | No | No |
| Shouldly | Yes | Yes | Yes | No | No |
| Should | Yes | No | Yes | Yes | No |
| NUnit | No | Yes | No | No | Yes |
| MSTest | No | Yes | No | No | Yes |
The best assertion library for you will depend on your specific needs and preferences. If you are looking for a powerful and expressive library with a wide range of features, then Fluent Assertions or Shouldly are good choices. If you are looking for a simple and easy-to-use library, then Should is a good option. And if you are already using NUnit or MSTest, then you can use the built-in assertions in those frameworks.
Best Java Assertion Libraries
AssertJ
This library is known for its expressive and easy-to-read syntax. It also has a wide range of features, including support for collections, exceptions, and asynchronous code.
Check out the AssertJ website.
Truth
This library is similar to AssertJ in terms of its syntax and features. However, it is also designed to be more concise and easier to read.
Check out the Truth website.
Hamcrest
This library is a matcher library that can be used to create custom assertions. It is a popular choice for creating assertions that are specific to a particular domain.
Check out the Hamcrest website.
JUnit
This library is a popular testing framework for Java. It includes a set of built-in assertions that can be used to verify the results of your tests.
Check out the JUnit website.
TestNG
This library is another popular testing framework for Java. It includes a set of built-in assertions that are similar to the ones in JUnit.
Check out the TestNG website.
Overview of Java Assertion Libraries
| Library | Expressive syntax | Wide range of features | Concise and easy to read | Lightweight | Built-in assertions |
|---|---|---|---|---|---|
| AssertJ | Yes | Yes | Yes | No | No |
| Truth | Yes | Yes | Yes | Yes | No |
| Hamcrest | No | Yes | Yes | Yes | Yes |
| JUnit | No | Yes | No | No | Yes |
| TestNG | No | Yes | No | No | Yes |
The best assertion library for you will depend on your specific needs and preferences. If you are looking for a powerful and expressive library with a wide range of features, then AssertJ or Truth are good choices. If you are looking for a simple and easy-to-use library, then Hamcrest is a good option. And if you are already using JUnit or TestNG, then you can use the built-in assertions in those frameworks.
Best Python Assertion Libraries
assertpy
This library is known for its expressive and easy-to-read syntax. It also has a wide range of features, including support for collections, exceptions, and asynchronous code.
Check out the assertpy website.
hypothesis
This library is a powerful tool for property-based testing. It can be used to automatically generate tests that verify the behavior of your code under a wide range of conditions.
Check out the hypothesis website.
pytest-bdd
This library is a BDD-style testing framework for Python. It provides a fluent API for writing assertions that are easy to read and maintain.
Check out the pytest-bdd website.
unittest.TestCase
This is the built-in assertion library for Python. It provides a basic set of assertions that can be used to verify the results of your tests.
Check out the unittest website.
Overview of Python Assertion Libraries
| Library | Expressive syntax | Wide range of features | Concise and easy to read | Lightweight | Built-in assertions |
|---|---|---|---|---|---|
| assertpy | Yes | Yes | Yes | No | No |
| hypothesis | Yes | Yes | No | No | No |
| pytest-bdd | Yes | No | Yes | No | Yes |
| unittest.TestCase | No | Yes | No | Yes | Yes |
The best assertion library for you will depend on your specific needs and preferences. If you are looking for a powerful and expressive library with a wide range of features, then assertpy is a good choice. If you are looking for a library that can help you write more robust tests, then hypothesis is a good option. And if you are already using pytest, then pytest-bdd is a good way to add BDD-style assertions to your tests.
Best JavaScript Assertion Libraries
Chai
This library is known for its expressive and easy-to-read syntax. It also has a wide range of features, including support for collections, exceptions, and asynchronous code.
Check out the Chai website.
Mocha
This library is a popular testing framework for JavaScript. It includes a set of built-in assertions that can be used to verify the results of your tests.
Check out the Mocha website.
Should.js
This library is similar to Chai in terms of its syntax and features. However, it is also designed to be more concise and easier to read.
Check out the Should.js website.
Jest
This library is a popular testing framework for JavaScript. It includes a set of built-in assertions that are similar to the ones in Mocha.
Check out the Jest website.
Vitest
This library is a new testing framework for JavaScript. It is designed to be fast and efficient, and it includes a set of built-in assertions that are similar to the ones in Mocha.
Check out the Vitest website.
Overview of JavaScript Assertion Libraries
| Library | Expressive syntax | Wide range of features | Concise and easy to read | Lightweight | Built-in assertions |
|---|---|---|---|---|---|
| Chai | Yes | Yes | Yes | No | Yes |
| Mocha | No | Yes | No | No | Yes |
| Should.js | Yes | No | Yes | Yes | Yes |
| Jest | Yes | Yes | Yes | Yes | Yes |
| Vitest | Yes | Yes | Yes | Yes | Yes |
The best assertion library for you will depend on your specific needs and preferences. If you are looking for a powerful and expressive library with a wide range of features, then Chai is a good choice. If you are looking for a simple and easy-to-use library, then Should.js is a good option. And if you are already using Mocha or Jest, then you can use the built-in assertions in those frameworks.
Summary
Some of the benefits of using assertions and assertion libraries are:
- They can help to ensure that your code is correct.
- They can help to identify bugs early in the development process.
- They can help to improve the quality of your code.
- They can help to make your code more maintainable.
So if you’re not using assertions in your tests already, hopefully some of the suggestions from this post will be of interest to you.
Have a favourite assertion library that isn’t listed here? Let me know in the comments below. Happy testing!