11-1
def get_formatted_name(city,country): '''函数返回一个格式为City, Country 的字符串''' full_name = city + country return full_name.title()
import unittest from city_functions import get_formatted_name class CityCountryTestCase(unittest.TestCase): def test_city_country(self): formatted_name =get_formatted_name('santiago ', 'chile') self.assertEqual(formatted_name,'Santiago Chile') unittest.main()
11-2
def get_formatted_name(city,country,population=''): full_name = city + country + '-population' + str(population) return full_name.title()
import unittest from city_functions import get_formatted_name class CityCountryTestCase(unittest.TestCase): def test_city_country(self): formatted_name =get_formatted_name('santiago ', 'chile','50000') self.assertEqual(formatted_name,'Santiago Chile-Population50000') unittest.main()
11-3
import unittest class Employee (): def __init__(self,first,last,salary): self.first = first self.last = last self.salary = salary def give_raise(self,salary=5000): self.salary+=salary class TesstEmployee (unittest. TestCase) : def setUp(self): self.emp = Employee('','',1000) def test_give_default_raise(self): self.emp.give_raise() self.assertEqual(self.emp.salary,6000) def test_give_custom_raise(self): self.emp.give_raise(6000) self.assertEqual(self.emp.salary,7000) unittest.main() 输出: .. ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK