Intermediate Python

Practice: Unit Testing

Intermediate Python

Check out a free preview of the full Intermediate Python course

The "Practice: Unit Testing" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina live-codes the testing practice that reviews unit test principles that were covered in the last few sections.

Preview
Close

Transcript from the "Practice: Unit Testing" Lesson

[00:00:05]
>> Nina Zakharenko: In this exercise on unit tests, we're gonna use the built-in Python unit test library to write test classes and test cases, and on our unit tests and make sure that they pass. So by now, you're an expert in Python unit testing, right? We wrote our first unit test, we used this exercise to use the unit test library in the Python standard library.

[00:00:34]
We didn't have to install anything extra, use any other frameworks. It's all available to us right here.
>> Nina Zakharenko: So let's go to VSCode. I'm gonna make a new file. And I'm gonna call it divisible.py.
>> Nina Zakharenko: And I'm gonna define a new function here, and I'm gonna call it divisible_by.

[00:00:58]
And checking a number, the number that I wanna check, and then the divisor.
>> Nina Zakharenko: And I'm gonna return, if check_number % divisor,
>> Nina Zakharenko: == 0. So if the number that I'm checking is 8 and I'm doing a % 2, it will be equal to 0 because 8 is an even number.

[00:01:30]
>> Nina Zakharenko: Let's make another new file. This time I'm gonna save it as test_divisible.py.
>> Nina Zakharenko: Okay.
>> Nina Zakharenko: And this is where we'll write our unit tests. So we want to import unittest. We also want to import the code that we're gonna test in a new file. So I'm gonna import, I'm gonna say from divisible,

[00:02:12]
>> Nina Zakharenko: So the name of our file. I'm gonna import the divisible_by method. I'm gonna write a new class, and I'm gonna call it DivisibleTestCase. What does it need to inherit from in order to be a valid test case?
>> Student: unitTest.TestCase.
>> Nina Zakharenko: That's right, unittest.TestCase.
>> Nina Zakharenko: And now I can go ahead and start writing some tests in here.

[00:02:43]
>> Nina Zakharenko: Remember, the test has to start with test_ or it's not going to be run. So I'm gonna say test_divisible_by, I'm gonna pass in self. I'm gonna use the assertTrue assertion. That if I call divisible_by with the arguments of 10 and 2, it should be true, and,
>> Nina Zakharenko: Let's say I want to test that assert true, that divisible_by for 10 and 3 is also true.

[00:03:27]
And I'm gonna put in a, if __name__ == '__main__', just run this unit test. Call the main method on the unittest module here. And it knows how to do the rest. So let me use the command pallet, run the Python file on my terminal, and I got an error here.

[00:03:53]
It says, AssertionError: False is not true. So this is a good opportunity for me to take a look and say, is it my code that's not correct or is it my test that's not correct? Got a failure. Well, it's actually my test that's not correct, right? 10 is not divisible by 3.

[00:04:10]
So I'm gonna change this to false and run the Python file in the repl again. And now I should see that one test ran and,
>> Nina Zakharenko: One test, or one test ran and it running successfully. I can also run,
>> Nina Zakharenko: This with the -v flag. Or if you pass in two dashes, that would be the dash for both flag.

[00:04:38]
And that will tell me exactly which test ran and which test case.
>> Nina Zakharenko: The nice thing here is I can have multiple test cases, multiple tests, and I don't have to do anything special besides for this. If I didn't have this check for a main method here, remember that I could run python -m unittest and pass in the name of the module.

[00:05:09]
So there's no .py here. Oops, I'm sorry, divisible.
>> Nina Zakharenko: There's no .py here. We're passing in the name of the module, which in this case is the name of the file. Ran our test successfully, awesome.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now