《Python编程-从入门到实践》第十一章习题训练

11-1城市和国家:

def city_function(city, country):
    r = city.title() + ', ' + country.title()
    return r
import unittest
from city_functions import city_function


class NameTestCase(unittest.TestCase):

    def test_city_country(self):
        test_case = city_function('santiago', 'chile')
        self.assertEqual(test_case, 'Santiago, Chile')


unittest.main()

11-3雇员:

class Employee:
    def __init__(self, first_name, last_name, salary):
        self.first_name = first_name
        self.last_name = last_name
        self.salary = int(salary)

    def give_raise(self, r = 5000):
        self.salary += r
import unittest
from employee import Employee

class TestEmployee(unittest.TestCase):

    def setUp(self):
        first_name = 'Daniel'
        last_name = 'Chung'
        salary = 10000
        self.myEmployee = Employee(first_name, last_name, salary)
        self.default_raise = 15000
        self.custom_raise = 20000

    def test_give_default_raise(self):
        self.myEmployee.give_raise()
        self.assertEqual(self.default_raise, self.myEmployee.salary)

    def test_give_custom_raise(self):
        self.myEmployee.give_raise(10000)
        self.assertEqual(self.custom_raise, self.myEmployee.salary)

unittest.main()


阅读更多

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