美文网首页
python编程:从入门到实践 第11章课后题

python编程:从入门到实践 第11章课后题

作者: 梦vctor | 来源:发表于2018-09-20 20:15 被阅读0次
    image.png
    def city_function(City,Country):
        msg=City+","+Country
        return msg.title()
    
    import unittest #导入模块unittest
    from name_function import city_function
    class CityTest(unittest.TestCase):
        def test_city_country(self):
            msg=city_function('santiago','chile')
            self.assertEqual(msg,'Santiago,Chile')
    
    unittest.main()
    
    image.png
    #11-2
    def city_function(City,Country,population=''):
        if population:
            msg=City.title()+","+Country.title()+'-'+population
        else:
            msg=City.title()+","+Country.title()
        return msg
    
    import unittest #导入模块unittest
    from name_function import city_function
    class CityTest(unittest.TestCase):
        def test_city_country(self):
            msg=city_function('santiago','chile')
            self.assertEqual(msg,'Santiago,Chile')
        def test_city_country_population(self):
            a=city_function('santiago','chile','population=5000000')
            self.assertEqual(a,'Santiago,Chile-population=5000000')
    unittest.main()
    
    image.png
    #11-3
    class Employee():
        def __init__(self,first_name,last_name,salary):
            self.first_name=first_name
            self.last_name=last_name
            self.salary=salary
    
        def give_raise(self,add=5000):
            salary=int(self.salary)+add
            return int(salary)
    
    import unittest
    from name_function import Employee
    
    class TestEmployee(unittest.TestCase):
        def setUp(self):
            self.my_employee=Employee('zhang','san','8888')
    
        def test_give_default_raise(self):
            
            self.assertEqual(self.my_employee.give_raise(),13888)
    
        def test_give_custom_raise(self):
            
            self.assertEqual(self.my_employee.give_raise(6666),15554)
    
    unittest.main()
    

    相关文章

      网友评论

          本文标题:python编程:从入门到实践 第11章课后题

          本文链接:https://www.haomeiwen.com/subject/jryinftx.html