高级编程作业11章

11.1

#city_functions.py
def city_fun(city,country):
	_city = city +', '+ country
	return _city.title()
#test_city_country.py
impotr unittest
from city_functions import city_fun

class t_city_fun(unittset):
	def _city_fun(self):
		__city = city_fun('Santiago','chile')
		self.assertEqual(__city,'Santiago, Chile')

unittest.main()

11.2

#city_functions.py
def city_fun(city,country,population):
	_city = city.title() +', '+ country.title() + ' - population '+population
	return _city
import unittest
from city_functions import city_fun

class t_city_fun(unittest.TestCase):
	def test_city_fun(self):
		__city = city_fun('Santiago','chile','5000000')
		self.assertEqual(__city,'Santiago, Chile')


def city_fun(city,country,population = ''):
	if population:
		_city = city.title() +', '+ country.title() + ' - population '+population
	else:
		_city = city.title() +', '+ country.title()
	return _city
import unittest
from city_functions import city_fun

class t_city_fun(unittest.TestCase):
	def test_city_fun(self):
		__city = city_fun('Santiago','chile','5000000')
		self.assertEqual(__city,'Santiago, Chile - population 5000000')
unittest.main()

11.3

class Employee():
	def __init__(self,f_name,l_name,pay):
		self.f_name = f_name
		self.l_name = l_name
		self.pay = pay
	def give_raise(self,pay_ = 5000):
		self.pay = self.pay+pay_
import unittest
from employee import Employee

class Test_employee(unittest.TestCase):
	def setUp(self):
		self.employee = Employee("Xiaoming","Smith",5000)
	
	def test_give_default_raise(self):
		self.assertEqual(5000,self.employee.pay)
	def test_give_custom_raise(self):
		self.employee.give_raise(5001)
		self.assertEqual(10001,self.employee.pay)

unittest.main()


阅读更多

没有更多推荐了,返回首页