Transcripted Summary

In this chapter, we are going to take a look at how you actually make fluent assertions using assertpy.

An integral part of any test automation framework is the assertion library that you choose.

A test that doesn't assert anything is really of little value.

When it comes to choosing assertion libraries in Python, there are many options.

You can either choose something that is out of the box with your test framework like unittests, self.asserts, and there are a bunch of other modules that you can take a look at.

I prefer assertpy because it has really nice syntax and gives you many of the assertion functions out of the box.

It's very easy to use and also has very nice readable error messages that you can print.

Assertpy is inspired by AssertJ, which is a very popular assertion library in the Java community.

So, how can one actually get started with assertpy and assertions?

A good starting point is you can go to their GitHub on assertpy/assertpy, and you can just read the README - it is quite self explanatory and has a lot of examples of the API that it provides.

Here you can see it gives you a basic idea of the test structure from the assertpy module - you actually import assert_that function.

After that, whatever you write is going to be quite similar or readable like English.

Here you see I assert_that one plus two is_equal_to three.

This is much better than what comes out of the box and it gives a very readable message.

Let's get started with this.

We'll come back to the same project that we have been working on, and I have already installed assertpy.

So as you can see, the Pipfile already has assertpy.

If you don't have it installed right now, make sure you are within your virtual environment and just run:

pipenv install assertpy

That should take care of adding it.

However, if you're following the project from this repo, it already has this added, so all you need to do is run:

pipenv install

It's going to take care of setting up assertpy in your project.

All right, let's start with some of the capabilities that assertpy can actually give us.

If you remember, we had this method where we were making a get request on the people API, and then verifying that out of the list of first names, we have a person whose name is actually "Kent".

While this is all fine - and you'll notice that I've actually made use of assert_that and contains.

Here, first_names is actually a list of names and I'm doing a contains check.

But assertpy has a really neat feature wherein you can do a basic extraction.

In this particular case, what I'm actually interested in is the first_names of all the list of dictionaries that I have.

So how can I do that?

I have the response_text and you can use the extracting method and specify what the key you want to extract out of this list.

Here, I want to extract first_names.

def test_read_all_has_kent():
    response = requests.get(BASE_URI)
    response_text = response.json()

Then let's run this.

The test passes.

Remember, if you have not already started the people API, then you need to still go ahead and activate your virtual environment (pipenv shell) and then run python so that your application is up.

So as you can see, this works well.

I was able to extract a key right out of the list and then do a contains check.

But it doesn't just stop there.

You can actually even chain methods together.

So let's say I want to do something like - first, I want to make sure that there is at least a person named "Kent" so I can do something like is_not_empty() and then do a contains check.

Let's see if this works, and of course, this passes.

How does a failing assertion look?

Let me just run this with contains('Gaurav').

As expected, our assertion failed, and you can see that it gives a very readable message out of the box, saying that this was the assertion I tried to hit and it expected this particular list to contain Gaurav, but it did not - obviously, because it's not there.

To make it pass, you can always just replace "Gaurav" with "Kent" and it will go back to passing on its own.

This is just a taste of some of the features that assertpy gives.

If you go ahead and read the README on the assertpy GitHub site, you can see that it can do a lot of things.

It can do string matches based on many different conditions that you might want to check.

Often, we end up writing these convenience methods in a strings.util or numbers.util file.

But assertpy has all of that written for you, so why not just make use of it instead of reinventing the wheel.

It can also do regex matches, so you can actually even just give you a regex and match whether your string matches that regular expression.

It has similar methods for numbers, for floats and also it can do all sorts of checks on lists like whether the list is empty, is not empty, whether it contain certain elements or not.

As I already told you, it can flatten the list using the extracting method, and the same operations can be done on tuples.

It also has a rich set of functions on dictionaries, which is nice because you have all these convenience methods written out of the box.

I wanted to show you one more thing.

Some test frameworks like to make use of soft asserts.

A soft assertion is basically an assertion where you execute your entire test and all the assertions that fail are collected and then printed right in the end.

So any failing assertion earlier in the test doesn't stop your test execution.

How can you do that with assertpy?

It's quite simple - you just need to import soft_assertions from assertpy.assertpy, which is a function and make sure that whatever logic you want to be under soft assertion is under the soft_assertions context manager within Python.

You just use with soft_assertions() and then just make sure that your assert statements are within.

We know that these are going to pass, so let me just change the back to "Gaurav" and run this.

def test_read_all_has_kent():
    response = requests.get(BASE_URI)
    with soft_assertions():
        response_text = response.json()

You can see it actually ran the entire test and did not fail at the first failing assertion, and we have a very nice readable message as expected.

This is just a taste of what assertpy can do.

I would encourage you to include it in your test automation frameworks, and make sure that you are able to leverage all the benefits that it gives.

That's about it for this chapter. I'll see you in the next one.


© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR