Getting started with pytest Transcripts
Chapter: Parametrization
Lecture: Testing without parametrization

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In the Chapter four directory. We've got a test finish initial we're going to start
0:06 testing this finish operation. I've got a test started the states that we need to test or that we wanna run finish from either an in progress card or a
0:19 done card or todo card and make sure that the end result is that it ends in done. So I've got a test finish in progress,
0:27 that's the most natural one. I've got something that already started and I want to finish it. The cards_db fixture is the same one we used we ended up
0:37 with in the last chapter I've got it stuck in a conftest file where it's the same as we had before,
0:46 there's a session scope fixture that connects to the database and then we've got
0:52 the cards_db that does it's a function scope that does delete all and then returns the database to the test.
1:00 So just reusing that for this chapter and let's start with the, what do we have to do? We've got we want to start with a card.
1:09 So let's just C equals card and maybe something that doesn't really matter what it is. And the initial state of the card we want is in Prog we've got
1:25 our card that's in progress, we have to get it into the database. So I'm going to do a cards_db.add_card let's go look at that again
1:35 That pop up, adds a card and returns the idea of the card. That's great because I need that ID. To get it later.
1:44 So go ahead and add card but we need to grab that index. What are we going to do with the index? We're going to use that index to finish it.
1:52 So card_db finish that takes a card ID. Which is our index I. So at that point so I've got a card I've added it to the database and
2:06 now I've said it to finish that card starting from in progress and now I need to get it back out to see what state it's in.
2:17 So I'm gonna do a cards_db get get card. What did it tell me what to do again Get card. No I just give it the index now that's going to return a card.
2:31 We can save it or we can just let's just save it. So cards_db Card equals cards_db. Okay card. And now with that card I have to check the state.
2:46 So maybe a final state equals card.state that should do it and then assert final state equals done. There's our little test And We're in Chapter four.
3:10 Chapter four pytest test finish initial. Cool that worked just to make sure that we didn't let's just change this to in progress.
3:26 Just just watch it fail to make sure we're really doing this occasionally. I like to just do that watch something fail just to make sure yes it
3:36 really is done instead of in progress. Okay so that workflow seemed to work.
3:43 I'm gonna look I'm looking at this card thing and I know this is this might be a premature optimization but I'm only using it in these two places.
3:52 So let's I'm gonna tighten this up a little bit by just saying state this spot and just say cards, the final state is cards_db Get card ID.
4:08 Get card of I. And then taking the state of that. So that's what we want now. This is the finished state is also only being used in one place.
4:16 I mean we could go crazy with this. We could we could since we're only using the index here, we could put the the add card into here instead of the I.
4:26 And then for that matter we're only using the C. In this place. So we could do we could like great let's just try it, see what this looks like.
4:34 I am looking for readability though so we want to make sure that we stay readable So if I grab this,
4:42 the value of I we could put it in here and then the card we could do that just go ahead and do that.
5:00 However, I really like my test to tell a story and this doesn't really tell a story to me. This is a bit confusing.
5:08 So I personally don't like that version but what do I like about this one. So here's the story. I've got a card kind of a given thing.
5:17 Given a card I added to the database. I finished the card and then I'm getting the final state.
5:24 Yes this is I'm getting the card to get the final state but really I'm just
5:27 trying to find the final state so this collapsing is okay with me but the the others seem to break apart the story in a way that I can't read it.
5:38 So. All right I've got one test here to test finish from in progress so I've got the in progress one done we need to do to do and done also
5:47 So let's just should be straight forward we test finished to do and our initial state is to do and the rest should be the same.
6:01 We just wanna make sure that we can get from todo to done. And we also want to test it from done.
6:10 It's already done. I want to be able to see if I can finish it from done. That should be fine.
6:18 Actually I'll notice that this something is here is because card requires a summary in there. But do I need it?
6:29 Should I assert to make sure that this something is the same. Does that add any value? So let's just make sure that I'm not grabbing this
6:38 do final summary. Final summary is summary assert final. Is that adding any value though.
6:54 So let's just go ahead and run it to make sure that things are still working
6:56 Oh final summary? Oh that's not final summary assert that the variable final summary is something, okay so that passes everything passes so far.
7:09 I've got two items. Oh I forgot to change the name. Start finished from done. We should have had three tests okay we've got three
7:18 tests finished from in progress finished from to do and finish from done.
7:22 But this extra assert its not really we don't we know it's not going to change
7:27 I mean we could test for that but that's not really what we're looking at. So this confuses the story so I'm not going to I'm not going to do that
7:35 I think we're focusing on checking to see that the state changes. If we wanted
7:41 to make sure that this this the summary doesn't change during this action. I think that would be a different test.
7:48 So. All right so I've got three tests I can test this but there's a lot of redundancy here so let's clean it up a bit.
7:54 So there's we're really all of these are all the same. All we're doing is changing actually all of it's the same except for the initial state
8:02 so that's where parameterization comes in really handy but we're gonna do another thing that seen before we get there.

Talk Python's Mastodon Michael Kennedy's Mastodon