Getting started with pytest Transcripts
Chapter: Test Functions
Lecture: Writing our first tests

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we have our environment set up. I've got I'm in my project directory,
0:05 project pytest course. I have a virtual environment activated and it's the same one that I already have pytest involved installed in.
0:14 And now let's write some tests. So I'm gonna open up a code editor. I'm using VS code but you can really use anything and let's pull down the
0:24 terminal so that we can see both the terminal and the editor. So within chapter we're going to put it in Chapter two and we want a new
0:35 file that starts with, it needs to start with test underscores so that pytest can find it easily and the rest of it doesn't matter.
0:43 Test something works and now I'm going to write a test function. So let's start with a test function.
0:51 Just any this first one I just wanted wanted to pass the name of.
0:54 It doesn't matter except for it does need to start with test underscore still both the
0:59 file and the function should start with that and we can really do anything we want
1:04 within this function but I'm just going to find a tuple 123 and assert that that value is equal to the expected value of 123 that should pass.
1:19 So let's write this is so I'm in the directory. I'm not in the Chapter two yet. So we'll go into chapter two and go ahead and run pytest.
1:32 Cool. So we ran pytest within the direct chapter two directory. and it found test something. It did list some information about our environment.
1:45 It showed what our directory is, the root directory of the testing and it collected one item. The one item is the test that it found.
1:54 If there were two tests which will write shortly, it would say two items. And it said test something with a dot,
2:00 the dot, the green dot means everything passed. And then down here it says one passed and then the time it took. So this is pretty neat already.
2:10 it gets even neater. Well, if this isn't enough information for you and you're like, well what if it found something else?
2:17 We can give it a -V flag and it'll tell you specifically which test function and the two colons between the the the path.
2:26 This right now it's just a file name but this is the if we were somewhere
2:30 else and there's a subdirectory this would be the full path and the then two colons and then the test name, this is called the the test node ID.
2:40 And it shows up in pytest documentation. I probably won't refer to it too much but that's the note. I node name or node ID.
2:47 So let's um let's make this a little bit more uh exciting by having a failing test. So let's let's do a failing test and to make it fail,
3:02 we'll just make the tuple not the same. So uh 123 is not equal to 321. So this should fail. So if I run high pytest again I get more output
3:13 So I get this test session start the same information here did collect two items It's these two items and uh the dot is passing and the red f is
3:26 for failing. And then below below that we see the list of failures. And in this case it's one test says this is the test name,
3:35 test fail. And it points us exactly where the assert is and where the where
3:41 the problem is the failure is right here it says assert 123 is equals 321 that is not right and the index zero is different.
3:53 So it noticed that this is different but we also know that this is different. So hopefully if we give it a -V as it says it says use
4:01 -V to get more diff. So let's do that and if we do that then we've got the -V. One of the things we'll notice.
4:09 We also get the uh the more verbose test listings, we have both the tests this one past and this one failed and then we have
4:18 a full diff that explains has little carrots which is nice exactly where the differences.
4:24 So that's pretty cool. And pytest will show us it also has a little summary and then also one test, one failed in one passed and the time it took.
4:35 So that's nice. Um But it gives us as much information as it can come up with. So if we have, so this is uh this is something where there's placement,
4:44 there's there's that's wrong. If we have something else, like let's do another test, that's 123, but then add another element, so 1234,
4:54 those are both equal also, but there isn't how is it going to show us exactly how that's different? So we can give it,
5:01 let's run that again with the -V. So we see the full diff and we get it runs both. It runs all the tests even, it doesn't stop. It's got,
5:11 it'll show us the this full list and the the same failures before and then this new failure and has a different creative way to say it,
5:21 it says like this is different. So the right one contains one more item, which is four. So that's handy.
5:28 So anyway, Pytest will try to give us more information than as much information as it can as to why the failure happened,
5:37 which is really nice and we we don't have to do just equals, we can do not equals or less than or really any expression that can be converted
5:46 to a boolean, which is really anything that but usually, you know, these sort of algebraic looking sort of expressions are common in in python
5:57 for asserts. Anyway, this whole section here. This is what the it's called the trace back is this part and it gives
6:07 us both trace back for both failures. So we have actually quite a bit more to learn about pytest,
6:14 but we're going to use uh instead of like 123 and comparing tuples for the rest of the project and the rest of the course,
6:24 we're going to use the cards application for testing. So let's let's take a look at the cards application next.
6:32 And if you had any difficulty with this, trying to get these two tests, uh, two or three tests to run.
6:38 this would be a good time to try to figure out why because hopefully this is pretty easy stuff. The if you're having difficulty,
6:48 one of the problems might be that you're not in the right directory is the same one or you might not be in the right environment.
6:55 Virtual environment. You also, if you installed pytest, make sure that pytest is installed and that it's part of your virtual environment.
7:06 Um that's pretty much it also python version. so check the version. I'm using 3.10.3 Pytest supports 3.7 and above.
7:17 So hopefully you're using 3.7 or above.


Talk Python's Mastodon Michael Kennedy's Mastodon