This video goes over how to use
yield in a pytest fixture to add teardown logic to keep your tests isolated.
[0:00] You want to keep your test functions isolated. Sometimes that means taking extra steps to clean up certain conditions you've set up for the tests to run so that they don't bleed over into other tests.
[0:08] In this example when we run the tests they fail and that's because we have a database whose state persists across all the tests. So anytime we insert some data, it sticks around.
[0:22] Let's fix this by using a pytest `yield` fixture. Let's start afresh and remove the previous database and then inside of our database fixture instead of returning the database we're going to yield it.After the yields will tell the database to purge itself of all data.
[0:40] Now when we run the test they pass, and we can run them a few more times just for good measure. To see what's happening here let's add a few print statements so it's a bit easier to follow along.
[0:50] We'll add one just after the database is setup, one right at the end of the fixture and finally one at the end of our test. Let's run the tests again, this time a bit more verbose and focusing in on just a single test function and now we can walk through what's happening here.
[1:08] Pytest goes ahead and runs the fixtures code as usual until it gets to the `yield` statement which it returns down to the test function.The test function goes ahead and runs all the way through and finishes and then the code after the `yield` statement is executed which in this case cleans out the database.
[1:25] This is a really clean approach as we can define what we need to do to set up a fixture alongside what we need to do to tear it down and clean it up afterwards and the yield statement acts as the divider between setup and teardown.
[1:38] But what if something does wrong in our test function? Let's simulate that by throwing in a random `Exception` and then we'll run the tests again.
[1:47] Importantly we can still see that the code after the yield function still executes. This is handy as if something goes wrong in our test function such as an assertion failingthat stops the test from finishing we still run that teardown code.
Sign up to receive email notification when new lessons are released.