Chapter Eleven
11-1 City and Country: Write a function that accepts two parameters: a city name and a country name. This function returns a string in the format City, Country, eg Santiago, Chile . Store
this function in a module called city_functions.py.
Create a program called test_cities.py to test the function you just wrote (don't forget, you need to import the module unittest and the function you want to test). Write a
method called test_city_country( ) and verify that the strings you get when you call the preceding functions with values like 'santiago' and 'chile' are correct. Run test_cities.py to verify that the
test test_city_country( ) passes.
11-2 Population: Modify the previous function to include the third mandatory parameter population, and return a string in the format City, Country - population xxx,
such as Santiago, Chile - population 5000000. Run test_cities.py and verify that the test test_city_country( ) fails.
Modify the above function to make the parameter population optional. Run test_cities.py again to confirm that test_city_country( ) passes again.
Write another test called test_city_country_population( ) to verify that it can be called with values like ' santiago' , ' chile' and ' population=5000000 '
this function. Run test_cities.py again to confirm that the test test_city_country_population( ) passes.
11-1
module city_functions.py
def get_formatted_name(city,country): ''' function returns a string formatted as City, Country ''' full_name = city + country return full_name.title()
test_cities.py
import unittest from city_functions import get_city_country class CityCountryTestCase (unittest.TestCase): ''' test city_functions.py ''' def test_city_country(self): ''' When calling the preceding function with values like 'santiago' and 'chile' ''' formatted_name =get_city_country( ' santiago ' , ' chile ' ) self.assertEqual(formatted_name, ' Santiago Chile ' ) # It's a dog, the case is wrong here unittest.main()
result:
11-2
module city_functions.py
def get_city_country(city,country,population): ''' function returns a string in the format City, Country ''' full_name =city + country + ' -population ' + str(population) return full_name.title()
test_cities.py
import unittest from city_functions import get_city_country class CityCountryTestCase (unittest.TestCase): ''' test city_functions.py ''' def test_city_country(self): ''' When calling the preceding function with values like 'santiago' and 'chile' ''' formatted_name =get_city_country( ' santiago ' , ' chile ' , 5000000 ) self.assertEqual(formatted_name,'Santiago Chile-Population 5000000') unittest.main() #The flawed part I capitalized Population here, but it is not actually capitalized, so according to others, it should be defined without full_name in city_functions.py
result:
11-3 Employee: Write a class called Employee whose method __init__( ) accepts first name, last name, and annual salary, and stores them in properties. Write a method called give_raise( ) that defaults to an
annual salary increase of $5,000, but can accept other annual salary increases as well.
Write a test case for Employee with two test methods: test_give_default_raise( ) and test_give_custom_raise( ). Use the method setUp( ) to avoid
creating a new instance of employee in each test method. Run this test case and confirm that both tests pass.
11-3
import unittest class Employee (): """ Collect employee information """ def __init__ (self,first,last,salary): ''' Store personal information in attributes ''' self.first = first self.last = last self.salary = salary def give_raise(self): ''' the method of annual salary increase ''' self.salary = 5000 return self.salary class TEmployee (unittest.TestCase) : ''' create a test Employee instance ''' def setUp(self): self.first = ' wang ' self.last = ' shi ' self.salary = int(300 ) def test_give_default_raise(self): """ Test whether employee salary is default 300 """ self.assertEqual(self.Employee. salary, 300 ) def test_give_custom_raise(self): """ Test whether the salary increase value is 5000 """ self.assertEqual(self.Employee.give_raise(), 5000 ) unittest.main()
result: