11-1:
city_functions.py
def country_city(city,country): #Accept city and country #return city,country result=city+','+country return result
test_cities.py
import unittest from city_functions import country_city class testCityFunctions(unittest.TestCase): def test_city_country(self): result=country_city('Shanghai','China') self.assertEqual(result,'Shanghai,China') unittest.main()
operation result:
11-2:
city_functions.py
def country_city(city,country,population): #Accept city and country and population #return city,country-population xxx result=city+','+country+'-population '+population return result
test_cities.py same as above
operation result:
Modified city_functions.py
def country_city(city,country,population=''): #Accept city and country and population #return city,country-population xxx if population: result=city+','+country+'-population '+population else: result=city+','+country return result
operation result
Modified test_cities.py
import unittest from city_functions import country_city class testCityFunctions(unittest.TestCase): # def test_city_country(self): # result=country_city('Shanghai','China') # self.assertEqual(result,'Shanghai,China') def test_city_country_population(self): result=country_city('Shanghai','China','250') self.assertEqual(result,'Shanghai,China-population 250') unittest.main()
operation result:
11-3:
employee.py
class Employee(): def __init__(self,first_name,last_name,salary): self.first_name=first_name self.last_name=last_name self.salary=salary def salary_raise(self,raise_money=5000): self.salary+=raise_money
test_employee.py:
import unittest from employee import Employee class TestEmployee(unittest.TestCase): def setUp(self): self.my_employee=Employee('Zhan','Jianzhou',1000000) def test_default_salary_raise(self): self.my_employee.salary_raise() self.assertEqual(self.my_employee.salary,1005000) def test_not_default_salary_raise(self): self.my_employee.salary_raise(100000) self.assertEqual(self.my_employee.salary,1100000) unittest.main()