As we discussed in our post on getting started with Pytest, the framework allows us to reuse tests by using test fixtures and parameterized functions. In this post, we will learn how to do so.
We will build on the simple calculator example that we discussed in the previous post and add to it. Without further ado, let us get started.
As we saw in our small example, there is some repetition in the two basic tests that we wrote:
# calculator_test.py
from calculator import add
def test_add():
firstNumber = 2
secondNumber = 1
assert add(firstNumber, secondNumber) == 3
def test_add():
firstNumber = 2
secondNumber = 1
assert subtract(firstNumber, secondNumber) == 1
PythonThis might seem trivial in this case, but it is one of the productivity enhancements we can get with Pytest. We can avoid repetition and set up some helper code that gets executed before any tests. These fixtures are the perfect places to initialize resources that will be needed by the tests.
We can create fixture functions by marking them using the @pytest.fixture decorator. This provides us with a way of injecting dependencies into our tests via function parameters.
Functions that require these fixtures can then accept them as an argument. So our fixture definition will become:
import pytest
@pytest.fixture
def numbers():
firstNumber=2
secondNumber=1
return [firstNumber,secondNumber]
PythonSince a fixture is a function, we could have either defined two functions to return the two numbers, or we can combine them in a list and return it from one. And now, we can have access to this list in our test case, and our test simplifies to:
def test_add(numbers):
assert add(numbers[0], numbers[1]) == 3
PythonFor the subtract one, we can simply do:
def test_subtract(numbers):
assert subtract(numbers[0], numbers[1]) == 1
PythonA few things to note about fixtures:
We might want to reuse some of our fixtures across files as well. We can do so by creating a file named conftest.py in our directory. The tests defined in this file are available to all the tests in the directory.
We can even have multiple conftest.py files in a project, but only one per Python module. Tests defined in a module can use all fixtures defined in the configuration for that module and those defined globally.
By default, all fixtures are executed once per function. This can be an expensive operation if we are doing some intensive operation in our fixture. Every time an argument of a test has a fixture, that fixture gets executed.
For instance, if we were calling an API, we would make the request every time, slowing down our tests. We can define scopes for fixtures which gives us the capability to re-use fixture instances in between tests.
The possible values of scope are: function
, class
, module
, package
or session
.
# content of conftest.py
import pytest
import smtplib
@pytest.fixture(scope="module")
def smtp_connection():
return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
PythonThe smtp object will be re-used across the module.
When it comes to writing tests, we usually want to test the function with multiple values. And more often than not, we end up writing multiple test cases for doing that. Pytest provides us with a feature known as parameterized test functions which enables testing of different scenarios in a single function.
We use the @pytest.mark.parameterize decorator. Before getting into the implementation, let us first understand what markers are.
Markers are decorators that allow classification of tests or give us the ability to perform a specific action. Some of the relevant markers are:
@pytest.mark.skip(reason=None): skip the given test function with an optional reason @pytest.mark.filterwarnings(warning): add a warning filter to the given test @pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark the test function as an expected failure if eval(condition) has a True value @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names.
To see a list of all markers, we can use:
pytest --markers
BashAs we found out, we can parameterize our test cases using markers. Let us assume we wanted to test not just positive numbers but negative numbers as well. Instead of writing multiple tests, we would instead use:
# calculator_test.py
from calculator import add
@pytest.mark.parametrize("firstNumber,secondNumber,expected", [
(1, 2, 3),
(2, -2, 0),
])
def test_add():
assert add(firstNumber, secondNumber) == expected
PythonThis provides us with a succinct way of combining values and avoiding repetition.
And that wraps up our exploration of fixtures and parameterized functions in Pytest. Now go and write some awesome tests! If you have any questions, feel free to drop a comment below.
I am terrible at optimizing my keyboard layout for anything. But off lately, my little…
I recently switched completely to the Brave browser and have set ad blocking to aggressive…
I was preparing a slide deck for a hackathon and decided to put in a…
I have been using npx a lot lately, especially whenever I want to use a…
Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…
While working on a project, I wanted to do an integrity check of a file…