Getting started with pytest Transcripts
Chapter: Test Functions
Lecture: Unit testing the Card class

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We'll go ahead and close up this the source code and within chapter two we got our test something that we've already played with
0:11 I'm gonna add another test and I'm gonna call that actually I'm gonna use this new
0:18 file thingy and call it test card just like the command line interface was doing I'm
0:27 going to say from cards import and then whatever I need to import right now we're
0:33 just going to test the card data structure so that's all I need to import. One of the cool things about data classes is we can do something like
0:43 access the fields within it in a dot syntax. So let's write a test to test that functionality and I'm just gonna go ahead and
0:59 create a card. You do that with the same card and then filling out information something and then the owner let's do Brian.
1:12 And just to do for the the state and then we'll go ahead and do an ID. Field in there. Now what's this going to look like?
1:28 So we when we access that we can do something C.id Owner state or summary or we can use that from dict.
1:39 That's cool. But so let's if I do summary say summary that should equal to something because I just did that let's go and just stick that in assert
1:56 and while we're at it let's just test everything to make sure everything is accessible
2:02 So the summary is something and then the owner is Brian and then the state
2:20 is 'todo' And the id. should be 123. Cool Is that it can we run that? Let's go ahead and try. So where are we at?
2:38 We're in the we need to get into the Chapter two directory and we've got our file test card pytest -V test. Sweet. It worked well that was easy.
2:57 Let's let's try another one. Let's do the default so if I don't fill anything in just defaults. If I do C equals cards but don't do anything in it.
3:13 What am I gonna get? Well we're gonna do I'm just gonna copy and paste to make this quick. We're gonna do the summary should be what should the summary
3:24 be? It should be none and the owner is none and the state is todo we we I think I remember the default being todo let's take a look again the API.
3:37 The state for the state is to do and the integer should be done also so we're gonna say none. Oh no todo. And this should be none.
3:56 Oh and I am already seeing some logic problems here so I just copied in pace
4:02 but we really shouldn't do summary anything equal to none so when comparing to none it's best to do is none.
4:18 Oops we do want equal for strings is none awesome so let's try that. Sweet. That worked to what do these look like?
4:30 What if I what if I want to print(c), Let's go and print them. Print, see and oops, forgot the braces, I'm gonna print those. Let's try that.
4:48 Nothing verbose, doesn't do anything, not verbose, still doesn't do anything. That's a bummer. What's happening is there's no failure going on.
5:00 So pytest will if there's any output within a test it will capture it and only show it to you if there's a failure. So let's make a failure happen.
5:13 Yeah. Ha ha. So is our print out their collected two items test summary Oh it did captured standard out call, captured that output. So that's cool.
5:28 But I don't really need the, I don't want to just add a failure just so I can see the output. So how do we do that otherwise?
5:35 There's a, there's a flag for pytest run it like that, there's no failures. If we do dash capture equals no, which is kind of a lot of typing,
5:48 we can see that. Let's do the in verbose also. So they separate a little bit. So it's printing out the, what the card looks like within here.
5:58 For each one of those, it's on a on the same line. That's a little annoying. Let's do another print just to create a new line.
6:11 I could have done a dash and also. Okay, so field access, this is what a card looks like if we print
6:18 it And with the defaults states there that's neat capture equals no is a lot of typing. So there's a there's dash S.
6:28 That does the same thing. That's cool. Let's take, we don't need these prints though after we figured it out.
6:33 So what I really wanted to show you here is just how easy it is to kind of learn about a data structure with tests.
6:42 We've got these little rendable things and as we build them up, I'm running everything right now. But you can easily for running all of it
6:51 you can easily just run one. So we can just say colon colon test defaults and just run run the one you're
7:01 working on. So this is neat now for we could write a whole bunch of tests and I went ahead and just pasted a bunch in so that we could talk
7:10 about them. We can test for equality to the the API Bit that says that the if there's two cards equal with the same same even if they have the same ID.
7:21 They should the equality should work. So we've got testing testing for equality. Well and what if the we said that it shouldn't matter if the idea is
7:31 different. So we're going to test for that too. And then we can test for inequality. So if things really are different then not equal should work.
7:40 From Dict. Here's the example from taking a dictionary and so we've got a card that has something brian to do in 123 the dictionary that kind of looks
7:52 the same and we can create a card from a dictionary and then make sure that those show up is equal that should work. And then also a two dicts.
8:04 So if you create a dictionary from from an existing card we would expect it to look like this and assert that those are equal.
8:15 Let's just go ahead and run that and see if that works. pytest -V. Ohh. It's got our other things in there to test something. So we just want pytest.
8:28 Let's clear that test card, sweet everything passed. And it just was really easy.
8:40 These are just little tiny snippets of things to make to test our understanding.
8:44 I really like this use of pytest and of unit testing just to understand kind of how things work.


Talk Python's Mastodon Michael Kennedy's Mastodon