The author Eric Matthes is a high school science and math teacher living in Alaska, where he teaches an introductory Python course. He has been writing programs since he was five years old. Eric currently focuses on writing software that addresses inefficiencies in education and brings the benefits of open source software to the field of education. In his spare time, he enjoys climbing mountains and spending time with his family
OK Great! We now know that the function still works for names like Janis Joplin, and we can be confident that it will work for names like Wolfgang Amadeus Mozart as well.
id: 616caee4bd2472500647ac2b0d841c41 - page: 254
Testing Your Code 221 try It y ourse lf 11-1. City, Country: Write a function that accepts two parameters: a city name and a country name . The function should return a single string of the form City, Country, such as Santiago, Chile . Store the function in a module called city _functions.py . Create a file called test_cities.py that tests the function you just wrote (remember that you need to import unittest and the function you want to test) . Write a method called test_city_country() to verify that calling your function with values such as 'santiago' and 'chile' results in the correct string . Run test_cities.py, and make sure test_city_country() passes . 11-2. Population: Modify your function so it requires a third parameter, population . It should now return a single string of the form City, Country population xxx, such as Santiago, Chile population 5000000 . Run test_cities.py again . Make sure test_city_country() fails this time .
id: a7caa6e7c80bb2d8cae71f54881af9a0 - page: 254
Modify the function so the population parameter is optional . Run test_cities.py again, and make sure test_city_country() passes again . Write a second test called test_city_country_population() that verifies you can call your function with the values 'santiago', 'chile', and 'population=5000000' . Run test_cities.py again, and make sure this new test passes . testing a Class In the first part of this chapter, you wrote tests for a single function. Now youll write tests for a class. Youll use classes in many of your own programs, so its helpful to be able to prove that your classes work correctly. If you have passing tests for a class youre working on, you can be confident that improvements you make to the class wont accidentally break its current behavior.
id: 62ebf79e0b95cefeaea5e8243c650bca - page: 255
A Variety of Assert Methods Python provides a number of assert methods in the unittest.TestCase class. As mentioned earlier, assert methods test whether a condition you believe is true at a specific point in your code is indeed true. If the condition is true as expected, your assumption about how that part of your program behaves is confirmed; you can be confident that no errors exist. If the condition you assume is true is actually not true, Python raises an exception. Table 11-1 describes six commonly used assert methods. With these methods you can verify that returned values equal or dont equal expected values, that values are True or False, and that values are in or not in a given 222 Chapter 11 survey.py list. You can use these methods only in a class that inherits from unittest .TestCase, so lets look at how we can use one of these methods in the context of testing an actual class. Table 11-1: Assert Methods Available from the unittest Module Method Use
id: e9bd837bded9d8afe03c0acadcd05e4d - page: 255