Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Your First unittest Test Case5:41 with Kenneth Love
Most of the power of testing in Python comes from the `unittest` framework and it's `TestCase` class. Let's look at starting a `TestCase` and writing a couple of simple tests.
unittest - Python's library for writing tests
TestCase - A collection of tests
python -m unittest tests.py
In a script
Remember, all tests in a
TestCase have to start with the word
test_ to be run. You can have methods that don't start with
test_ for other purposes if you need them.
All the code that we need for writing unit tests is included in the Python 0:00 standard library, in a module named Unit Test. 0:03 We'll need to import it in each of our test files. 0:06 The main structure that we're going to be creating is called a test case. 0:09 A test case is a class that contains multiple methods, some of which are tests, 0:12 and others that are just methods that you need. 0:16 There are two special methods, Set Up and Tear Down, that are run before and 0:18 after each test. 0:21 But we'll look at those while we write our tests. 0:23 Speaking of, let's get to that right now. 0:25 Once upon a time, I gave the Treehouse Forums a challenge. 0:28 I wanted everyone to build a Rock-Paper-Scissors style game, 0:32 where you'd have three things or more. 0:37 Each one is better and worse than some of the others. 0:40 As a player, you get to pick one of them, and the computer would pick one. 0:44 And then we, we see, okay, 0:48 the one that you picked is better than the one the computer picked, or vice versa. 0:49 And so, eventually you'd win, or you'd lose, or you'd tie. 0:53 So this inside the rps folder here, this is my version of the game. 0:56 And the game itself, the part where you pick the moves and everything like that, 1:01 is a little tricky to test just by the basics of unit test. 1:06 You need to know some stuff that we're not gonna actually cover in this course to be 1:11 able to test this one easily, and there's probably also some clean up and 1:15 refactoring I could do to make it easier for that, 1:20 but we're not gonna worry about it. 1:22 What is good, though, for testing, is this moves.py file. 1:24 So, moves.py is the different moves you can take. 1:29 You can take a rock. 1:32 You can take paper. 1:32 You can take scissors. 1:33 And, this covers how they interact with each other. 1:34 So, we're not quite ready to test this yet. 1:38 That's probably gonna be the next video. 1:40 We don't know about unit tests, because we haven't written any unit tests. 1:43 So, let's actually write our first set of unit tests, and 1:46 then we'll be well on our way to being able to do this. 1:50 So we'll get a new file. 1:53 And I'm gonna call this test.py, and what do I need to do in here? 1:54 Well, first thing you need to do is bring in the unit test library, 2:00 because that's what let's me write tests. 2:04 So, import unittest. 2:06 And then, since I want to test all the stuff here at moves.py, 2:10 let's just import moves. 2:14 Okay, so we're cool, we're good. 2:17 Now, unlike doc tests, we're gonna write all of our tests in a separate file, 2:19 we're not gonna write our tests in the file with our actual running code. 2:23 To write our tests, we're gonna group our tests into what's called a test case. 2:27 So, we're gonna make a new class and we're gonna call this MoveTests, and 2:34 it's going to extend unittest.TestCase. 2:39 Now, it's not required that you put the word tests into the name of the class for 2:43 your test case. 2:48 It's pretty common practice, and it kind of denotes, hey, these are the tests for 2:49 this thing. 2:52 But there is one naming convention we have to follow, and 2:53 that is that our tests have to start with the word test. 2:57 So, we'll say def, cuz we're defining a function, or rather, a method, and 3:02 test, and just to get to grips with how unit test works, 3:07 let's write two simple tests that are all about math. 3:12 So first, let's do five plus five. 3:15 Okay? 3:21 So, 5 plus 5 is 10. 3:22 Let's make sure that's true. 3:22 We're gonna use the assert keyword, 3:24 which says make sure that whatever comes after me is true. 3:26 So, 5 plus 5 is equal to 10. 3:30 Okay? 3:33 Easy enough. 3:34 We know that's true. 3:35 Let's write one more. 3:37 We'll do one plus one, which also extends self. 3:37 And let's assert, all right one plus one equals three is not true. 3:41 [BLANK_AUDIO] 3:46 Right, assert not, so that's a swap. 3:50 This is gonna come back as false, the not swaps it over to be true, 3:53 and the assert goes hey, it's true, cool, I'm happy, okay? 3:59 So, let's try running this. 4:03 Let's see what we can do. 4:05 So, I'm going to go into the rps directory, and 4:07 I'm gonna do python -m unittest tests.py. 4:12 So, just like when we ran doc tests, we specify the -m module for 4:17 unit test, it knows how to find and run the tests. 4:22 It ran two tests, we got two little dots. 4:27 It took it basically no time at all, and everything's okay, everything passed. 4:29 Great. 4:34 That's amazing. 4:35 Lots of times, though, so, I, I mentioned with doc tests, 4:36 I don't usually want to run them every time I run the script, which is true. 4:39 With unit test, though, I kinda wanna just be able to run tests.py, and 4:43 it'll run the unit test for me. 4:47 So, let's go add that in. 4:49 We go down here to the bottom. 4:51 [SOUND] And we'll add if 4:53 name is equal to main, 4:57 unittest.main. 5:02 So, what this does is, if we run the file directly, 5:06 run the unit tests that are in here. 5:09 Now, let's try that out. 5:11 But you should be able to do python tests.py, and we get the same output. 5:13 Starting a test case is pretty straightforward, and 5:18 it's really handy being able to run the tests through either the -m 5:20 argument to our Python command, or with that if name equals main pattern. 5:23 The test that we wrote all used the word assert, and 5:28 a statement that we expected to be true. 5:30 This works, but isn't always the most convenient way to word a test. 5:32 In the next stage we'll look at several of the available assertions or 5:36 testable conditions that test case gives us. 5:38
You need to sign up for Treehouse in order to download course files.Sign up