Effective PyCharm (2021 edition) Transcripts
Chapter: Unit testing
Lecture: Writing the core tests
0:00 Now that we have an idea of how to run pytest.
0:02 Let's write the actual test for this project.
0:05 We don't need that. We don't need that.
0:07 We're gonna work with our core here and let's go create a new file table test
0:12 singular, remember singular. So we're gonna start by testing what I call the
0:19 happy path. The way we expect our software to work.
0:22 So let's go over here and write a test something.
0:25 There's basically two main scenarios that we would like to test here.
0:28 One that there are tables available to be booked and then the other one is that
0:34 given one of those tables? We could Okay,
0:37 so let's go over here and right.
0:39 The first one now naming these tests if you haven't done a lot of testing is
0:43 interesting because the name of the test is different than regular function names.
0:48 Regular function names need to be meaningful but also manageable because you're going to use them
0:52 in your code testing function names will never be called by humans.
0:57 They will only be called by pytest.
0:59 So we can make them super descriptive.
1:01 So how about tests there are tables available.
1:07 That's gonna be our test name long for a regular function.
1:10 Perfect for tests. The way this works is we're gonna go over here and say
1:13 core and we can import that from 'app.core'.
1:17 Given a choice of category. Remember we had like burgers thai food and so on
1:22 Given a choice we would like to find the available tables but let's go over
1:27 here one. I think that might be thai food.
1:29 I'm not sure now. That might not be super clear.
1:32 What does that one mean? So let's go over here and introduce a variable choice
1:37 like that. Right? So we're gonna go over here and say tables order this
1:42 and what do we want to assert just that there is a table associated at least
1:46 one with category of food one.
1:49 Now, that seems good. We can come down here and just doing a search
1:52 remember, Python has a built in a certain thing.
1:54 That's how py tests makes its statements.
1:57 So one way we could do this so we could just assert tables.
2:00 The truthiness of this list here.
2:03 If we look at what comes back in a return as a list.
2:06 So actually that would work. But let's be a little more explicit.
2:10 What we're really trying to test is that there's one or more tables but we can
2:14 say the length of tables is greater than or equal to one.
2:18 All right, that's our first meaningful test.
2:21 We're running all the tests in this folder.
2:23 So let's run it again. There we go.
2:27 Look at that. Are there are tables.
2:29 Perfect. Now one of the ideas is you want to determine if we had written
2:35 this code. Wow. We were actually developing this library.
2:39 Maybe we would write the test then and do the test first.
2:41 Even one of the things you want to actually verify if the test is actually checking
2:45 the failure case if this work to work.
2:47 If there were no tables, we would return an empty list.
2:50 So let's make sure it fails.
2:52 Yeah. Okay, looks like that one is failing this assertion greater zero greater than
2:56 one. Not true. So it looks like our find available tables is working.
3:02 What's the other happy path test here?
3:05 It's table can be booked. That is our second test.
3:09 This one's a little more interesting,
3:11 a little bit more nuanced of what we need to test.
3:14 So let's get a hold of all the tables and our core library has all tables
3:20 And let's also make sure that none of these are books.
3:23 So let's say 't for t' and all the tables if 't.is booked' not as booked
3:31 like that. So this will give us all the tables that are not booked in
3:35 case. For some reason we run another test and it happens to change the state
3:39 of our little in memory database thing.
3:40 We want to make sure we're just working at tables that can be booked.
3:43 So the table that we want to book,
3:45 let's call it table is going to be tables of zero.
3:48 It will be the first one that's not booked.
3:50 And then let's just try to book it and see what happens.
3:52 So we'll go over here and say core. book table and we have a table.
3:57 table_ id now let's just make some assertions about this first.
4:02 Let's assert that there is a table we got back when we tried to book it
4:06 For some reason we didn't get none back or whatever.
4:10 Let's assert that the object we got back is booked,
4:13 right, so that it's properly configured when we book it.
4:16 Not just here's the table, but actually it is booked was set and that we
4:20 want to make sure it's the same table.
4:26 All right. That should do it.
4:27 These three things. I think as a set of three properties about this will verify
4:33 that Yes, we got a table that it was booked.
4:35 It was the one that we wanted a book and so on.
4:38 All right, run it perfect. Our tests are passing.
4:42 I think we've got some great tests here are there are tables available and tables can
4:47 be booked together. Really verify that our code is working well.