Fixtures Teardowns
1. Fixtures Teardowns
In this video, we will learn about fixture teardowns.2. What is a fixture teardown
Fixture teardown is a process of cleaning up (or "tearing down") resources that were allocated or created during the setup of a testing environment. Remember the picnic analogy, which contained three steps: invite our friends to prepare the food, have fun, and clean up? In this analogy, "cleaning up" - is a teardown.3. Why to use teardowns
Teardowns are a very important part of the fixtures. Lack of teardowns can lead to significant issues, like memory leaks, low speed of execution, performance issues, invalid test results, and pipeline failures. We might meet all of those issues both during the test and after its end.4. When to use
We should use teardown every time we create a fixture. But it is especially important to use teardowns when manipulating big objects, which can lead to a lack of memory. Also, if we have more than one test that uses similar variables name, we can get the wrong results of the tests since the variables can get mixed up. In case if we are using "autouse" for the fixture, the absence of a teardown potentially might lead to even more dramatic leaks. On the other side, it is not necessary to use teardowns if our project includes only one script with only one test. In this case, teardowns might be an overkill. But otherwise, we should use it.5. Lazy evaluation in Python
"Yield" - is a special type of return statement in Python that allows us to create generators. Usually, that means that the function does not return all the results at once but does in portions, step by step, which is also called "lazy evaluation". In the following example the function "lazy increment" iteratively returns numbers increasing each one by one. But the "yield" keyword makes it a generator which operates "lazy". So the function does not return 4 immediately. Instead, each time we call it the loop makes exactly one step.6. How to use
But how can one use the teardown in pytest? One of the options is the Python keyword "yield". In the context of pytest, yield means "the line between the setup and the teardown phases". To implement a teardown with "yield", we simply need to replace the return with yield and place all the teardown code afterward. That's how pytest knows how to prepare an object, when to return it, and how to clean up at the end.7. Teardown example
Let's look at the example. Pytest was already imported here. We have a test function and two fixtures (one with autouse). The important thing is the code of "add numbers to list". After the list was modified, we yielded the "init list", and after that, we called for its method "clear" to make it empty. That's how we ensured that the list won't stay in the environment after the test or between them (in case we had more than one).8. Summary
We learned about fixture teardown, which is a process of cleaning up resources that were allocated or created during the setup of a testing environment. We can implement a teardown by entering the "yield" keyword instead of "return" and placing teardown code after it. Teardowns helps us to ensure the tests results, prevent software failures, and potential drops of performance. We should always use teardowns, especially when we have more than one test in a big project.9. Let's practice!
Now that you know about teardowns, try to implement some!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.