In the last exercise, we saw how to check for equality between two values in the unittest framework using the
.assertEqual method of the
TestCase class. The framework relies on built-in assert methods instead of
assert statements to track results without actually raising any exceptions. Specific assert methods take arguments instead of a condition, and like assert statements, they can take an optional message argument.
Let’s go over three commonly used assert methods for testing equality and membership, their general syntax, and their
assert statement equivalents.
assertEqual()method takes two values as arguments and checks that they are equal. If they are not, the test fails.self.assertEqual(value1, value2)
assertIn()method takes two arguments. It checks that the first argument is found in the second argument, which should be a container. If it is not found in the container, the test fails.self.assertIn(value, container)
assertTrue()method takes a single argument and checks that the argument evaluates to
True. If it does not evaluate to
True, the test fails.self.assertTrue(value)
assert statements would be the following:
The full list for equality and membership can be seen in the Python documentation. Let’s put these methods into practice!
Small World Air planes are equipped with an on-board entertainment system which we need to create some tests for. Take some time to review
Note the three functions in
get_daily_movie(): returns the movie of the day.
get_licensed_movies(): returns a list of licensed movies the plane can play.
get_wifi_status(): returns the current wifi status of the plane.
Note the two test cases in
test_movie_license(): is intended to test if a daily movie is licensed
test_wifi_status(): is intended to test if the wifi is currently active
Run the code to proceed.
Every flight has a free movie. We want to create a test that checks our database to make sure that the uploaded movie has a valid license, or else we could pay some hefty fines.
Inside of our
test_movie_license() test method, we have two variables defined:
daily_movie: which stores the value of the current free daily movie.
licensed_movies: which stores the value of all the current licensed movies.
To test if we have a license for the current daily movie, we need to compare if
daily_movie exists inside of
self.assertIn() assert method inside of
test_movie_license() to check if the
daily_movie is licensed.
Our entertainment system also provides WiFi as a purchase option for passengers. We want to make a test that ensures the WiFi is enabled.
Inside the test method called
test_wifi_status() we have a variable called
wifi_enabled which stores the boolean value of whether the wifi is turned on or not.
self.assertTrue() assert method inside of
test_wifi_status() to test that
True. The test should fail because currently wifi is disabled.