1. Introduction to fixtures
Today we will learn fixtures in pytest.
2. What is a fixture
Fixture is a prepared environment that can be used for test execution. For example, we might want to use some datasets for the tests. The process of preparing such an environment is called fixture setup.
Imagine that we want to go for a picnic with our friends. To do this, we have to complete the following steps: "Invite our friends and prepare the food", "Have fun", "Clean up". Fixtures are the functions that are responsible for the context preparation steps, which is the **1st** in this case.
3. Why do we need fixture
But why do we need it? Fixtures are the typical example of the "divide and conquer" approach. It helps to make the setup process easier by making our code more modular. Firstly, fixtures isolate the test of the environmental preparation. Secondly, fixtures make the code reusable, especially if we want to use the same preparation code for different tests.
4. Fixture example: overview
Let's take a look at the example of a fixture. Let's say we have a Python list named "data", and we want to test the following: that it contains nine elements and that it contains the elements "five" and "twenty-one". But before checking the data, we have to initialize it, so we can have it as a Python variable. In this case, the data initialization should be implemented as a fixture, separated from the test function.
5. Fixture example: code
Look at this code. First, we imported pytest. Then, we declared the fixture function "data" - that would be the name of the variable we want to use. It is important to declare the "pytest fixture" decorator to let Python know that this is not a regular function but a fixture. Otherwise, we won't be able to use fixture functionality. After that, we declared the regular function "test_list", which contains three test conditions, as discussed before.
Note that we passed the "data" fixture into the test function. As a result, inside of "test_list" we used "data" as a "list", not as a "function". In other words, pytest called the fixture already, so we could use the output of the "data" function, and not the "function" object itself.
6. Fixture example: output
If we look at the output of that test, it looks exactly the same as before. There is a CLI command to run the test and the test outputs: package versions, number of tests collected, test scripts that were analyzed, and the results of each test. In this case, we had only one test function in one script, thus one test passed.
7. How to use fixtures
Finally, to use fixtures, we must do the following steps. First, prepare the software and the tests for it. Second, look at your tests and distinguish the "environment preparation" parts, like data preprocessing or declaration of global variables. Then, create fixture functions for them. To create a fixture, declare the fixture decorator "pytest fixture" and declare the test function right after. Finally, we can use the created fixture in the test function by passing the fixture's name into the test function and running the test by executing a CLI command.
8. Summary
We learned about fixtures. A fixture is a prepared environment that can be used for test execution. We use them to make the setup process easier and more modular. A simple example of a fixture is preparing a Python list with data we want to test. One can define a pytest fixture by declaring the pytest fixture decorator followed by a fixture function. We can treat fixture names in the tests as regular variables.
9. Let's practice!
Now you can use fixtures by yourself. Try it in the next exercises!