第十一次作业

xiaoxiao2021-02-28  33

第十一章部分习题

课本:《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()

结果:

转载请注明原文地址: https://www.6miu.com/read-2624558.html

最新回复(0)