1 00:00:00,000 --> 00:00:04,929 [MUSIC] 2 00:00:04,929 --> 00:00:07,950 Let's write some unit tests for the point class. 3 00:00:07,950 --> 00:00:12,150 To do this we'll need to create a project to store our unit tests. 4 00:00:12,150 --> 00:00:14,920 Unit tests are contained in their own assembly, so 5 00:00:14,920 --> 00:00:19,220 that they're separate from the code of the application, or library that they test. 6 00:00:19,220 --> 00:00:22,180 This allows us to distribute or publish the application, 7 00:00:22,180 --> 00:00:24,780 without shipping the unit test with it. 8 00:00:24,780 --> 00:00:27,770 There are a number of ways we can create this project. 9 00:00:27,770 --> 00:00:31,280 The easiest is to use the extension we just installed. 10 00:00:31,280 --> 00:00:35,290 Using this extension we can have Visual Studio generate the project and 11 00:00:35,290 --> 00:00:39,250 have it create the scaffold code for testing the point class for us. 12 00:00:39,250 --> 00:00:42,930 We just right-click on the name of the class and click Create Unit Test. 13 00:00:47,100 --> 00:00:50,060 Make sure that the correct unit test framework is selected here. 14 00:00:51,080 --> 00:00:54,710 Because we previously installed xUnit.net test extensions. 15 00:00:54,710 --> 00:00:56,930 We can select xUnit.net. 16 00:00:56,930 --> 00:00:59,960 If we already had a unit test project set up. 17 00:00:59,960 --> 00:01:01,670 We could select it here. 18 00:01:01,670 --> 00:01:05,250 Instead, we'll keep this set to new test project. 19 00:01:05,250 --> 00:01:06,820 We can specify the names for 20 00:01:06,820 --> 00:01:12,060 the project, namespace, output file and test methods here. 21 00:01:12,060 --> 00:01:15,870 We'll keep the defaults as there are fairly standard ways to name these. 22 00:01:15,870 --> 00:01:19,480 Organizations or teams may have their own naming conventions, 23 00:01:19,480 --> 00:01:22,710 in that case you want to change these to match your teams. 24 00:01:22,710 --> 00:01:26,760 Finally we pick what we want to start out with in the test method body. 25 00:01:26,760 --> 00:01:31,190 I like to have them assert failure, we'll see what this means in a bit. 26 00:01:31,190 --> 00:01:36,020 Click on OK to have Visual Studio generate the unit test project and the scaffold for 27 00:01:36,020 --> 00:01:37,021 our unit tests for us. 28 00:01:38,790 --> 00:01:41,140 Let's take a look at what it generated. 29 00:01:41,140 --> 00:01:46,260 We now have another project in the solution named TreehouseDefense.Tests. 30 00:01:46,260 --> 00:01:48,390 This is our unit test project. 31 00:01:48,390 --> 00:01:50,310 The extension created the project and 32 00:01:50,310 --> 00:01:53,600 installed all the NuGet packages that are needed. 33 00:01:53,600 --> 00:01:55,910 Let's take a quick look at these packages. 34 00:01:55,910 --> 00:01:59,810 Just right click on the project and then click Managed NuGet packages. 35 00:02:01,060 --> 00:02:04,580 Here we see the NuGet packages that are needed for xUnit to work. 36 00:02:04,580 --> 00:02:07,350 This final one here is a test runner. 37 00:02:07,350 --> 00:02:12,200 Visual Studio uses this package to execute tests and get the results. 38 00:02:12,200 --> 00:02:15,810 There's also a console version of this package which can be used to execute 39 00:02:15,810 --> 00:02:17,290 tests from the console. 40 00:02:17,290 --> 00:02:18,150 Which is handy for 41 00:02:18,150 --> 00:02:22,310 when we don't want to have Visual Studio open in order to run our tests. 42 00:02:22,310 --> 00:02:25,420 It can be installed as a separate NuGet package. 43 00:02:25,420 --> 00:02:29,610 We didn't need to use the extension to set up our test project. 44 00:02:29,610 --> 00:02:32,300 The test project is just a C# class library. 45 00:02:32,300 --> 00:02:34,500 We could have created the project and 46 00:02:34,500 --> 00:02:37,720 downloaded the individual NuGet packages ourselves. 47 00:02:37,720 --> 00:02:41,090 I just find using the extension a lot more convenient. 48 00:02:41,090 --> 00:02:46,010 Inside the project that was generated, we have one file called point test. 49 00:02:46,010 --> 00:02:48,300 It contains the test class for the point class. 50 00:02:49,620 --> 00:02:53,220 A test method was created for each public method in the point class. 51 00:02:54,760 --> 00:02:57,280 This is where we write our test. 52 00:02:57,280 --> 00:03:01,630 Notice that above each test method there's an attribute named fact. 53 00:03:01,630 --> 00:03:04,630 You may not be familiar with attributes in C#. 54 00:03:04,630 --> 00:03:09,010 Attributes allow us to annotate the code with additional information. 55 00:03:09,010 --> 00:03:12,560 This information can be used by the compiler at runtime 56 00:03:12,560 --> 00:03:15,380 using a feature of .Net called reflection. 57 00:03:15,380 --> 00:03:19,650 In this case the attribute is used to tell the unit test runner that this method 58 00:03:19,650 --> 00:03:20,505 is a test method. 59 00:03:20,505 --> 00:03:25,212 MS test and N unit also use attributess to denote a test. 60 00:03:25,212 --> 00:03:28,019 MS tests names this attribute test method. 61 00:03:28,019 --> 00:03:30,576 And N unit simply names it test. 62 00:03:30,576 --> 00:03:32,310 But all three have the same effect. 63 00:03:32,310 --> 00:03:35,730 You can pass parameters to attributes, but we don't need to right now. 64 00:03:35,730 --> 00:03:39,010 Parentheses are optional if there are no parameters. 65 00:03:39,010 --> 00:03:43,080 Now our task is to write unit test cases for the point class. 66 00:03:43,080 --> 00:03:46,400 Let's start by testing that the constructor of the point class is 67 00:03:46,400 --> 00:03:47,810 coded correctly. 68 00:03:47,810 --> 00:03:51,670 We'll put this code in the first method named point test. 69 00:03:51,670 --> 00:03:52,920 When writing unit test, 70 00:03:52,920 --> 00:03:57,760 we follow a very simple pattern which is arrange, act, assert. 71 00:03:57,760 --> 00:04:02,450 First, we prepare to perform an operation by arranging our data. 72 00:04:02,450 --> 00:04:06,120 If we look at the point constructor, we'll see that it takes two parameters. 73 00:04:07,210 --> 00:04:09,345 We have x and y. 74 00:04:09,345 --> 00:04:12,870 In our test we'll need to decide what will pass the constructor. 75 00:04:14,400 --> 00:04:21,510 So I'll say int x = 5 and int y = 6. 76 00:04:23,620 --> 00:04:26,040 Now we need to execute the operation. 77 00:04:26,040 --> 00:04:28,440 This is the act part of the pattern. 78 00:04:28,440 --> 00:04:31,810 In this case we'll construct a point with these parameters. 79 00:04:31,810 --> 00:04:40,195 So I'll say var point = new Point and pass in x and y. 80 00:04:40,195 --> 00:04:42,152 Now, we need to verify that the x and 81 00:04:42,152 --> 00:04:45,325 y fields in the point object were set correctly. 82 00:04:45,325 --> 00:04:47,035 This is called an assertion. 83 00:04:47,035 --> 00:04:51,495 Because we are asserting that something is the way we expected to be. 84 00:04:51,495 --> 00:04:54,685 If the assertion fails, the test fails. 85 00:04:54,685 --> 00:04:56,940 Here we already have an assertion but 86 00:04:56,940 --> 00:04:59,905 we'll need to change it to something that makes sense for this test. 87 00:04:59,905 --> 00:05:06,719 So I'll say Assert.Equal(x), which is what we expect, 88 00:05:06,719 --> 00:05:12,940 and point.X, which is what was actually set. 89 00:05:12,940 --> 00:05:14,102 We'll also check Y. 90 00:05:18,998 --> 00:05:20,150 So there you go. 91 00:05:20,150 --> 00:05:21,680 There's the test. 92 00:05:21,680 --> 00:05:25,210 When this test passes, we can be sure that the fields of the class 93 00:05:25,210 --> 00:05:28,780 are initiated to the correct set of values when it's instantiated. 94 00:05:28,780 --> 00:05:32,270 You may be thinking this was a pretty dumb thing to test. 95 00:05:32,270 --> 00:05:36,282 And yes, a lot of unit tests, test dumb things like this. 96 00:05:36,282 --> 00:05:40,172 But a bunch of little dumb tests add up to ensuring that we have high quality 97 00:05:40,172 --> 00:05:42,170 software and peace of mind. 98 00:05:42,170 --> 00:05:44,680 Let's say when I was coding the point class, 99 00:05:44,680 --> 00:05:47,530 that I did something like this in the constructor. 100 00:05:51,615 --> 00:05:53,490 It was an honest mistake. 101 00:05:53,490 --> 00:05:56,340 X and Y are really common variable names. 102 00:05:56,340 --> 00:05:59,120 Especially when coding for 2D graphics. 103 00:05:59,120 --> 00:06:03,100 In reality, I get x and y mixed up all the time. 104 00:06:03,100 --> 00:06:05,600 Seriously, I mean all the time. 105 00:06:05,600 --> 00:06:10,080 And before I was religious about writing unit tests, I often wouldn't discover 106 00:06:10,080 --> 00:06:14,460 what I did until things started moving across the screen in the wrong direction. 107 00:06:14,460 --> 00:06:18,030 Then, I'd have to go track down where I went wrong. 108 00:06:18,030 --> 00:06:22,270 This meant inspecting all of the hundreds of x and y's in the program. 109 00:06:22,270 --> 00:06:24,410 It's enough to make someone go insane. 110 00:06:24,410 --> 00:06:25,750 I'm not kidding. 111 00:06:25,750 --> 00:06:28,780 Writing a dumb little test method like the one we just wrote 112 00:06:28,780 --> 00:06:31,080 is well worth the peace of mind that it brings.