Mastering PyCharm Transcripts
Chapter: Unit testing
Lecture: Testing failure conditions

Login or purchase this course to watch this video and the rest of the course contents.
0:01 We've tested what I call the happy path
0:03 or the successful types of things,
0:05 we tested that yes there are tables, yes you can book a table,
0:08 let's test the other side of things.
0:11 The first test we want to write is that you cannot book a non-existent table,
0:15 so what do we mean by that,
0:18 well over here we tried to book this table
0:21 and we got this book back, and we don't really care about what comes back,
0:27 this is going to be a string that's supposed to go in here
0:30 and we're going to say not an ID,
0:33 so when we pass not an ID, what we'd like to see is that this is an error,
0:43 so what should happen here, well there should be an exception I think,
0:47 it could raise an error or it could just return none,
0:51 I'd kind of vote for raising an error, but let's see what happens.
0:54 Look at that, entity not found error, that sounds pretty awesome
0:59 so we tried to say we're going to get a table
1:02 but it said if there's no table we're going to raise this
1:05 there's no table with this id, and sure enough, that's what we got,
1:08 that's actually good, that means this is working
1:10 we tried to book a non-existent table, we got an error
1:13 but we need to indicate that, we need to tell pytest this is the intent
1:18 so over here we'll use a context manager
1:21 we'll say with pytest raises and then we give it the exception type
1:29 so core. ENFE, entity not found error
1:32 also notice right here I typed ENFE
1:35 just hitting the cap letters of this and it pulls up entity not found error
1:39 and you can even see it highlights it,
1:41 this is a really great trick for typing out long words, long type shortly.
1:45 And then we say, within that context block
1:49 we're going to try to call his function
1:51 and let's suppose for a minute it does not raise this,
1:54 I'll just have it do nothing for a sec, and if we run this,
2:00 well, I guess we weren't really able to get that other one to work, were we,
2:05 I've got to say pass, let's just do this, we're not going to test that
2:11 so over here we got an error because it did not raise the right kind of exception
2:17 we got an attribute error and then somewhere up here
2:20 we were expecting an entity not found error, so we got the wrong type,
2:25 so you'll see that we're testing this and if we do raise that error now
2:29 yeey, everything works.
2:31 Okay, very last thing, there's one other similar test that we want to do
2:38 and that is you cannot book a booked table,
2:43 so let's go over here and we'll get all the tables
2:46 and we're going to book one and then we'll try to book it a second time.
2:50 So we'll get all the tables, give us the first one
2:53 we'll book it by that id, I guess we don't really need this
2:57 maybe we could just inline this
3:01 I am not sure if this is better or worse,
3:04 but here it is booked, so this one is already booked
3:07 now we're going to come down here and try to do this again
3:13 try to book the same one twice, so this should work out not so well
3:18 in fact, let's pick a different one,
3:22 I'm a little concerned that up here we're not resetting this
3:25 and that we might have already booked that one,
3:27 so we'll pick that one and this one as well
3:31 okay let's see if we got it working— we do not, what went wrong?
3:36 Nothing went wrong, it worked, table unavailable, error
3:39 however, why did our test crash?
3:42 Because we're searching or we're looking for an entity not found there,
3:47 and we want table unavailable, now we should be all good.
3:50 Great, our tests are working,
3:52 so hopefully that gives you a good sense of how to write and use these tests,
3:55 we now have a pretty solid application,
3:58 now let's take a moment and kind of step back here,
4:01 step back and close a few of these things
4:04 we're going to come with them out so that we're not actually running as much
4:08 you'll see why in the next lecture.