Mastering PyCharm Transcripts
Chapter: Unit testing
Lecture: Writing our tests

Login or purchase this course to watch this video and the rest of the course contents.
0:02 So we wrote this test and we can run it
0:04 and guess what, it passes because we're asserting true,
0:07 but yeah, that's not really that interesting,
0:09 let's write some actual test.
0:12 So remember, we're going to work on our core bit over here
0:15 and it does things like pull back a table,
0:18 you find available tables, given a choice
0:21 and this is like a numerical value that corresponds to
0:24 say like Thai food or burgers or whatever,
0:27 so we're going to write some tests
0:29 and the first one that we want to write is that there are tables available at all
0:34 in the beginning when the app starts, for some food category,
0:41 so let's name the test that, test_there_are_tables_available,
0:50 nobody is going to have to call this so let's make it descriptive,
0:53 so let's come up here and create a choice,
0:56 we'll say our choice is going to be one and be super explicit about this,
0:59 we'll say one tables, these are the tables corresponding to choice or category one,
1:04 so we'll say core.find available tables and we'll give it choice
1:09 and then we just need to do as assert that there are one tables
1:13 that is that the length of these tables is greater than zero
1:16 so maybe we could be a little more explicit I guess
1:19 because who knows really what comes back here,
1:21 maybe a query or something, so len maybe makes it a little more obvious.
1:24 Now let's go down here and one of the principles of testing
1:29 is that basically we want to see the test fail first
1:34 so let's just do a quick return nothing and run our test,
1:38 we failed, we got zero where we expected it to be greater than zero,
1:45 right here we asserted the value of that, now it being false,
1:51 it is zero which is false and so on.
1:54 So let's get that little trick away, this is not a real database
1:59 it's coming out of memory but just to keep it simple.
2:02 Now we should see our test.
2:04 Pass, awesome, so you can see all the output here that we get from pytest
2:08 and we have our test there, let's go write another test.
2:13 The next thing that we want to test is that we can actually book a table,
2:18 not just that they exist, but that we can book them
2:25 so we'll test that table can be booked,
2:28 and let's go and get the tables,
2:31 so we'll say tables = core.all_tables
2:35 that's just going to be something to start with
2:38 and then what we want to do is say come over here
2:41 and get a single table or selected table or whatever
2:44 is going to be a table of zero
2:47 we've already asserted that there are some so this should be okay.
2:51 So here's the table that we're going to work with
2:54 and then let's say we're going to go book it
2:59 so we'll book a table.table_id
3:03 so then all we're going to do is that assert that the booked one,
3:09 well we could assert that the thing is there and that it is booked,
3:15 so we have a table that we got back, it is booked
3:18 and maybe the final thing we'll assert
3:21 that booked.table_id == table.table_id
3:28 the one that we booked is actually the same one
3:31 it might not necessarily be the same pointer, but it is the same one
3:34 it looks like we're working with some funky offset here
3:37 so let's fix that, there we go,
3:40 okay, so that seems like a reasonable test, let's run it.
3:43 Boom, it passed, let's suppose for some reason it didn't pass
3:48 we should put not equal here— boom it fails,
3:52 so we can look at this test run over here a little bit
3:55 we have a few things that we can do,
3:57 notice you already have the time in milliseconds which is pretty awesome
4:00 you can sort alphabetically, we can sort by the slowest to fastest
4:05 you can collapse these down like this, or you can expand them out,
4:08 so if we had it like this we could just go say look at different modules,
4:12 we could come over here and run only the failed test, pretty cool,
4:17 so we could rerun them again a bunch of times by clicking this,
4:21 or we could run just the failed ones and notice this one is failed
4:24 so of course, we go and fix it
4:28 it's failed because we did sort of bad test,
4:32 we run that and now it's fixed.
4:34 That goes away, there's no more ones to run, but we can run them all,
4:37 there's one other thing here that is pretty interesting,
4:42 here we go run them all up there,
4:44 notice this, auto test, watch this
4:50 I probably need to leave that up for that to have any sort of meaning
4:53 but maybe make it smaller,
4:56 so let's go over here, to our core
4:59 and let's say we're trying to do an optimization
5:01 or a non-optimization, I don't know
5:04 let's define a method called test
5:08 and it takes a t for table or just a table
5:13 and in here, it is just going to do this, we could say test t
5:17 now this is insane to write, we're not going to say this,
5:20 like this is not how it would be
5:23 but watch what happens, notice when I save that,
5:27 it reran the test, it reran it right away,
5:31 and like oh yeah this is supposed to be table
5:33 so we could go back and put a t like this,
5:36 hit save, wait a couple of seconds and now the tests are running again
5:40 and it refactored this and say okay I wanted to say table here, that's cool,
5:44 wait a second and so as you work on your program,
5:47 the tests are just continuously running,
5:49 I'm not sure I would really like to work this way,
5:51 I feel like it would stress me out a little bit too much
5:53 to just have this constantly like running,
5:55 but it is pretty cool to basically have this green here meaning
5:59 your program is okay and it's just a continuous light of status right,
6:05 that's pretty awesome, let's unravel this because this is silly
6:07 but it give a little thing to write some extra code there,
6:10 all right, so those are the two— it's still running, isn't it, let's stop it.
6:17 So those are the two sort of straightforward tests,
6:19 that there are tables and that we can book them,
6:22 those are the main thing that we can do.
6:24 We're going to come back and look
6:26 at a slightly different variation for testing error conditions,
6:29 which is also important in code.