《Python 程式設計:從入門到實踐》第十一章(測試程式碼)練習題答案
阿新 • • 發佈:2018-12-11
# -*- coding: gbk -*- def country_city(country,city,population=''): return(country.title()+","+city.title()+population) #11-2 import unittest from beifen import country_city class NamesTestCase(unittest.TestCase): def test_country_city(self): countrycity = country_city('china','beijing') self.assertEqual(countrycity,'China,Beijing') def test_country_city_population(self): countrycity = country_city('china','beijing','20') self.assertEqual(countrycity,'China,Beijing20') unittest.main() #11-3 import unittest from employee import Employee class SalaryTestCase(unittest.TestCase): def setUp(self): self.emp = Employee('li','xiaoming',10000) def test_give_defalult_raise(self): self.emp.give_raise() self.assertEqual(15000,self.emp.salary) def test_give_custom_raise(self): self.emp.give_raise(6000) self.assertEqual(16000,self.emp.salary) unittest.main() class Employee(): def __init__(self,first_name,last_name,salary): self.first_name = first_name self.last_name = last_name self.salary = salary def give_raise(self,salary=5000): self.salary+=salary