Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

unittest Example

00:00 As you continue along in this course, you’re going to start to use unit tests. I wanted to make a quick diversion away from mocking in general, and just go into a basic unit test example.

00:15 So this is what a very basic unit test might look like. At the top of the file, we have an import of the unittest module, and this comes built into Python—you don’t have to install it.

00:29 It is typical to name your tests starting with test_. And usually, you store these in a tests/ directory where you have a whole bunch of tests; this is just an example of one unit test.

00:44 I have the function, a very simple function called hello(). I have it in the same file as the unit test. This is not really typical, but usually you would be importing functions from other modules outside of your tests. It’s a good practice to keep your functions—your business logic, if you will—separate from your unit tests, but in this example, I just want to show a basic “How unittest works,” so I have my function in the same file as my test. The function is hello(), it takes in a parameter, and there’s just one little subtle thing here is that if the name is 'Maria',

01:23 we’re going to say 'Yo, Maria'. If it’s any other name, we’ll just say f'Hello, {name}'. All right? So to actually write a unit test, after importing the unittest module you create a class, and you should start it with a capital T, Test.

01:42 So, Test with a capital T and then whatever comes after that is kind of arbitrary. I’m just calling it Example.

01:49 And then this inherits from the unittest.TestCase class, and then inside this class, you create functions for each unit test. So you define a function, and it’s typical to start the name of the function with test_, so .test_hello_return_value().

02:11 I’m testing the return value of the hello() function and it should be a string. Here I’ve set result equal to the return value of hello() with a name of 'Aleah'. So since the name is not 'Maria', it should return 'Hello, Aleah'.

02:28 And this is a string, so the way I’m testing that is I’m using this .assertIsInstance() method. And since we are in the class and we’re inheriting from unittest.TestCase, we need to pass in self to our method.

02:48 And we’re saying self.assertIsInstance() of the result

02:53 is it an instance of a str (string)? So this should be True. And then I have another test that is testing the specific use case of 'Maria'.

03:04 So this time the result is hello() with 'Maria' passed in. And now I want to say self.assertTrue(result.startswith('Yo')).

03:15 So basically I’m saying that if 'Maria' is passed in, then the result should start with the string 'Yo', not 'Hello'.

03:24 Now, there are a bunch of these assert methods. Here’s .assertIsInstance(), where you could check the data type of the return of the function. There’s .assertTrue(), there’s .assertFalse().

03:34 You can assert exceptions. I highly recommend reading the documentation from the unittest module, and they explain and list all these different assert methods you can use.

03:45 But I wrote two unit tests inside the TestExample class, and then to run the tests, I’m going to open up the terminal

03:55 and I have this file called test_example in this directory, and the way I can run unit tests is by saying python 3 -m, for module, and I’m going to use the unittest module, and then you can pass the name of your test file, so this is test_example.py, And then you press Enter and it runs all the tests inside this TestClass that start with test_.

04:23 These both passed. It says Ran 2 tests, we have two dots here, and then the OK at the bottom. If we change this and let’s say instead of 'Maria' we just say 'Tom', so the return should not start with 'Yo', so this should fail. We can run the test again.

04:47 And this time we see that it Ran 2 tests and we had one failure.

04:54 If we inspect—I’m just going to bring this up a little higher so you can see it.

05:02 So, the first dot represents success, so the first test passed. The second one has an F for failure, and then it just shows why it failed.

05:11 So, this .assertTrue() was not True, and False is not true, so we got a failure. So, this is an example of a unit test.

05:21 You’re going to see this coming up in the next section of the course. Yeah, so hopefully this was helpful for you in understanding a typical Python unit test file.

Become a Member to join the conversation.