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