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

2018-09-20  本文已影响0人  梦vctor
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()
上一篇下一篇

猜你喜欢

热点阅读