第十一次作业

第十一章部分习题

课本:《Python编程 从入门到实践》

环境:Python 3.6.2

下面只写部分习题(大多都十分简单)

代码:

#11-1
import unittest
def fun1(city,country):
    return city+','+country


class CitiesTest(unittest.TestCase):
    def test_city_country(self):
        result=fun1("santiago","chile")
        self.assertEqual(result,"santiago,chile")

print("#11-1")
unittest.main()

#11-2
import unittest
def fun1(city,country,population):
    return city+','+country+' - population '+str(population)


class CitiesTest(unittest.TestCase):
    def test_city_country(self):
        result=fun1("santiago","chile",5000000)
        self.assertEqual(result,"santiago,chile")

print("#11-2")
unittest.main()

#11-3
import unittest
class Employee():
	def __init__(self,first_name,second_name,salary):
		self.first_name=first_name
		self.second_name=second_name
		self.salary=salary

	def give_raise(self,increase=5000):
		self.salary+=increase


class TestEmployee(unittest.TestCase):
	"""docstring for TestEmployee"""
	def setUp(self):
		self.first_name="Tom"
		self.second_name="Black"
		self.salary=5000

	def test_give_default_raise(self):
		result=Employee(self.first_name,self.second_name,self.salary)
		result.give_raise()
		self.assertEqual(result.salary,self.salary+5000)

	def test_give_custom_raise(self):
		result=Employee(self.first_name,self.second_name,self.salary)
		result.give_raise(6000)
		self.assertEqual(result.salary,self.salary+6000)
		

print("#11-3")
unittest.main()

结果:




猜你喜欢

转载自blog.csdn.net/weixin_39367127/article/details/79868465