《Python 编程:从入门到实践》第十一章(测试代码)练习题答案

# -*- 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

猜你喜欢

转载自blog.csdn.net/zenmehaichonga_2009/article/details/82817213