hw6(第六周)

# [11-1, 11-2]
import unittest
# 11-1, 11-2
def city_country(city, country):
    return city + ', ' + country

def city_country2(city, country, population):
    return city + ', ' + country + ' - population ' + str(population)

def city_country3(city, country, population=5000000):
    return city + ', ' + country + ' - population ' + str(population)

class Test(unittest.TestCase):
    def test_city_country(self):
        self.assertEqual(city_country('Santiago', 'Chile'), 'Santiago, Chile')

    def test_city_country2(self):
        self.assertEqual(city_country2('Santiago', 'Chile'), 'Santiago, Chile - population 5000000')

    def test_city_country3(self):
        self.assertEqual(city_country3('Santiago', 'Chile'), 'Santiago, Chile - population 5000000')

    def test_city_country_population(self):
        self.assertEqual(city_country3('Santiago', 'Chile', population=5000000), 'Santiago, Chile - population 5000000')

unittest.main()

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

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

class TestEmployee(unittest.TestCase):
    def setUp(self):
        self.employee = Employee('A', 'B', 1000)
        self.employee2 = Employee('C', 'D', 1000)

    def test_give_default_raise(self):
        self.employee.give_raise()
        self.assertEqual(self.employee.salary, 6000)

    def test_give_custom_raise(self):
        self.employee2.give_raise(3000)
        self.assertEqual(self.employee2.salary, 4000)

unittest.main()
阅读更多
想对作者说点什么?
相关热词

博主推荐

换一批

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