Getting started with pytest Transcripts
Chapter: pytest Fixtures
Lecture: Using fixtures for setup and teardown

Login or purchase this course to watch this video and the rest of the course contents.
0:01 In this last example we didn't have much going on here. We're just in this the num fixture,
0:08 we just returned 42 but we could conceivably think that there's some actions going on here
0:14 and in the next example we'll definitely take a look at pushing setup and tear down into the fixture. So let's say we've got this test DB file where
0:25 I'm I am importing DB object or DB class from some DB In the test, I have to connect to the database and then and I do that with a call
0:37 to the DB class in sharing the object, I do some action on the database and get a result back from that.
0:45 And again I'm checking to make sure that the result is 42 and then oops I'm doing the tear down and I'm closing here.
0:54 So I have to to close the database after I'm done with it. This is sort of a common scenario,
1:00 you can grab a resource, utilize it and close it or do something. Now this this is a problematic as we have it right now.
1:09 Hopefully you can see the problem when everything passes everything is fine, but if things fail, we if this assert fails,
1:17 if some action returns something other than 42, this close is never gonna hit. So we can't actually do the assert their and the tear down,
1:28 we have to move in this case we would really want to move the assert down to the down to the bottom set up and tear down for the database is within
1:37 the test and that's a little problematic now just to as an example, so let's let's go and run that Chapter three.
1:46 Still pytest test DB. That does work and it works because I've got this very simple database object. So in for this test case I just have this
2:01 this class and it doesn't really do much other than return 42, but it has an init to connect to the database and disconnect.
2:10 It's not really doing anything except for printing. And but let's let's take a look to just make sure that that is doing that
2:17 within the test. And again you can use -s to turn off output collection so that we can see those printouts.
2:26 And sure enough, I connect I do the action and disconnect. That's exactly what we want to have happen.
2:33 We were talking about this problem with the a certain having to tear down here
2:38 just to be fair python without pytest python has some ways to get around that
2:43 namely context managers. So let's look at an example of that same example if we
2:48 use the context lib context lib has a cool closing context manager that we can use
2:54 and we use it by just using a with block for a context and uh wrapping this called the DB
3:03 It creates, it returns the dB object after we're done at the end of the with block, it will call the close.
3:10 So I actually wanted to include this because context managers are pretty cool and you can use them with in testing also and the closing is cool.
3:20 As long as there's a close action that will work, let's run that. And sure enough we've got I'm just gonna leave this bigger,
3:28 yep, we connect, we do some action and the disconnect gets called when this context manager leaves, so that's pretty neat.
3:35 We can use a fixture instead though. So the the normal flow was set up call an action close if we use a pytest fixture, we can do the same thing.
3:49 So like our our initial num fixture. This DB call call does some action and so this is the the setup and this
4:03 is the tear down at the close of the tear down the yield. So in the, in our first example with the test dB fix,
4:14 In our previous example with the num fixture, it just returned 42 but in this case we want to do work after the return
4:21 So instead of return, will use yield and that will return the value and
4:28 hang out and this this DB object is available within this function to be able to
4:32 do the tear down afterwards and then we're passing the DB object to our test and now we can do some action on it and we don't have to worry about it
4:42 that the fixtures doing the set up and tear down of our resource for us. So just to make sure this still works, will run test dB fix awesome.
4:56 So this is still calling the connecting, doing the action and tearing down because we're yielding that DB object to the test.
5:06 Now, one of the cool things about fixtures is you can you can still use context managers. So let's combine the two concepts.
5:14 Now, in this example we are combining the two techniques, so we've got the context live with closing and we've got a little fixture DB fixture
5:24 that's using the context manager. So it's saying with closing DB as DB Yield DB, So we're not directly calling close,
5:33 we're letting this context manager called close for us and we can just yield within the fixture. The test is the same and we'll get the tear down
5:45 happen as part of the exiting out of the context, let's make sure that that works. Yeah, we're connecting the database,
5:57 doing some action and disconnect all of it's working great. So for the fixture side, we we can just show you these again,
6:05 set up a resource, yield an object, close the resource afterwards and this happens after the, all the tests are done that.
6:13 Use this fixture. Now, if we want to yield within a context manager,
6:19 we can in this context just sticks around until the the fixture is ready to call the teardown.

Talk Python's Mastodon Michael Kennedy's Mastodon