Getting started with pytest Transcripts
Chapter: pytest Fixtures
Lecture: Testing cards with database setup and teardown

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now I'd like to take the concept of fixtures and apply it to the cards application
0:05 So I would need to pick a feature to test and the feature we're going to test is the count feature it's a fairly simple concept if I run cards with
0:16 nothing in it, there's no cards listed but I can run count here and get zero. Awesome. Now if I add something,
0:27 cards add something now, cards lists it and the count is one. That's easy enough. Let's see if we can turn this into an automated test In
0:46 Chapter three, I've moved the previous learning items we've got into a learning fixtures
0:53 subdirectory and we're going to use a test cards subdirectory under Chapter three still. And let's get started with this.
1:04 So I generally, I know I'm gonna have to do import cards so import. That's good. And I want my first test to be testing for empty and how
1:18 should this start? So I kind of want to say given an empty database when count is called, Then out, return zero. That should be easy enough.
1:34 So I need to have a database that will be our setup and then call count and then check, assert that it's zero.
1:41 So let's get started database, we need to set up that. So cards, cards, DB. And the cards DB takes a path lib path object.
1:52 So we're gonna have to create one of those. So I'll just go ahead and cardsDb, the DB.
2:01 path that we'll have to pull in path lib path lib equals path lib path and now I need a directory.
2:16 So to get a directory, I'm gonna utilize something that python has called temp file so from temp file import and it has a temporary directory class,
2:32 but it can be this temporary directory class works great as a context manager.
2:36 So we'll go ahead and use it as a context manager with temporary directory as maybe db dir and shift those over.
2:54 So now I'm creating a temporary directory, creating a path and passing it to cards. Now, this should be since I'm creating in a temporary directory,
3:04 this should definitely be empty. So I think we're good there. So we just call dB count count equals dB count.
3:15 And then we should assert that the count equals zero. I'm in test cards. Director subdirectory of Chapter three. Great pyTest test count initial.
3:34 Yeah, wow, that actually works. That's pretty cool. So this is the setup. So this is our given given this setup when count is called count,
3:44 returns zero. So there's Oh, I forgot to tear down the database. So we have to clean up,
3:53 turn down maybe call that. Turn down db. close the same thing as the camp assert before. So we want the dB close to be before we assert So
4:07 go ahead and do there. So we've got the this is our setup This is our tear down kind of in the middle.
4:17 The real part, the real part of this code that I want to be obvious in this test is just really this line and this line the rest of it is
4:28 just extra stuff that is sort of distracting. It's necessary boilerplate for the test.
4:34 But um but I don't really like it because we'll give you an example. I wanna test like one item.
4:42 If we test not non empty or one item given an empty directory when count is called then count return zero. But but I want to have a return like 2
5:01 so I can't do it uh empty here. Given a database with two items, I need to put some items in there. So Db. Add card and we know how to do we know what
5:19 cards are card and something. Let's have a couple of cards. So now we've got two items. We started out with this empty directory,
5:35 empty database. We added a couple of cards that should end up being 2 name error. What'd I do wrong? Oh I forgot to import card.
5:50 No we've got two tests. Oh that's this, this is one item. So I really want one item, make sure that still works okay.
6:02 And one item get aways with one item. This there's still a lot of redundant stuff between these tests.
6:11 So let's let's try to fix that by putting in to a fixture.


Talk Python's Mastodon Michael Kennedy's Mastodon