Understanding Unittest.Mock

Testing Doubles

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment introduces the base of the code that we want to tests and creates a test to verify it. As the test has a dependency, we create manually a testing double to isolate it.

Keywords

  • mock
  • create
  • test
  • verify
  • dependency
  • manual
  • double
  • isolate

About this video

Author(s)
Mario Corchero
First online
25 December 2018
DOI
https://doi.org/10.1007/978-1-4842-4413-5_1
Online ISBN
978-1-4842-4413-5
Publisher
Apress
Copyright information
© Mario Corchero 2019

Video Transcript

In this segment, we are going to cover Testing Doubles. We’re going to see some basic concepts about testings. Uh, we’re going to see how to create testing doubles and why do we need them. And we’ll learn by… in understanding why test isolation is important, how does it help us. Hello. We’re going to see how to test code in a way that’s repeatable and isolated from the central systems. So let’s see, we have here a function called “get user data” that takes the user and then a session. The session is a request session which is basically an object that allow us to hit an HTTPS endpoint. All this function is doing is just constructing the URL that it wants to hit and then get the data out of it by using the session. We will want to test this code. And if we were to run it, as we can see here, we just create the session, pass it to get user data with the user and query it. This will return us the real response that contains the actual data as extracted from the web page. This is far from it, you know, when we’re trying to find unittests. But what we are trying to do is create a test that is repeatable and where we can assert and the function only that we wrote rather than the system that it’s querying. For this, quite often what we do is we just create what’s named as a testing double. A testing double is just basically an object that has an interface similar to the original one and behaves like the original one but doesn’t have all that code that we don’t want to test. It just allows us to isolate our code from the real world. So as an example, we can create here a fake session which basically just have a method called get that they say URL. We are going to make, for simplicity, just going to return just testing. When we now pass this session, so we say session = FakeSession, using this testing double to isolate their function from the real GitHub. When we run it here, we can see now that the type they do is testing. But of course there are situations when we have more complex behavior. Let’s say for example that as we can see in this old example, we are not only just getting the data but we are also working with it. So when we get the response, we’re going to get the JSON response out of it and then we’re going to take the name out of it. If we run this code again, just an example, we see that get the name of the user. If we wanted to test this in isolation, we’d have to write two testing doubles. So we’ll start by, for exam… by creating the, um, the session as we did before, so FakeSession. Again, we could get method. And now this time, what this is going to return is another class which is going to be the response, so FakeResponse. Now this FakeResponse is going to be another class, another testing double that’s going to have a JSON method that once it’s called, it’s going to return whatever they do one, now we have to control what do we want the class to do. So for example we can say that this is going to return A dictionary that contains name and the body is going to be Mr. Testing McTest. When we run out this code passing now the FakeSession, we can see how again, we are testing out a code without the need of it reaching to the real GitHub. This is key when we write unittest because this allows us to write our test in a way that’s independent and isolated from the real system. This means that no matter what happens in the isolate, say for example that the user waives and the test get deleted in the real GitHub, our test will be saved because they are using this testings doubles. Additionally, we make sure that the tests are repeatable because, again, even if the user was not deleted but was modified or if there’s an error in the connection, using testing double isolates our code and ensures that we are only testing the part of the code that we wanted to. In the following… for the following segment, we’ll see how Python through its standard libraries allow us to create this kind of testing doubles in a much more real simpler way.