If you’re a developer, you must be familiar with unit testing: a framework to check for errors or bugs in a program and ensure that it works as expected. In the age of programming, one is all too familiar with Python, a popular, high-level, and general-purpose programming language. When working with unit testing in Python, Pytest comes into play.
Pytest is a built-in automated testing framework in Python that makes it easier to test codes. It can run several tests in parallel, generally involves a compact piece of code, and saves computation time if compared to unit tests – Python’s default testing framework. However, some processes of unit testing in Python can prove to be confusing, and this article is here to clear one of them – Pytest Mocking.
See Also: How To Create a Nested List In Python
What Is Mocking?
Mocking is one of the processes in unit testing where the unit has external dependencies. Sometimes, one may wish to avoid dealing with them during the test; they may be costly or may not be available during the time of testing. In such cases, replacement objects are used that replicate and simulate the behavior of the actual dependencies to isolate the code from the latter. This process lets us focus on the code instead of the behavior of these external dependencies.
In simple terms, when we write codes, certain services or actions are attached to them. These cause results that are, although crucial to the code, unwanted while testing the same. Assume that you’re building an emergency services app, and you need to test out the feature that allows you to place emergency calls/requests. However, it will be inconvenient, not to mention illegal, to contact these services without an emergency every time you do a test run. Therefore, you need to test this feature without placing a call/request to emergency services.
At times, our code may use dependencies like date and time, due to which we may get different outputs every time during a test run. To remove this issue, we must replace the date and time with an object that, in general terms, acts like a ‘catalyst‘ – it can mimic the usage of date and time in the code without affecting the output.
Mocking make all of this possible, allowing us to concentrate only on the actual code, its correctness, and its result without any undesired issues.
What Is Pytest Mocking?
In Python, we use mocks to replace objects for testing. Python has a built-in mock library called unittest.mock – it allows us to do the same and make assertions regarding the use of replaced objects. The mock objects contain inspectable information about the usage of dependencies. This built-in mock library accommodates a core class called Mock, which removes the need for creating stubs throughout the test suite.
We use pytest-mock to create the mock objects through the mocker fixture: an interface in pytest-mock. To implement mocking, we first install the pytest-mock Python package.
How To Use Mock In Pytest?
The first step to mock in Pytest is to import the Mock class from unittest.mock.
After the import, we need to define the desired function. It is vital to remember you can only mock the dependencies and not the desired function. After importing the desired function, we must define the function you want to mock by specifying its complete dotted path within the test script.
The step that follows is setting expectations. We understand setting expectations as defining the value that should be returned instead of in the mocking function. We can use assertions to put various conditions and check if they are true or false.
On completing the above steps, you must ensure that your test script is ready. You will realize that your setup does not realize that the mock object is not the actual dependency and will directly run the method as if it were a real one.
After completing the test setup, the final step is to run it and check whether it passes. You can similarly add various test cases to test different possible outcomes and figure out any errors or bugs in your code.
In a nutshell, Pytest mocking has made unit testing a straightforward and plain-sailing task. Running numerous test cases and understanding various outcomes without employing the dependencies has emerged as a simple, uncomplicated task. We have now not only understood, in uncomplicated terms, what mocking is but also simple steps to use it in a test setup.