《Python编程——从入门到实践》第十一章部分习题解

# 11-1.
city_functions.py
def city(city, country):
	return(city.title() + ', ' + country.title())
test_cities.py
import unittest
from city_functions import city

class TestCase(unittest.TestCase):
	def test_city_country(self):
		string_city = city('santiago', 'chile')
		self.assertEqual(string_city, 'Santiago, Chile')
		
unittest.main()
# 11-3
employee.py
class Employee():
	def __init__(self, name, first_name, salaries):
		self.name = name
		self.first_name = first_name
		self.salaries = salaries
	
	def give_raise(self, salary = None):
		if salary == None:
			self.salaries += 5000
		else:
			self.salaries += salary

test.py
import unittest
from employee import Employee

class TestEmployee(unittest.TestCase):
	def setUp(self):
		self.default_raise = Employee('Mike', 'James', 50000)
		self.custom_raise = 2000
	
	def test_give_default_raise(self):
		self.assertEqual(self.default_raise.salaries, 50000)
		
	def test_give_custom_raise(self):
		self.default_raise.give_raise(self.custom_raise)
		self.assertEqual(self.default_raise.salaries, 50000 + self.custom_raise)
		
unittest.main()


你可能感兴趣的:(作业)