Python API test automation framework (Part 3) Adding fluent assertions using assertpy

Logos in header image sources: Python, Requests, JSON, HTTP

This is second post in a series on how to build an API framework using python.

You can read previous parts below:

An integral part of any test automation framework is how you perform assertions ✅ 🔴. You can also argue that it’s the essential bread and butter of test automation. Ever seen a test that does not assert anything? 🤔

While you can always choose what comes out of the box with your language or test framework, or even come up with your own wrappers. It’s always a good idea to not reinvent the wheel ☸️ and leverage a good assertion lib to save you some valuable implementation time.

For python, there are many options available. However I particularly like the assertpy library for its fluent assertion capabilities and the fact that it has excellent support for working with native python data structures like list, set, dict among other primitive data types.

It is heavily inspired from AssertJ library which in itself is quite popular in the Java community and used in many popular open source tools

Starting with fluent assertions

How to start with fluent assertions with assertpy, you may ask?

Like any other python module, You should start by adding it to your virtual env by using below

Also, reading the exhaustive and well written Github readme is an excellent starting point as it provides you with a huge list of operations and methods that are possible with assertpy. You can learn more on assertpy/assertpy

Basic assertions

Let’s see some basic assertions for our use cases and I would leave the rest to you to explore as and when you encounter certain use cases.

It’s generally always a good idea to skim through the library’s capabilities so that your brain remembers if capability X is something that you have already seen and just that act can help you with future use case

Let’s say you want to chain multiple assertions on a particular person from the same people response that we have been looking at:

Remember we did the check of extracting the first names from available persons and then added a check to see if Kent is contained in this:

We can use capabilities offered by assertpy to extract a list from a list of dict objects based on a certain key and then chain multiple assertions together. The above can be effectively replaced with.

Observe that we are able to do multiple checks on the same object and this reads fluently like an english sentence. 🙌🏼

Also, Notice when the assertion fails, we get a friendly human readable error message like below:

Replace Gaurav with Kent to see the assertion pass.

The most common operations that you might perform with the assertion library are:

And these among many others are easily supported by assertpy out of the box.

Printing a custom assertion message

Let’s say you want your own custom message to be printed when an assertion fails, You can easily do that use description='' keyword argument

For example, I’ve wrongly modified the expected response code from no_content to ok and assertpy gives the below readable stack trace along with our provided error message

Soft assertions

You can also do Soft assertions (i.e not stop your test on the first failure) but instead raise a single assertion error at the end, to do so, ensure that you make use of soft_assertions() function and python context manager using with clause

In the above example, we’ve deliberately modified the status code assertion and the response assertion.

Below is how it looks like once the test has run, we see both assertion messages show up.


I hope you have a better sense of how a dedicated fluent assertion library makes your framework much better and I hope you would use them going forwards. That’s it for this post. We shall see how to work with XML and JSON data types in the next posts in the series

If you found this post useful, Do share it with a friend or colleague and if you have thoughts, I’d be more than happy to chat over at twitter or comments. Until next time. Happy Testing and Coding.

Originally published at on December 7, 2020.

Manager SDET at Gojek, Bengaluru, I ❤️ to build scalable test automation frameworks and teams. Blog at 🇮🇳

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store