1 00:00:07,880 --> 00:00:13,860 Welcome back. Testing a class definition involves creating instances, 2 00:00:13,860 --> 00:00:16,545 and invoking methods on those instances. 3 00:00:16,545 --> 00:00:21,105 Here's a little class that lets us define points on an XY plane. 4 00:00:21,105 --> 00:00:24,680 Instead of just treating him as two numbers or making a tuple, 5 00:00:24,680 --> 00:00:27,885 we're going to actually make an instance of a class. 6 00:00:27,885 --> 00:00:31,440 Each instance will represent one point with 7 00:00:31,440 --> 00:00:35,490 an attribute for its X position and another for its Y position. 8 00:00:35,490 --> 00:00:40,720 I'm going to draw a little grid again just so we have a place to plot our points. 9 00:00:51,170 --> 00:00:54,299 So, we can have a point like three, 10 00:00:54,299 --> 00:00:58,930 comma four, which would be plotted there. 11 00:01:00,220 --> 00:01:03,440 To test our definition of this class, 12 00:01:03,440 --> 00:01:06,665 we're really going to have to test the three methods that we have. 13 00:01:06,665 --> 00:01:10,895 We have a constructor, the underscore init. 14 00:01:10,895 --> 00:01:14,750 We've got the distance from origin, 15 00:01:14,750 --> 00:01:17,300 and we've got the mood method. 16 00:01:17,300 --> 00:01:20,150 Let's start by doing the constructor, 17 00:01:20,150 --> 00:01:23,390 because that's really the heart of any class. 18 00:01:23,600 --> 00:01:30,050 How do we test if we've implemented this to underscore init the constructor method? 19 00:01:30,050 --> 00:01:32,810 How do we test if we implemented it correctly? 20 00:01:32,810 --> 00:01:36,710 To figure that out, you got to remember the mechanics of classes, 21 00:01:36,710 --> 00:01:43,265 or basically we create a new point by invoking the class. 22 00:01:43,265 --> 00:01:47,555 When you call point on line 19, 23 00:01:47,555 --> 00:01:53,180 you will create a new instance of the point class and then behind the scenes, 24 00:01:53,180 --> 00:02:00,965 the constructor, the init method will be called with three getting passed as init X, 25 00:02:00,965 --> 00:02:02,900 four as init Y, 26 00:02:02,900 --> 00:02:07,510 and the new instance itself will be bound to the self parameter. 27 00:02:07,510 --> 00:02:14,120 Now, the purpose of the init method is to change the contents of self. 28 00:02:14,120 --> 00:02:16,790 Initially self has no attributes, 29 00:02:16,790 --> 00:02:19,550 but after we've finished executing the init, 30 00:02:19,550 --> 00:02:21,665 then self should have two attributes. 31 00:02:21,665 --> 00:02:25,475 It should have an X attribute and a Y attribute, 32 00:02:25,475 --> 00:02:26,750 and they should be bound to 33 00:02:26,750 --> 00:02:30,635 the corresponding values three and four that have been passed in. 34 00:02:30,635 --> 00:02:33,290 So, to test the class constructor, 35 00:02:33,290 --> 00:02:39,625 we create a point and then we check whether it has its attributes correctly set. 36 00:02:39,625 --> 00:02:42,345 Is it Y attribute set to four, 37 00:02:42,345 --> 00:02:46,000 is it X attribute set to three. 38 00:02:47,570 --> 00:02:51,350 Now usually, when we're doing a side-effect test, 39 00:02:51,350 --> 00:02:55,685 we would have to create an object like a list or a dictionary, 40 00:02:55,685 --> 00:02:56,990 and then invoke a function, 41 00:02:56,990 --> 00:03:00,410 and then see if the list or the dictionary has appropriately changed. 42 00:03:00,410 --> 00:03:02,600 In this case, we didn't have to do that, 43 00:03:02,600 --> 00:03:07,705 because the invocation of the point class, 44 00:03:07,705 --> 00:03:10,760 both creates the instance and invokes the method. 45 00:03:10,760 --> 00:03:13,220 So, those two are combined into one step. 46 00:03:13,220 --> 00:03:18,410 But then afterwards, we run our tests to see whether it came out right. 47 00:03:18,410 --> 00:03:21,830 By the way, if this is a little confusing for you 48 00:03:21,830 --> 00:03:27,260 because you're just coming from learning classes and the mechanics are a little hazy, 49 00:03:27,260 --> 00:03:29,900 running this in code lens on your own would be really 50 00:03:29,900 --> 00:03:33,860 helpful to make sure you understand what's going on. 51 00:03:33,860 --> 00:03:37,355 Now, the next method that I'm going to show you 52 00:03:37,355 --> 00:03:41,030 test for is the distance from origin method. 53 00:03:41,030 --> 00:03:44,060 You can see that that distance from origin method, 54 00:03:44,060 --> 00:03:52,000 what it's supposed to do is tell you how far away the point is from the origin. 55 00:03:52,000 --> 00:03:54,695 In this case, it's a distance of five. 56 00:03:54,695 --> 00:03:58,805 If we pick some other point like two two, 57 00:03:58,805 --> 00:04:02,065 you would have a different distance. 58 00:04:02,065 --> 00:04:06,990 But the three four point should have a distance away of two. 59 00:04:06,990 --> 00:04:09,295 So, in order to do a test, 60 00:04:09,295 --> 00:04:11,990 this is a method that just returns a value. 61 00:04:11,990 --> 00:04:15,010 It doesn't change the contents of the point. 62 00:04:15,010 --> 00:04:18,185 So, we should be doing a return value test. 63 00:04:18,185 --> 00:04:21,020 I'm just going to get rid of a few of these markings. 64 00:04:21,020 --> 00:04:24,920 So, testing the distance from origin method. 65 00:04:24,920 --> 00:04:28,085 This is a return value test. 66 00:04:28,085 --> 00:04:31,220 I still need to create a point in the first place. 67 00:04:31,220 --> 00:04:34,835 I could have actually just use the P that I defined on line 19. 68 00:04:34,835 --> 00:04:36,030 But when I'm defining a test, 69 00:04:36,030 --> 00:04:38,840 I'd like to have everything that's involved in the test right there. 70 00:04:38,840 --> 00:04:41,080 So, I'm recreating a new point P, 71 00:04:41,080 --> 00:04:43,005 that's at position three, four. 72 00:04:43,005 --> 00:04:47,455 Then I'm checking is at a distance five away from the origin. 73 00:04:47,455 --> 00:04:49,610 I should have some other tests as well. 74 00:04:49,610 --> 00:04:52,640 I should make some different points that are at different distances. 75 00:04:52,640 --> 00:04:54,920 I could check whether a point at two, 76 00:04:54,920 --> 00:04:58,575 zero is a distance two away from the origin. 77 00:04:58,575 --> 00:05:01,595 Finally, I've got this move method. 78 00:05:01,595 --> 00:05:03,215 Now this move method, 79 00:05:03,215 --> 00:05:06,150 what it's doing is taking a point, 80 00:05:06,340 --> 00:05:10,320 like this three, four point, 81 00:05:10,520 --> 00:05:13,560 and it's going to translate it. 82 00:05:13,560 --> 00:05:16,220 It's going to make it move some distance. 83 00:05:16,220 --> 00:05:19,400 So, we'll pass in to the move method, 84 00:05:19,400 --> 00:05:22,250 a distance to move in the X direction, 85 00:05:22,250 --> 00:05:25,530 and a distance to move in the Y direction. 86 00:05:25,930 --> 00:05:28,510 So in order to test that, 87 00:05:28,510 --> 00:05:32,120 that is a method whose purpose is to have a side effect. 88 00:05:32,120 --> 00:05:33,980 It's supposed to change the point. 89 00:05:33,980 --> 00:05:36,965 Therefore, I need to have a side effect test. 90 00:05:36,965 --> 00:05:40,685 So, I'm going to create tests here that again I 91 00:05:40,685 --> 00:05:44,155 create my initial point and then I move it. 92 00:05:44,155 --> 00:05:50,980 So, I'm moving it minus two in the X plane and buy up three in the Y. 93 00:05:51,260 --> 00:05:57,960 So, I'm going to go minus two from three over to one, 94 00:05:57,960 --> 00:06:01,270 and plus three in the Y. 95 00:06:01,490 --> 00:06:05,475 So, I should end up at one, seven. 96 00:06:05,475 --> 00:06:07,905 So, in my tests I'm checking. 97 00:06:07,905 --> 00:06:10,915 So first, I create the point that I move it, 98 00:06:10,915 --> 00:06:13,565 and then I see if it's correctly been moved. 99 00:06:13,565 --> 00:06:16,429 If my move method is correctly implemented, 100 00:06:16,429 --> 00:06:21,750 the X value should be one and the Y value should be seven. 101 00:06:21,750 --> 00:06:25,400 So in this case, we'll run 102 00:06:25,400 --> 00:06:29,285 our tests and we'll find out whether it's all correctly implemented. 103 00:06:29,285 --> 00:06:31,160 It is correctly implemented. 104 00:06:31,160 --> 00:06:36,380 If I had made some change or some of these methods weren't defined yet, 105 00:06:36,380 --> 00:06:38,120 then I would fail some of the tests. 106 00:06:38,120 --> 00:06:40,190 The purpose again of having these tests, 107 00:06:40,190 --> 00:06:44,510 is to check whether the implementation of the methods is correct. 108 00:06:44,510 --> 00:06:46,700 If the methods have been implemented correctly, 109 00:06:46,700 --> 00:06:48,410 you should pass all the tests. 110 00:06:48,410 --> 00:06:50,720 If they haven't been implemented correctly, 111 00:06:50,720 --> 00:06:52,720 then one or more of the test will fail. 112 00:06:52,720 --> 00:06:56,270 So, that's the skeleton of how to test a class definition. 113 00:06:56,270 --> 00:06:58,700 Basically, you need to assess for each method, 114 00:06:58,700 --> 00:07:02,380 whether it's purpose is to return a value or have a side effect. 115 00:07:02,380 --> 00:07:05,160 Testing the class constructor is special, 116 00:07:05,160 --> 00:07:08,430 because each test will invoke the class with some parameters, 117 00:07:08,430 --> 00:07:13,175 and then check whether the returned instance as its instance variables set correctly. 118 00:07:13,175 --> 00:07:14,780 For the other methods, 119 00:07:14,780 --> 00:07:17,060 you will also have to create an instance, 120 00:07:17,060 --> 00:07:19,250 but then you invoke them and either check whether 121 00:07:19,250 --> 00:07:21,890 the return value is correct or check whether 122 00:07:21,890 --> 00:07:28,050 the instance has had its variable to change appropriately. I'll see you next time.