1. Fixtures autouse
Today we will learn about the fixture "autouse" feature.
2. Autouse argument
"Autouse" is an optional boolean argument of a fixture that can be passed to the fixture decorator. When the autouse equals True, the fixture function executes regardless of whether it was requested or not. Sometimes there are fixtures that we need to call repeatedly. Autouse helps to avoid repeated calls that would lead to unnecessary computing. In that way, autouse helps to reduce unnecessary calls of fixtures that we always need.
3. When to use
When should one apply "autouse" to their fixture? Generally speaking, when we need to apply certain environment preparations or modifications for all tests at the same time. For example, when all of our tests run on the same data, using the same connections, in the same environment, or we just want to monitor the process. All such cases should be addressed with an autouse argument.
4. Autouse example
Look at this code. Here we have two functions: a fixture "set_pd_options" that is autoused and a test function "test_pd_options". In this example, we are changing the parameter "display max columns" of the "pandas" library from its default value (which is 20, by the way) to five thousand. Note that the test function DOES NOT request the fixture "set pd options" explicitly. But the option "max columns" has been changed anyway because the fixture "set pd options" has an "autouse equals True" in its decorator.
5. Autouse incorrect example
Note that we can not declare variables inside of the fixture function and use them outside of it by default. Let's take a look at the example of the incorrect use case. Here, we defined a fixture "wrong_fixture", that is "autoused" and returns a Python list. The "wrong_fixture" function was executed. But, we cannot use its result because we did not pass the "wrong fixture" name to the test function. Thus, with the help of "autouse", we can only change the variables that were declared outside of the fixture, such as environment options or global variables. However, we can not use the fixture's output without passing the fixture name to the testing function.
This code will work. But note that we do not need to use autouse in this simple case since we passed the fixture to the test function explicitly.
6. Autouse example: output
The output of the example looks as before, and also it informs us that the one test passed.
7. Summary
Let's summarize. Autouse is an optional boolean argument of a fixture. One should pass "autouse equals True" into the fixture decorator to use it. The main reason for using it is to reduce the number of redundant fixture calls to simplify our code. When autouse is True, the fixture function executes regardless of a request. We should use it in case when we need to apply certain environment preparations or modifications for all tests. For example, when we want to read and prepare the same data, configure connections and environment parameters, or implement a monitor.
8. Let's practice!
Now try to interact with the new parameter by yourself.