Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
The `unittest` library has many different assertions but some are more commonly used than others. Let's look at how to check for membership, what class a variable is an instance of, and a couple of others.
New Terms
assertIn(x, y) - Make sure x
is a member of y
(this is like the in
keyword)
assertIsInstance(x, y) - Make sure x
is an instance of the y
class
assertGreaterEqual(x, y) - Make sure x
is greater than or equal to y
assertLessEqual(x, y) - Make sure x
is less than or equal to y
If you've been writing Python for a while, you've probably gotten pretty used to 0:00 using in key word the for checking membership. 0:03 You want to know if a certain value is in a list or dictionary. 0:06 Lots of time we want to know if this is true when we're writing tests too. 0:09 So unit test gives us assert in and assert not in. 0:12 There are a couple of other handy lists, [UNKNOWN] and dictionary assertions, too. 0:17 Let's take a look at them. 0:20 For our next set of tests, we're going to use a couple of new classes that I wrote 0:22 they're here in this dice dot pie file inside the dice directory. 0:26 And, so these represent a die roller, like you might use for a table top game or 0:31 a role playing game. 0:36 You can make a roll, where is that class? 0:38 There's the class. 0:40 You can make a roll of dice with a string like one d six or 0:41 two d six or five d eight, whatever. 0:47 And it'll create how many dice with however many sides and 0:50 it rolls them to get a number. 0:54 So, I'm going to test a few things in this module. 0:56 The first thing I want to test is whether or not a die up here is created correctly. 0:59 So, let's set up a new file here that we're gonna call test.py. 1:04 And we need to to import unittest and we need to import dice so 1:13 we have everything set up, and lets do a class DieTests. 1:20 Unittest.TestCase, and we're gonna use the setup method. 1:24 So def setUp, and I'm gonna create just a couple of dice directly. 1:33 I'm gonna make a d6, which is dice.Die with six sides. 1:40 And I'm gonna make a d8, which is dice.Die with eight sides. 1:45 Okay. So now, let's test the creation of a die. 1:52 So they've been created and 2:00 I should now be able to do self.assertEqual(self.d6.sides, 6). 2:03 The number six should have six sides. 2:09 And I just do self.assertIn(self.d6.value, 2:13 range(1, 7) because remember with range, we have to specify a higher end 2:19 that's one higher than the number we actually want as the highest end. 2:24 So I've got everything in here. 2:29 Now, it should be impossible for 2:31 this test to fail because these, this is exactly how we're creating the dice. 2:32 It should just work. 2:37 But it's always good to have tests like this so that if your die creation, 2:38 your whatever your object is, the creation changes. 2:42 Your tests will warn you about it. 2:45 And then you can either fix your tests or 2:47 you can fix your code, whichever one is now out of date. 2:49 Okay. 2:52 So I'm not gonna bother with the if name main thing. 2:53 I'm just gonna run the tests. 2:55 So python -m unittest. 2:57 Ope. 3:02 I need to get into that directory. 3:03 Dice. There we go. 3:06 Python -m unittest tests.py. 3:07 Ran one test, test was fine. 3:13 All right. 3:14 I also want to test that I can add multiple dice together. 3:16 So, let's write a quick little test for that. 3:21 So, def test_add(self) : 3:25 self.assertIsInstance(self.d6+self.d8, int). 3:29 So what does IsInstance does? 3:36 So, IsInstance is like the IsInstance function, in, 3:41 the shell, which says tell me if this variable is an instance of this class. 3:46 Well, assertIsInstance works the same way, but it's an assertion. 3:51 So run this insertion, make sure that the first item, this one right here, 3:55 is a member of this class an int or an instance of this class and end. 4:02 And then we're good, it comes back as either true or false. 4:07 Okay? 4:11 So let's also before we go any further in this. 4:12 Let's write some tests for the roll class. 4:18 So, RollTests(unittest.TestCase) 4:21 : I want to make a couple other things here as well. 4:28 So that they are always around. 4:32 So self.hand1 = dice.Roll('1d2') because 4:35 the smallest die we can have is a two-sided die. 4:39 And self.hand3 = dice.Roll('3D6') okay. 4:44 So hand one, cause there's one die in it. 4:52 Hand three, cause there's three dice in it. 4:54 And I want to be able to test the upper and 4:57 lower boundaries of these total rolls, right? 4:59 So these are gonna be a lot like the ones we did on the rock paper scissors game. 5:05 But instead of the greater and less, we're gonna use assert greater equal or 5:08 assert less equal so that we don't have to add on that extra number. 5:12 So anyway, let's do test_lower(self) : and we're going to do 5:17 self.assertGreaterEqual(int(self.hand3), 3). 5:22 Cuz we have three dice. 5:31 The lowest number on each of them is one. 5:32 So the lowest that those three dice can add up to is three. 5:34 And then let's also do [SOUND] test_upper(self) 5:39 : self.assertLessEqual(int(self.hand3), 5:45 18) because three six highest dice, 5:51 six is the highest number for each one, 5:55 six plus six plus six is 18, or less if they're not there. 5:59 So we can do this int call. 6:06 Because we defined the dunder nth method in our class. 6:09 This is great because if we ever change our class to where we can't make an nth of 6:13 it, then this is gonna immediately blow up. 6:17 And we're gonna go, oh, this doesn't work right. 6:20 Now our class also has a dunder contains method. 6:23 And our die class has a dunder EQ method that's defined. 6:28 So we can write a test to make sure that as given die is inside of a given hand. 6:32 So let's test that out too. 6:40 So let's do test_membership(self) : and we need to make a couple of things here. 6:43 So let's do our test_die = dice.Die(2) two sides. 6:49 And test_die.value = self.hand1.results(0).value so 6:56 we're making a fake die. 7:04 We're giving it the same number as whatever's in hand one. 7:07 And then we're gonna do self.assertIn(test_die, 7:11 self.hand1.results) So, we're just making sure that 7:15 a die with the value of the first or only die in hand1 is in hand1. 7:21 We're cheating a little bit, but it's okay. 7:27 There are some ways around this. 7:31 We could do mocking and stuff like that. 7:32 But that's not important at this point. 7:34 Okay, let's run these tests. 7:36 So we come back down here and we run our tests again. 7:39 All five tests pass, everything's good. 7:44 So this is actually really good. 7:47 It covers most of the functionality in our two classes that we'd want to test. 7:48 We do have a couple of try accept blocks we want to test, but 7:53 we'll cover those in the next video. 7:56 I love working with simple assertions like this. 7:59 They give me a lot more confidence about what's going on in my code, because I can 8:01 make sure that my collection objects are holding exactly what I expect them to. 8:04
You need to sign up for Treehouse in order to download course files.
Sign up