1 00:00:00,200 --> 00:00:02,990 Sometimes we want our code to fail in certain ways. 2 00:00:02,990 --> 00:00:03,880 We want to be sure and 3 00:00:03,880 --> 00:00:07,570 raise a value error when someone sends us a string instead of a number. 4 00:00:07,570 --> 00:00:11,510 We want to trigger a does not exist when a database record hasn't been created yet. 5 00:00:11,510 --> 00:00:14,960 So of course we want to test that these exceptions get raised. 6 00:00:14,960 --> 00:00:16,850 Let's look at the assert raises assertion. 7 00:00:18,370 --> 00:00:24,070 So when we read our Roll class and we look here, we see the description is this. 8 00:00:24,070 --> 00:00:27,890 And we have this die_pattern.match, which if we go look at die_pattern, 9 00:00:27,890 --> 00:00:31,770 we see it has to be a number, and then the letter d and then another number. 10 00:00:32,930 --> 00:00:37,030 And if it doesn't do that, we see it raises this value error. 11 00:00:37,030 --> 00:00:40,020 So we know that, that'll happen, right? 12 00:00:40,020 --> 00:00:44,910 If we pass in a bad value then we're gonna get this value error back. 13 00:00:44,910 --> 00:00:46,910 So let's write a test to cover that, 14 00:00:46,910 --> 00:00:49,530 to make sure that it works the way that we think it does. 15 00:00:50,880 --> 00:00:54,884 So we're gonna write a def test_bad_description. 16 00:00:57,640 --> 00:01:01,090 And we're gonna use what's called a context manager which is this width. 17 00:01:01,090 --> 00:01:02,890 It's where we're able to control the context, 18 00:01:02,890 --> 00:01:06,090 we're able to change variable inside, within here. 19 00:01:06,090 --> 00:01:10,680 So we're gonna say self.assertRaises and it's going to raise a value error. 20 00:01:10,680 --> 00:01:16,140 And then we're gonna call dice.Roll and we're gonna pass it in 2b6. 21 00:01:17,910 --> 00:01:19,360 All right? We know that, that's wrong. 22 00:01:21,000 --> 00:01:24,690 So 2b6 because the b and the d don't match, so we know this is wrong. 23 00:01:24,690 --> 00:01:25,360 This assertion is 24 00:01:25,360 --> 00:01:27,750 used a little bit differently than the others that we've used. 25 00:01:27,750 --> 00:01:31,370 We had that width keyword I talked about, the context manager. 26 00:01:31,370 --> 00:01:36,050 And then we're also passing in a value of what the exception is going to be. 27 00:01:36,050 --> 00:01:37,510 And it's a block. 28 00:01:37,510 --> 00:01:38,880 Within that block, then we call the code. 29 00:01:39,920 --> 00:01:44,640 So if the code actually causes the exception, then the test passes and 30 00:01:44,640 --> 00:01:45,850 if it doesn't then it fails. 31 00:01:45,850 --> 00:01:49,980 It's slightly backwards thinking but you really get used to it. 32 00:01:49,980 --> 00:01:51,040 So let's try running this one. 33 00:01:52,480 --> 00:01:53,780 So that works. 34 00:01:53,780 --> 00:01:55,960 So all of our tests have passed and it's because we're so 35 00:01:55,960 --> 00:01:59,600 super smart, or it's because we know what the code's gonna do and 36 00:01:59,600 --> 00:02:02,550 we're writing our test for exactly what the code's gonna do. 37 00:02:02,550 --> 00:02:07,390 Let's write a new test that won't pass, 'kay? 38 00:02:07,390 --> 00:02:11,960 So let's actually go up here to the we're gonna do it down here. 39 00:02:11,960 --> 00:02:15,916 And we're gonna write def_test_small_die. 40 00:02:15,916 --> 00:02:19,150 'Kay, so we're gonna have a small die in this. 41 00:02:19,150 --> 00:02:26,920 So we're gonna do with self.assertRaises(ValueError). 42 00:02:26,920 --> 00:02:31,318 So just like before we're expecting this to, 43 00:02:31,318 --> 00:02:35,168 to fail, we're going to do Roll 1d2. 44 00:02:35,168 --> 00:02:39,581 Now this is actually a totally acceptable die, we can roll a two sided die, 45 00:02:39,581 --> 00:02:41,610 it's called a coin. 46 00:02:41,610 --> 00:02:44,600 So, we know this is valid, so this test should fail. 47 00:02:44,600 --> 00:02:47,130 So lets run this, and it fails. 48 00:02:47,130 --> 00:02:50,050 The value error was not raised, 'kay? 49 00:02:50,050 --> 00:02:52,740 So that's great, we, we know how this fails. 50 00:02:52,740 --> 00:02:56,460 Now we can go in here and we can actually mark this as expected to fail, 51 00:02:56,460 --> 00:03:00,940 if you look at the docs which I'll link to, you'll see that you can mark this. 52 00:03:00,940 --> 00:03:02,440 That's kinda silly. 53 00:03:02,440 --> 00:03:06,160 We know this one's going to fail because this is a bad test case. 54 00:03:06,160 --> 00:03:07,000 So we're just gonna delete it. 55 00:03:08,500 --> 00:03:09,300 Okay. 56 00:03:09,300 --> 00:03:13,100 So, testing for exceptions though, as we've done here with assertRaises 57 00:03:13,100 --> 00:03:16,800 is a great way of making sure that your code fails like you're expecting it to, 58 00:03:16,800 --> 00:03:18,770 and like you want it to. 59 00:03:18,770 --> 00:03:23,570 Somewhat related to assertRaises are the assertWarns and assertLogs assertions. 60 00:03:23,570 --> 00:03:26,200 You can use these to make sure that warnings, or log entries, 61 00:03:26,200 --> 00:03:27,200 are created by your code.