20.8. Unit Tests¶
Now that you have learned about classes and inheritance we can explain how to write your own unit tests.
20.8.1. Testing your Code¶
How do you test that your program worked? You may put in print statements about what you expect the output to be and compare that to what you actually get to see if your code is correct.
In fact in test-driven development you should write test cases before you start writing any code. That way you will know when your code works.
Run the code below to see what it prints.
This type of test is better than just printing out actual output because then you have to think about what the correct value should be. It lets you quickly compare the expected value and the actual value. However, you have to visually compare the two, which takes some time and you could miss something that doesn’t match.
Note
The tests above are using a f-string which is an easier way to create a string
with expression results in the string. Just add f
before the start of the string and {expression}
in the string.
Another way to write the code above would be to use a list comprehension. These are a quick way to return a new list by applying some logic to each element of the list.
result_list = [returned_value for item in list logic]
Run the code below to see what it prints.
20.8.2. Creating Test Cases¶
What types of tests should you create? You should create tests that check
that the function works for a variety of normal input. You should also think about how
to handle unusual input such as checking that total_even
works even for an empty list or
a list with only one item.
20.8.3. Understanding Unit Tests¶
Another way to test your code is to write unit tests. You have already been using hidden unit tests in this ebook to check your code as shown below. You probably quickly realized that you wanted all the tests to “pass” which is highlighted in green. This gives you an even quicker visual indication that your code is working than having to compare the expected output to the actual.
Let’s look at the unit tests that have usually been hidden.
Run the code below to see the unit tests again. See the unit tests after the code. The last unit test compares the result of recPerimeter(3.0, 2)
to wrong value. Fix the value so that all of the tests pass.
Look at the unit tests above and answer the questions below.
Run the code below to see what the unit tests do and then add another test to test the result when x is higher than y.
Fix the function below to return the middle characters from the passed string.
If the string has an odd length then return the middle character. If the string has an even
length return the two middle characters. For example, get_middle('abc')
returns
'b'
and get_middle('abcd')
returns 'bc'
. Also add tests cases to
test the result when str
has only one or two characters in it.
Add tests to check the result when str
has one or two letters.
Note
Unit tests in this ebook must include the line from unittest.gui import TestCaseGui
and inherit from TestCaseGui
. Outside of this
ebook you should include the line import unittest
and inherit from unittest.TestCase
.
Unit tests inherit from a class that includes several methods. As you can see
from the code above one of the methods is assertEqual
which returns the result of a == b
.
-
Q-8: Drag each method to what it returns.
Read this page and try again.
- assertTrue(x)
- x == True
- assertIn(a, b)
- a in b
- assertIsInstance(a, b)
- isinstance(a, b)
- assertEqual(a, b)
- a == b
- assertNotEqual(a, b)
- a != b
- assertFalse(x)
- x == False
Note
All of the assert methods can take a third parameter which is a message (string) that explains what was tested.
20.8.4. Writing Unit Tests¶
To write a unit test you need to do the following.
Import the library (
from unittest.gui import TestCaseGui
in this ebook andimport unittest
elsewhere)Create a class that inherits from the correct class (
TestCaseGui
in this ebook andunittest.TestCase
elsewhere )Optionally create a
setUp
method which is called before every test method is run.Create one or more methods that start with
test
to test different things in your codeTest one or more results using
assert
methods such asself.assertEqual
Optionally create a
tearDown
method which is called after every test method is runRun the tests using
ClassName.main()
in the ebook and otherwiseunittest.main()
Note
The order that the tests run is not the order they are defined.
Fix the code below to run and notice the test results.
Modify the code above to add a method in the Car
class to set (change) the model name and add a test method to test that the new method works.
Put the code in order to create tests for a Person class initials method that returns the first letter
from the first name followed by the first letter of the last name. The Person class initializer
takes the person’s first name and last name. First define a setUp
method to
create two Person objects and then define the test_initials
method.