P192
#11-1
#city_functions.py
def place(city,country):
"""生成城市和国家"""
full_name = city+', '+country
return full_name.title()
import unittest
from city_functions import place
class Testplace(unittest.TestCase):
"""测试city_functions.py"""
def test_city_country(self):
"""能够正确处理像Santiago, Chile这样的地点吗?"""
place_name = place('Santiago','Chile')
self.assertEqual(place_name,"Santiago, Chile")
unittest.main()
#11-2(1)
#city_functions2.py
def place(city,country,population):
"""生成城市 国家和人口数"""
full_name = city+', '+country+' - population '+population
return full_name.title()
import unittest
from city_functions2 import place
class Testplace(unittest.TestCase):
"""测试city_functions.py"""
def test_city_country(self):
"""能够正确处理像Santiago, Chile这样的地点吗?"""
place_name = place('Santiago','Chile')
self.assertEqual(place_name,"Santiago, Chile")
unittest.main()
#11-2(2)
#city_functions3.py
def place(city,country,population = ''):
"""生成城市 国家和人口数"""
if population:
full_name = city+', '+country+' - '
full__name = full_name.title()+population
return full__name
else:
full_name = city+', '+country
return full_name.title()
import unittest
from city_functions3 import place
class Testplace(unittest.TestCase):
"""测试city_functions.py"""
def test_city_country(self):
"""能够正确处理像Santiago, Chile这样的地点吗?"""
place_name = place('Santiago','Chile')
self.assertEqual(place_name,"Santiago, Chile")
unittest.main()
#11-2(3)
#city_functions3.py
def place(city,country,population = ''):
"""生成城市 国家和人口数"""
if population:
full_name = city+', '+country+' - '
full__name = full_name.title()+population
return full__name
else:
full_name = city+', '+country
return full_name.title()
import unittest
from city_functions3 import place
class Testplace(unittest.TestCase):
"""测试city_functions.py"""
def test_city_country(self):
"""能够正确处理像Santiago, Chile这样的地点吗?"""
place_name = place('santiago','chile')
self.assertEqual(place_name,"Santiago, Chile")
def test_city_country_population(self):
msg = place('santiago','chile','population = 5000000')
self.assertEqual(msg,"Santiago, Chile - population = 5000000")
unittest.main()
P198 11-3
第一种方法
#eply.py
class Employee():
def __init__(self,first_name,last_name,salary):
self.first_name = first_name
self.last_name = last_name
self.salary = int(salary)
def give_raise(self,add_salary = 5000):
self.salary += add_salary
import unittest
from eply import Employee
class TestEmployee(unittest.TestCase):
"""针对Employee的测试"""
def setUp(self):
"""创建雇员实例"""
self.employee = Employee('chen','xie','1000')
def test_give_default_raise(self):
self.employee.give_raise()
self.assertEqual(self.employee.salary,6000)
def test_give_custom_raise(self):
self.employee.give_raise(3000)
self.assertEqual(self.employee.salary,4000)
unittest.main()
第二种方法
#eply1.py
class Employee():
def __init__(self,first_name,last_name,salary):
self.first_name = first_name
self.last_name = last_name
self.salary = int(salary)
self.Raise = 5000
def give_raise(self):
self.salary += self.Raise
return self.salary
import unittest
from eply1 import Employee
class TestEmployee(unittest.TestCase):
"""针对Employee的测试"""
def setUp(self):
"""创建雇员实例"""
self.employee = Employee('chen','xie','1000')
self.my_salary = [5000,2000]
def test_give_default_raise(self):
self.assertEqual(self.employee.give_raise(),6000)
def test_give_custom_raise(self):
self.employee.Raise = self.my_salary[1]
self.assertEqual(self.employee.give_raise(),3000)
unittest.main()