Getting started with pytest Transcripts
Chapter: pytest Fixtures
Lecture: Using builtin fixtures

Login or purchase this course to watch this video and the rest of the course contents.
0:00 But let's take a look at the built ins here. So we've got some things that look like they might be kind of cool.
0:07 The ones that I want to look at right now is the the temp path ones So down here at the bottom we got Tmp path factory and tmp path where we
0:17 are, we're looking at this fixture, that's a sessions to go fixture. there's two related fixtures that pytest gives us tmp path,
0:28 which is a way to have a temporary directory unique to each test function,
0:33 invocation. That's pretty cool. But we're setting up the test directory as a session
0:39 scope fixture. So what we need is this Tmp path Factory and that returns a factory instance for the test session. That's actually could be better.
0:49 doc string there, but I'll show you how to use it. So we've got all our code within fixture sharing.
0:56 I want to leave this here so that you can play with it at it as is, but I'm going to copy everything to a built in fixtures directory.
1:05 So where we were here and copy everything *.py into building fixtures. Cool. So now the built in,
1:19 let's close these off and built ins have the it's the same code we had before even including the doc strings because we just copied.
1:30 So that's cool, I'm gonna go over and make sure that it still works. Of course. Good to start out working.
1:40 Let's start replacing this. So we don't need this anymore. I'm gonna take, so this temporary directory and what was that fixtures again fixtures,
1:53 the tmp path factory, and that's what we're gonna use. So instead of this temporary directory and just this this fixture itself is going to use
2:04 the Tmp path factory fixture. The way this works is you create directories with it.
2:11 So I create temporary directories. I'm gonna say db path equals tmp path factory and you say make temp and you have to give it a base name.
2:27 So I'm gonna just give it cards_db temp. It really doesn't matter because the whole thing's created in a temporary directory and we have
2:36 a subdirectory within their And so that's my db path, at the session level. And now that's that's already a path lib object path lib
2:49 path object. So I don't need this set up and I don't need it as
2:55 a context manager anymore because the Tmp path factory already works like that because it's a pytest fixture. So leave this setup comment there,
3:07 restless coming over and now, um I don't need path lib anymore. I don't need a temp file anymore.
3:17 And now I'm just using this built in Tmp path factory to create a temporary directory. I make the temporary directory here,
3:25 we pass it to the Cards_db and everything else just works as normal. And then when I close the database and then pytest will it's part of Tmp path
3:35 factory when it cleans up it will clean up the directory. So we're in the built in fixtures directory where I just changed this.
3:44 So let's make sure everything works. But now let's look at the, the session again, pytest set up, show to see it, so the Tmp path factory gets called,
3:59 then the session. And I also wanted to point out, I am pointing this out earlier, every level shows you which fixtures are used.
4:07 So the session uses the factory, Cards_db uses Cards_db session and then the test is sort of inherits all of
4:15 them and uses the Cards_db and Tmp path factory. This is pretty great. Really,
4:21 what I wanted to show you here is that there's a whole bunch of cool built ins within pytest and it's useful to check them out.
4:29 So you can check them out here and there's with dash dash fixtures, you can see all the different ones available.
4:36 But I definitely recommend using, looking at the looking up the pie test website. So you go and check them out.

Talk Python's Mastodon Michael Kennedy's Mastodon