python第十一章习题

11-1

代码:

import unittest

def city_country(city,country):
	return city.title() + ", " + country.title()
class cityTestCase(unittest.TestCase):

	def test_city_country(self):
		name = city_country("beijing","China")
		self.assertEqual(name,"Beijing, China")

unittest.main()

输出结果:

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

11-3

代码:

import unittest

class Employee():

	def __init__(self,xing,name,money):
		self.xing = xing
		self.name = name
		self.money = money

	def give_raise(self,raise_money = 5000):
		self.money += raise_money

class EmployeeTestCase(unittest.TestCase):

	def setUp(self):
		self.employee = Employee("Wu","zesheng",100000)
		self.employee2 = Employee("Wu","zhixuan",10000)
		self.my_raise_money = 10000

	def test_give_default_raise(self):
		self.employee.give_raise()
		self.assertEqual(105000,self.employee.money)

	def test_give_custom_raise(self):
		self.employee2.give_raise(self.my_raise_money)
		self.assertEqual(20000,self.employee2.money)

unittest.main()

输出结果:

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK


阅读更多
想对作者说点什么? 我来说一句

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