1 00:00:00,510 --> 00:00:03,860 All the code that we need for writing unit tests is included in the Python 2 00:00:03,860 --> 00:00:06,850 standard library, in a module named Unit Test. 3 00:00:06,850 --> 00:00:08,720 We'll need to import it in each of our test files. 4 00:00:09,840 --> 00:00:12,870 The main structure that we're going to be creating is called a test case. 5 00:00:12,870 --> 00:00:16,320 A test case is a class that contains multiple methods, some of which are tests, 6 00:00:16,320 --> 00:00:18,500 and others that are just methods that you need. 7 00:00:18,500 --> 00:00:21,790 There are two special methods, Set Up and Tear Down, that are run before and 8 00:00:21,790 --> 00:00:23,240 after each test. 9 00:00:23,240 --> 00:00:25,860 But we'll look at those while we write our tests. 10 00:00:25,860 --> 00:00:27,250 Speaking of, let's get to that right now. 11 00:00:28,360 --> 00:00:32,610 Once upon a time, I gave the Treehouse Forums a challenge. 12 00:00:32,610 --> 00:00:37,358 I wanted everyone to build a Rock-Paper-Scissors style game, 13 00:00:37,358 --> 00:00:40,850 where you'd have three things or more. 14 00:00:40,850 --> 00:00:43,010 Each one is better and worse than some of the others. 15 00:00:44,340 --> 00:00:48,300 As a player, you get to pick one of them, and the computer would pick one. 16 00:00:48,300 --> 00:00:49,729 And then we, we see, okay, 17 00:00:49,729 --> 00:00:53,852 the one that you picked is better than the one the computer picked, or vice versa. 18 00:00:53,852 --> 00:00:56,773 And so, eventually you'd win, or you'd lose, or you'd tie. 19 00:00:56,773 --> 00:01:01,610 So this inside the rps folder here, this is my version of the game. 20 00:01:01,610 --> 00:01:06,630 And the game itself, the part where you pick the moves and everything like that, 21 00:01:06,630 --> 00:01:11,430 is a little tricky to test just by the basics of unit test. 22 00:01:11,430 --> 00:01:15,230 You need to know some stuff that we're not gonna actually cover in this course to be 23 00:01:15,230 --> 00:01:20,460 able to test this one easily, and there's probably also some clean up and 24 00:01:20,460 --> 00:01:22,240 refactoring I could do to make it easier for that, 25 00:01:22,240 --> 00:01:24,430 but we're not gonna worry about it. 26 00:01:24,430 --> 00:01:28,287 What is good, though, for testing, is this moves.py file. 27 00:01:29,432 --> 00:01:32,140 So, moves.py is the different moves you can take. 28 00:01:32,140 --> 00:01:32,800 You can take a rock. 29 00:01:32,800 --> 00:01:33,430 You can take paper. 30 00:01:33,430 --> 00:01:34,710 You can take scissors. 31 00:01:34,710 --> 00:01:38,410 And, this covers how they interact with each other. 32 00:01:38,410 --> 00:01:40,870 So, we're not quite ready to test this yet. 33 00:01:40,870 --> 00:01:43,020 That's probably gonna be the next video. 34 00:01:43,020 --> 00:01:46,830 We don't know about unit tests, because we haven't written any unit tests. 35 00:01:46,830 --> 00:01:50,310 So, let's actually write our first set of unit tests, and 36 00:01:50,310 --> 00:01:53,060 then we'll be well on our way to being able to do this. 37 00:01:53,060 --> 00:01:54,590 So we'll get a new file. 38 00:01:54,590 --> 00:02:00,900 And I'm gonna call this test.py, and what do I need to do in here? 39 00:02:00,900 --> 00:02:04,459 Well, first thing you need to do is bring in the unit test library, 40 00:02:04,459 --> 00:02:06,910 because that's what let's me write tests. 41 00:02:06,910 --> 00:02:08,790 So, import unittest. 42 00:02:10,400 --> 00:02:14,240 And then, since I want to test all the stuff here at moves.py, 43 00:02:14,240 --> 00:02:15,580 let's just import moves. 44 00:02:17,540 --> 00:02:19,610 Okay, so we're cool, we're good. 45 00:02:19,610 --> 00:02:23,640 Now, unlike doc tests, we're gonna write all of our tests in a separate file, 46 00:02:23,640 --> 00:02:27,900 we're not gonna write our tests in the file with our actual running code. 47 00:02:27,900 --> 00:02:33,190 To write our tests, we're gonna group our tests into what's called a test case. 48 00:02:34,220 --> 00:02:39,080 So, we're gonna make a new class and we're gonna call this MoveTests, and 49 00:02:39,080 --> 00:02:41,909 it's going to extend unittest.TestCase. 50 00:02:43,710 --> 00:02:48,420 Now, it's not required that you put the word tests into the name of the class for 51 00:02:48,420 --> 00:02:49,560 your test case. 52 00:02:49,560 --> 00:02:52,690 It's pretty common practice, and it kind of denotes, hey, these are the tests for 53 00:02:52,690 --> 00:02:53,890 this thing. 54 00:02:53,890 --> 00:02:57,750 But there is one naming convention we have to follow, and 55 00:02:57,750 --> 00:03:02,570 that is that our tests have to start with the word test. 56 00:03:02,570 --> 00:03:07,200 So, we'll say def, cuz we're defining a function, or rather, a method, and 57 00:03:07,200 --> 00:03:12,190 test, and just to get to grips with how unit test works, 58 00:03:12,190 --> 00:03:14,770 let's write two simple tests that are all about math. 59 00:03:15,920 --> 00:03:18,820 So first, let's do five plus five. 60 00:03:21,040 --> 00:03:22,020 Okay? 61 00:03:22,020 --> 00:03:22,900 So, 5 plus 5 is 10. 62 00:03:22,900 --> 00:03:24,580 Let's make sure that's true. 63 00:03:24,580 --> 00:03:26,802 We're gonna use the assert keyword, 64 00:03:26,802 --> 00:03:30,213 which says make sure that whatever comes after me is true. 65 00:03:30,213 --> 00:03:33,735 So, 5 plus 5 is equal to 10. 66 00:03:33,735 --> 00:03:34,394 Okay? 67 00:03:34,394 --> 00:03:35,090 Easy enough. 68 00:03:35,090 --> 00:03:35,990 We know that's true. 69 00:03:37,130 --> 00:03:37,840 Let's write one more. 70 00:03:37,840 --> 00:03:41,994 We'll do one plus one, which also extends self. 71 00:03:41,994 --> 00:03:46,033 And let's assert, all right one plus one equals three is not true. 72 00:03:46,033 --> 00:03:50,033 [BLANK_AUDIO] 73 00:03:50,033 --> 00:03:53,990 Right, assert not, so that's a swap. 74 00:03:53,990 --> 00:03:59,140 This is gonna come back as false, the not swaps it over to be true, 75 00:03:59,140 --> 00:04:02,230 and the assert goes hey, it's true, cool, I'm happy, okay? 76 00:04:03,570 --> 00:04:05,020 So, let's try running this. 77 00:04:05,020 --> 00:04:05,820 Let's see what we can do. 78 00:04:07,970 --> 00:04:12,891 So, I'm going to go into the rps directory, and 79 00:04:12,891 --> 00:04:17,590 I'm gonna do python -m unittest tests.py. 80 00:04:17,590 --> 00:04:22,580 So, just like when we ran doc tests, we specify the -m module for 81 00:04:22,580 --> 00:04:24,680 unit test, it knows how to find and run the tests. 82 00:04:27,030 --> 00:04:29,610 It ran two tests, we got two little dots. 83 00:04:29,610 --> 00:04:34,360 It took it basically no time at all, and everything's okay, everything passed. 84 00:04:34,360 --> 00:04:35,030 Great. 85 00:04:35,030 --> 00:04:36,320 That's amazing. 86 00:04:36,320 --> 00:04:39,740 Lots of times, though, so, I, I mentioned with doc tests, 87 00:04:39,740 --> 00:04:43,360 I don't usually want to run them every time I run the script, which is true. 88 00:04:43,360 --> 00:04:47,730 With unit test, though, I kinda wanna just be able to run tests.py, and 89 00:04:47,730 --> 00:04:48,780 it'll run the unit test for me. 90 00:04:49,890 --> 00:04:51,709 So, let's go add that in. 91 00:04:51,709 --> 00:04:53,067 We go down here to the bottom. 92 00:04:53,067 --> 00:04:57,703 [SOUND] And we'll add if 93 00:04:57,703 --> 00:05:02,339 name is equal to main, 94 00:05:02,339 --> 00:05:06,246 unittest.main. 95 00:05:06,246 --> 00:05:09,218 So, what this does is, if we run the file directly, 96 00:05:09,218 --> 00:05:11,259 run the unit tests that are in here. 97 00:05:11,259 --> 00:05:13,092 Now, let's try that out. 98 00:05:13,092 --> 00:05:17,400 But you should be able to do python tests.py, and we get the same output. 99 00:05:18,510 --> 00:05:20,620 Starting a test case is pretty straightforward, and 100 00:05:20,620 --> 00:05:23,540 it's really handy being able to run the tests through either the -m 101 00:05:23,540 --> 00:05:28,010 argument to our Python command, or with that if name equals main pattern. 102 00:05:28,010 --> 00:05:30,550 The test that we wrote all used the word assert, and 103 00:05:30,550 --> 00:05:32,430 a statement that we expected to be true. 104 00:05:32,430 --> 00:05:36,010 This works, but isn't always the most convenient way to word a test. 105 00:05:36,010 --> 00:05:38,880 In the next stage we'll look at several of the available assertions or 106 00:05:38,880 --> 00:05:40,830 testable conditions that test case gives us.