Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The Python standard library includes a unit test module that's very powerful and very flexible and is also very easy to use. Unit tests are valuable for a few reasons. Unit tests are automated tests that you can run on your code and you can write these tests once and save them and run them over and over. So if you have a set of code that's got a lifetime to it, that over the course of its lifetime it gets updated, it gets changed, having these unit tests already written and being able to run these unit tests on your code is going to have some real value for you.
So let's look at how this is done in Python. I've got the saytime module here, which has been used for a number of different purposes in this course. And you'll notice at the end of it it's got a little test function. And when I wrote this, this just seemed like the easiest way to do it. And it's also, I like to be able to look at the results and say yeah, that looks right. That looks right. That looks right. If this were anymore complicated or if it was going to have much of a development lifecycle to it, I would use unit tests instead.
And so we're going to go ahead and do that. This is a unittest script that I wrote for saytime. And I basically took those textual tests and I made them into unit tests. And this is very, very easy to do. All I do is I import saytime and I import the unittest. So this is the module that I'm working on and this is the unittest framework from the Python standard library. And then I create this class.
And then at the end of the module here, if __name == "__main__", instead of just calling my own main, I call a special main that's in the unittest package. And in the unittest package, the special main will actually open up this file and parse it and find the classes that import unittest, and it will go ahead and run the test in that class. And so here is the class. So I can name it whatever I want. And it has a special method called setUp. In this special method setUp, I can do any initializations that I want to.
And so here I've created this list of numbers. I could have just as easily put that in the test for the numbers, but I wanted to demonstrate the setUp here. And so I take a range of 11 numbers, from 0-10, and I make a list out of them, and I assign them to this variable here. And so that gets used in test_numbers. So here is the first method. These methods will get run as the tests. So the first method here is test_numbers. And I've got some words that I'm going to compare those numbers again. And these are the words that will be the results of my saywords numwords.
So 'oh', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', those are the results that I'm expecting. And so I enumerate through the self.nums. I get both an index and the number itself. And then I run this assertEqual method, which is from the unittest package, and it's going to compare two values. And if these two values are equal, then fine. It will leave it alone. And if they're not, then it will throw an AssertionError. And so here the two values are the result from my saytime.numwords and the word from this tuple here.
If they equal, then fine. And if they're not equal, then it'll throw an AssertionError. And so the same thing here, test_time. I have time_tuples. And so this is a tuple with a bunch of tuples in it. And these are the times that I'm testing against. And these are hours and minutes. And then here's the list of the results that I want, "midnight", "one past midnight", "eleven o'clock", etcetera. And if they compare equal, it's the same thing. I enumerate through the time_tuples. I run them through the module that I'm testing and I compare them to this tuple up here of these words.
If they're equal, fine. If they're not equal, it throws an AssertionError. So this is how this works. And obviously you can write your own tests. There are a number of different assertion tests. You can find those in the documentation for the unittest module. I strongly recommend that you read the documentation for the unittest package. It's got a lot of options. It has a lot of methods that you can use for your testing. There are a lot of different assertions that you can test for. And it even has some ways to do some more complicated tests.
This kind of test that I've written here is the simplest case. And for most purposes, this is actually just fine, and it does the job and it does the job well. So let's go ahead and run it. When we run this, you'll notice that we get our little test. And we've been just selecting Python Run for all of these. There's a special one here in the PyDev module for Eclipse. It's called Python unit-test. And we're going to run that one. Say OK. And here it ran our unittest. We'll maximize this so we can see what it does.
First of all, it finds files. And it finds the test-saytime. And that's our unittest module that we just wrote. And it imports the test module. So it's importing the saywords. And here it runs the two tests, test_numbers and test_time. It ran the two tests. It didn't throw any assertion errors. And there's the result that we're expecting. If it had not gotten the result that it wanted, let's just go ahead and misspell one of these words here. I'll just throw an extra o in there. We'll save that and we'll run it.
And now we got one failure. You see it got an AssertionError that oh did not equal to ooh. So it gives us enough information here that we can actually go through the code and we can say, oh, well, I've got a typo in my assertion test or there's an actual problem in my module that I need to go and fix. So the beauty of this is, is that as your code goes through its development lifecycle, you make one release. You make another release. You make another release. You add features. You take features away. You change features.
Your unit-tests are already written. And so you can accumulate more. You can change them. And this is going to help you to keep the quality of your code high throughout its development lifecycle. So unit-tests are very useful. The unit-test package that comes in the standard Python library is very complete and very well written and very reliable. If you're going to be releasing your code using Python, you must submit unit-tests with it. And of course the unit-tests are going to be of value to you in any code that has a lengthy product lifecycle.
Get unlimited access to all courses for just $25/month.Become a member