Getting started with pytest Transcripts
Chapter: pytest Fixtures
Lecture: Sharing fixtures through conftest.py
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Okay, we have this pretty cool setup, we have a couple of fixtures that work together.
0:06
We have a function level one that makes sure that the database is empty. We have a module scope one that connects to and closes the database done
0:16
And then we have some tests and I'd like to be able to write a whole bunch of tests in different test files,
0:22
but not have to keep these all around in each test module. So I'm gonna next talk about sharing fixtures with a conftest file.
0:32
So I've got a different directory set up. So this this is all in test cards,
0:37
but I've got a different directory called fixture sharing that I'd like to to share with
0:44
you and I'm just going to keep this open so we can copy some code out of it. So with within conftest we're gonna put the the fixtures gonna grab the
0:53
fixtures and put it in conftext and this is just the naming convention, it's conftest.py, It can be anything else.
1:04
This is what this looks for and you can have one of these per directory if you want, you can share fixtures within this directory,
1:13
I can't share, use it outside of it, it can be in here, it can be lower too. So if we had sub directories under under fixture sharing,
1:22
we could use it there too, but we don't, it's just here, so this is that's just the fixture part and then just one we'll put just
1:32
one in the one of these in test one copy that just when it goes here, do I need to import anything Cards_db is just gonna come from conftest
1:43
So I don't need it cards_db. But right here, cards.card, I'll have to import card for that,
1:56
cards for that card object. The rest of us should be okay for the 01 test empty With that in zero.
2:13
And this I'm just using the cards_db. So I don't need to import anything that's kind of amazing and that's it.
2:21
Right now, let's take a look at what this test output looks like. So we're gonna go into fixture sharing and we should be able to just run any
2:38
everything here. So if we do pytest -V, we get test one item passed. Test empty passed. So yeah, this works, how is it working?
2:50
Let's take a look at. So we got this we don't need that multilevel anymore This conftest file that's sharing that. It can be used to share fixtures.
2:59
We have the test zero, it's just empty for zero. And test one, test two more. Let's let's watch it with the set up.
3:10
Shell. Okay Bummer. So we have something going on that I dont really want I really wanted the cards_db
3:23
That fixture to be to run to the connect to the database to run once per session, then I think we have the answer.
3:33
So it's once for module here and so it's going to run once for test zero and once for test, test one,
3:41
what we want is session and I probably should have named this something different now because it's this module everywhere, but it's only used here.
3:50
It's not used any in any of the test. Just replace cards module with, you know, session if we wanted to, should have chosen a better name anyway,
4:00
but let's look a little module session. Okay, so change the name cards_db
4:18
session. Now, the tests still just refer to the cards_db. We haven't changed anything with the tests. What happens now? Do we have to update the tests?
4:27
We shouldn't have to Oh, here we go. So we've got cards_db session running at the beginning and at the end and then
4:38
our function scoped cards_db running around tests, awesome. So now we have this these fixtures within this conftest file and I
4:48
can write as many tests as I want and group them together.
4:51
Just use these fixtures within this directory. This is a great way to share fixtures around the test directory.