WEBVTT 1 00:00:00.070 --> 00:00:01.580 line:15% Hi guys and welcome back. 2 00:00:01.580 --> 00:00:04.420 line:15% In this video, we're going to be testing our first function 3 00:00:04.420 --> 00:00:07.600 line:15% and it is this divide function here, really simple function. 4 00:00:07.600 --> 00:00:09.720 All it does is it takes two parameters, 5 00:00:09.720 --> 00:00:13.520 dividend and divisor, and divides one by the other. 6 00:00:13.520 --> 00:00:15.050 We're going to be doing a little bit more 7 00:00:15.050 --> 00:00:18.580 in this function later on, but for now, it's just this. 8 00:00:18.580 --> 00:00:20.360 I've also added the typing module here 9 00:00:20.360 --> 00:00:22.500 to say that the dividend and the divisor 10 00:00:22.500 --> 00:00:24.883 are both int or floats. 11 00:00:25.990 --> 00:00:28.410 Let's go over to our test functions file 12 00:00:28.410 --> 00:00:30.320 to begin writing our tests. 13 00:00:30.320 --> 00:00:33.550 It is important that your file is called test_ 14 00:00:33.550 --> 00:00:35.870 and then, the name because that's what unittest 15 00:00:35.870 --> 00:00:39.020 is going to look at in its default configuration. 16 00:00:39.020 --> 00:00:40.370 You can change that later on, 17 00:00:40.370 --> 00:00:42.893 but we're not going to look at how in this course. 18 00:00:43.840 --> 00:00:48.720 The first thing to do here is to import the test case class 19 00:00:48.720 --> 00:00:53.163 from unit test, and also, of course, our divide function. 20 00:00:54.530 --> 00:00:57.350 Then, we're going to create a class test functions, 21 00:00:57.350 --> 00:01:02.350 for example, and it's going to inherit from test case. 22 00:01:02.630 --> 00:01:06.013 Inside here, we can write our first function. 23 00:01:06.990 --> 00:01:09.303 Functions should also start with test_, 24 00:01:11.000 --> 00:01:14.388 for example, test_divide_result. 25 00:01:14.388 --> 00:01:18.290 This test is just going to check that the divide function 26 00:01:18.290 --> 00:01:21.800 returns what we would expect a divine function to return 27 00:01:21.800 --> 00:01:23.753 with two normal values. 28 00:01:25.010 --> 00:01:28.200 So, we'll say that the dividend is going to be 15, 29 00:01:28.200 --> 00:01:30.490 the divisor is going to be three, 30 00:01:30.490 --> 00:01:33.230 And the expected result is going to be 5.0. 31 00:01:34.510 --> 00:01:35.950 These are just variables. 32 00:01:35.950 --> 00:01:38.770 They don't have any specific meaning in our tests. 33 00:01:38.770 --> 00:01:39.990 Even though they have the same names 34 00:01:39.990 --> 00:01:42.700 as the function parameters, it's totally irrelevant. 35 00:01:42.700 --> 00:01:44.530 You can name these whatever you want. 36 00:01:44.530 --> 00:01:45.840 What is important, though, 37 00:01:45.840 --> 00:01:49.940 is that you do self.assertEqual(divide) 38 00:01:52.980 --> 00:01:55.840 of the dividend and the divisor 39 00:01:55.840 --> 00:01:57.300 and then, the expected_result. 40 00:01:57.300 --> 00:02:00.000 So what we're doing here is we're passing to 41 00:02:00.000 --> 00:02:03.180 this assertEqual method two values, 42 00:02:03.180 --> 00:02:07.480 divide or the result of executing the divide function 43 00:02:07.480 --> 00:02:09.860 and our expected_result. 44 00:02:09.860 --> 00:02:13.080 And it's just gonna check that they are the same, 45 00:02:13.080 --> 00:02:16.940 so that dividing 15 by three is equal to 5.0. 46 00:02:18.080 --> 00:02:20.150 We have written our first test. 47 00:02:20.150 --> 00:02:22.350 Now, if you're using the default configuration, 48 00:02:22.350 --> 00:02:24.930 i.e. your file is called test_ 49 00:02:24.930 --> 00:02:28.810 and your tests are called test_ and then, the name, 50 00:02:28.810 --> 00:02:30.460 you can right click test_functions 51 00:02:30.460 --> 00:02:33.160 using PyCharm and run the tests. 52 00:02:33.160 --> 00:02:34.480 If you are using the terminal, 53 00:02:34.480 --> 00:02:36.410 we will have instructions on how to run these tests 54 00:02:36.410 --> 00:02:40.530 on the terminal in a little info box. 55 00:02:40.530 --> 00:02:43.730 So let's press run and then, you see that you get 56 00:02:43.730 --> 00:02:48.240 ran one test in .002 seconds and it is okay. 57 00:02:48.240 --> 00:02:50.360 No tests failed. 58 00:02:50.360 --> 00:02:53.290 Whenever you're writing a test it's very important 59 00:02:53.290 --> 00:02:56.240 that once you've written the test that you're happy with, 60 00:02:56.240 --> 00:02:58.000 you play around with it to make sure 61 00:02:58.000 --> 00:02:59.170 that it is actually working. 62 00:02:59.170 --> 00:03:01.870 So now I'm gonna change the expected result of 4.0 63 00:03:01.870 --> 00:03:04.533 and when I press play, this test should fail. 64 00:03:05.770 --> 00:03:07.320 And it does, which is good 65 00:03:07.320 --> 00:03:09.520 and it says here the expected result is 5.0. 66 00:03:11.611 --> 00:03:14.430 At this point, let me say that, in some cases, 67 00:03:14.430 --> 00:03:16.660 when you make a division in any programming language, 68 00:03:16.660 --> 00:03:21.000 the result may not be exact, so it could be 5.00000001 69 00:03:23.000 --> 00:03:24.990 just because of how the computers do division 70 00:03:24.990 --> 00:03:26.760 and we have some links in the resources section 71 00:03:26.760 --> 00:03:29.280 of this lecture to explain why that happens. 72 00:03:29.280 --> 00:03:33.300 But if the division was not exact, 73 00:03:33.300 --> 00:03:35.640 that is okay if you use a slightly different method. 74 00:03:35.640 --> 00:03:38.000 So, instead of self.assertEqual, 75 00:03:38.000 --> 00:03:40.630 whenever you're handling division, I always recommend 76 00:03:40.630 --> 00:03:42.443 you do self.assertAlmostEqual. 77 00:03:44.930 --> 00:03:47.340 And then, the last argument after divide 78 00:03:47.340 --> 00:03:49.373 and expected_result is a delta, 79 00:03:51.000 --> 00:03:54.430 which is what the allowed difference 80 00:03:54.430 --> 00:03:56.580 between these two values is. 81 00:03:56.580 --> 00:03:59.720 That is just in case there is a small inconsistency 82 00:03:59.720 --> 00:04:02.060 in the division there, that this will still work. 83 00:04:02.060 --> 00:04:04.250 So now that we've done this, let's press play again, 84 00:04:04.250 --> 00:04:06.240 and the tests pass. 85 00:04:06.240 --> 00:04:08.210 By the way, down here on the bottom left, 86 00:04:08.210 --> 00:04:11.500 there is a small icon that says Toggle auto-test, 87 00:04:11.500 --> 00:04:13.140 which is something you can click on PyCharm 88 00:04:13.140 --> 00:04:14.410 and whenever you make a code change, 89 00:04:14.410 --> 00:04:16.130 the tests will run automatically. 90 00:04:16.130 --> 00:04:18.270 I usually leave it on when I'm writing tests 91 00:04:18.270 --> 00:04:20.430 and I have my code, like an application. 92 00:04:20.430 --> 00:04:21.680 For now, we're gonna leave it off, 93 00:04:21.680 --> 00:04:23.040 but it is there If you want it. 94 00:04:23.040 --> 00:04:24.740 It can be helpful. 95 00:04:24.740 --> 00:04:26.430 Let's begin writing our second test, 96 00:04:26.430 --> 00:04:30.410 which is for a different equivalence class. 97 00:04:30.410 --> 00:04:32.870 We're gonna talk later on what equivalence classes 98 00:04:32.870 --> 00:04:35.820 and boundaries are when you're talking about testing, 99 00:04:35.820 --> 00:04:38.200 but essentially, you wanna start thinking about 100 00:04:38.200 --> 00:04:40.330 what different types of values are 101 00:04:40.330 --> 00:04:42.240 that your functions can take. 102 00:04:42.240 --> 00:04:44.640 For example, this function in this test, 103 00:04:44.640 --> 00:04:48.600 we've tried with two positive, non-zero numbers. 104 00:04:48.600 --> 00:04:51.080 I would call this one equivalence class. 105 00:04:51.080 --> 00:04:52.810 These two numbers are similar. 106 00:04:52.810 --> 00:04:55.080 You could, instead of 15, tried 30. 107 00:04:55.080 --> 00:04:56.860 Instead of three, you could type 10, 108 00:04:56.860 --> 00:04:59.950 but you would expect the function to work in the same way 109 00:04:59.950 --> 00:05:02.880 with 15 or 30 or 300. 110 00:05:02.880 --> 00:05:05.700 However, it may work differently 111 00:05:05.700 --> 00:05:07.940 if you put in a negative number. 112 00:05:07.940 --> 00:05:10.830 And that is what I would call a different equivalence class, 113 00:05:10.830 --> 00:05:12.810 a different type of input. 114 00:05:12.810 --> 00:05:16.610 So we will write a test for negative division 115 00:05:18.130 --> 00:05:20.650 and here we'll say that the dividend is 15, 116 00:05:20.650 --> 00:05:25.650 the divisor is -3 and the expected result is -5.0. 117 00:05:27.710 --> 00:05:30.800 And then, we will do exactly the same as before. 118 00:05:30.800 --> 00:05:32.830 We will just test that. 119 00:05:32.830 --> 00:05:35.970 Calling the divide function with these two arguments 120 00:05:35.970 --> 00:05:39.273 will result in this or it will almost result in that. 121 00:05:40.590 --> 00:05:43.793 Pressing play gives us an okay result once again. 122 00:05:45.070 --> 00:05:47.110 Finally, we can try a third equivalence class, 123 00:05:47.110 --> 00:05:49.260 which is for a zero number. 124 00:05:49.260 --> 00:05:53.313 So, we will say test_divide_dividend is zero. 125 00:05:54.830 --> 00:05:58.360 We will set the dividend to zero and the divisor to five 126 00:05:58.360 --> 00:06:02.170 and then, the expected_result should be zero. 127 00:06:02.170 --> 00:06:05.370 Whenever you divide zero by anything, it should be zero. 128 00:06:05.370 --> 00:06:08.030 And now, we're going to say self.assertEqual 129 00:06:08.030 --> 00:06:10.610 because I don't expect the output 130 00:06:10.610 --> 00:06:12.550 to be zero point something, 131 00:06:12.550 --> 00:06:14.820 so I don't wanna do self.assertAlmost Equal. 132 00:06:14.820 --> 00:06:16.220 I wanna do self.assertEqual. 133 00:06:19.130 --> 00:06:23.150 Pressing play again and there we have our three tests. 134 00:06:23.150 --> 00:06:27.120 You can see that writing tests can be very straightforward 135 00:06:27.120 --> 00:06:29.670 when your code is very straightforward. 136 00:06:29.670 --> 00:06:33.210 Here, we've got a simple function that doesn't do a lot 137 00:06:33.210 --> 00:06:35.450 and we still have to write three tests for it, 138 00:06:35.450 --> 00:06:37.523 but they're all very simple tests. 139 00:06:38.380 --> 00:06:41.350 In a simple test, which is a test that I like most, 140 00:06:41.350 --> 00:06:45.310 you have your inputs and your outputs defining the test 141 00:06:45.310 --> 00:06:47.600 and then, you have a single check 142 00:06:47.600 --> 00:06:50.370 or a single assert at the end. 143 00:06:50.370 --> 00:06:52.990 There is a benefit to doing tests in this way, 144 00:06:52.990 --> 00:06:57.330 which is that it's really clear what the function does. 145 00:06:57.330 --> 00:06:59.730 The function should be able to divide. 146 00:06:59.730 --> 00:07:01.890 It should able to handle negative results 147 00:07:01.890 --> 00:07:04.320 and they should be able to handle a dividend of zero 148 00:07:04.320 --> 00:07:05.720 without giving you an error. 149 00:07:06.640 --> 00:07:08.260 We will be adding more tests 150 00:07:08.260 --> 00:07:10.570 and also other functions in the coming videos, 151 00:07:10.570 --> 00:07:14.490 but try always to keep functions or classes, 152 00:07:14.490 --> 00:07:17.730 if you're doing that, simple and your tests simple 153 00:07:17.730 --> 00:07:19.970 and straightforward, as well. 154 00:07:19.970 --> 00:07:21.330 Let's move on to the next video. 155 00:07:21.330 --> 00:07:22.280 I'll see you there.