1 00:00:00,830 --> 00:00:05,950 Visual Studio created one test method for each public method in the point class. 2 00:00:05,950 --> 00:00:09,780 But in reality, we'll usually have more than one test method for 3 00:00:09,780 --> 00:00:11,740 each method in the target. 4 00:00:11,740 --> 00:00:14,532 We treat each test method as a test case. 5 00:00:14,532 --> 00:00:18,338 For example, we created a test for the distance to method for 6 00:00:18,338 --> 00:00:22,200 the distance between (0, 0) and (3, 4). 7 00:00:22,200 --> 00:00:25,410 However this is just one possible case. 8 00:00:25,410 --> 00:00:28,630 There are countless other scenarios we could test. 9 00:00:28,630 --> 00:00:32,210 We can't possibly make tests for every combination of points. 10 00:00:32,210 --> 00:00:34,980 This is where our good judgment comes in. 11 00:00:34,980 --> 00:00:38,080 I find it best to break up the scenarios into groups and 12 00:00:38,080 --> 00:00:41,110 then write one or two tests for each group. 13 00:00:41,110 --> 00:00:43,452 If the test for a group of scenarios passes, 14 00:00:43,452 --> 00:00:47,839 then I can extrapolate that the code will work for all the scenarios in that group. 15 00:00:47,839 --> 00:00:52,393 For example one group of tests could be points that are horizontally aligned, 16 00:00:52,393 --> 00:00:55,620 another group are vertically aligned points. 17 00:00:55,620 --> 00:00:58,670 Another are points that are diagonal from each other. 18 00:00:58,670 --> 00:01:02,520 There's also the case where two points are in the same position. 19 00:01:02,520 --> 00:01:04,270 These are some of the basic cases. 20 00:01:05,400 --> 00:01:08,100 We also need to think about edge cases. 21 00:01:08,100 --> 00:01:12,130 Edge cases are cases when the input parameters are at the extreme 22 00:01:12,130 --> 00:01:13,620 end of what the code should accept. 23 00:01:13,620 --> 00:01:19,010 For example, we could test what happens when a point is passed in whose x and 24 00:01:19,010 --> 00:01:22,710 y values are the maximum values for what can be stored in an integer. 25 00:01:23,790 --> 00:01:27,390 In addition to edge cases we can have corner cases. 26 00:01:27,390 --> 00:01:31,030 Corner cases are cases that shouldn't normally happen but 27 00:01:31,030 --> 00:01:35,880 we need to make sure that the code handles it gracefully if it's expected to. 28 00:01:35,880 --> 00:01:41,260 Things like what happens if null is passed in or a point with negative x or y values. 29 00:01:42,370 --> 00:01:47,020 We often learn the most about our code when considering edge or corner cases. 30 00:01:47,020 --> 00:01:50,870 That said, we also need to use our judgement regarding the practicality of 31 00:01:50,870 --> 00:01:52,440 many of these tests. 32 00:01:52,440 --> 00:01:53,260 After all, 33 00:01:53,260 --> 00:01:57,960 distance too is a single line method that does a straightforward computation. 34 00:01:57,960 --> 00:02:02,540 Three or four tests should be enough to prove that it's implemented correctly. 35 00:02:02,540 --> 00:02:06,590 For example, we could assume that if the method is able to accurately compute 36 00:02:06,590 --> 00:02:10,720 the distance to a point where both the x and y values are different. 37 00:02:10,720 --> 00:02:15,130 Then it can also do so when only the x or y value are different. 38 00:02:15,130 --> 00:02:18,770 It comes down to our comfort level with making that assumption. 39 00:02:18,770 --> 00:02:20,330 Experience with testing helps too. 40 00:02:21,350 --> 00:02:24,860 At the very least, let's write another test that verifies that 41 00:02:24,860 --> 00:02:27,660 two points in the same location have a distance of zero. 42 00:02:28,750 --> 00:02:31,810 We'll write another test of the DistanceTo method. 43 00:02:31,810 --> 00:02:34,900 We'll need to name this something different than DistanceToTest 44 00:02:34,900 --> 00:02:37,380 since we already have a method with that name. 45 00:02:37,380 --> 00:02:39,410 We can name it whatever we want. 46 00:02:39,410 --> 00:02:41,990 There are a number of different conventions that folks 47 00:02:41,990 --> 00:02:44,030 use when creating test names. 48 00:02:44,030 --> 00:02:47,970 Since no one actually has to call these methods, we can make the names for 49 00:02:47,970 --> 00:02:50,700 test methods longer than we normally would. 50 00:02:50,700 --> 00:02:55,410 I prefer to name test methods so that they describe the case that they're testing. 51 00:02:55,410 --> 00:02:59,170 Another convention is to put the name of the method that's being tested first, 52 00:02:59,170 --> 00:03:02,460 followed by an underscore and the name of the test case. 53 00:03:02,460 --> 00:03:06,670 This is fine, many test cases are testing the behavior of the unit and 54 00:03:06,670 --> 00:03:09,960 not just the results of calling a single method though. 55 00:03:09,960 --> 00:03:14,490 The important thing is that test cases can be distinguished from each other based on 56 00:03:14,490 --> 00:03:17,050 what they're testing by looking at their name. 57 00:03:17,050 --> 00:03:20,925 So I'll name this new test DistanceToPointAtSamePosition. 58 00:03:21,940 --> 00:03:27,700 So I'll say Fact, then public void 59 00:03:27,700 --> 00:03:33,657 DistanceToPointAtSamePosition. 60 00:03:37,244 --> 00:03:40,825 For this test, let's just go ahead and copy what we already wrote for 61 00:03:40,825 --> 00:03:41,890 the previous test. 62 00:03:46,049 --> 00:03:49,390 We'll make both the point and the target the same place. 63 00:03:49,390 --> 00:03:57,381 So I'll say (3, 4) and the expected distance between them should be (0, 0). 64 00:03:57,381 --> 00:03:58,550 So there you go. 65 00:03:58,550 --> 00:04:00,670 That's all we need for this test. 66 00:04:00,670 --> 00:04:04,739 I'll change the name of the other method to something like DistanceTo, 67 00:04:07,004 --> 00:04:11,701 WithPythagoreanTriple, because 3, 4, and 68 00:04:11,701 --> 00:04:17,940 5 are whole numbers that form the sides of a right triangle. 69 00:04:17,940 --> 00:04:20,480 Otherwise known as a Pythagorean triple. 70 00:04:20,480 --> 00:04:23,600 We could write a couple more test cases for the Point class but 71 00:04:23,600 --> 00:04:25,360 I'll leave that up to you so we can move on.