YamatoDaiski的博客

A Simple Blog (๑•́ ₃ •̀๑)

第十一章课后作业

Chapter 11

11-1 城市和国家

# city_functions.py
def city_country(city, country):
    info = city.title()+", "+country.title()
    return info

# test_city.py
import unittest
from city_functions import city_country


class CityCountryTestCase(unittest.TestCase):
    def test_city_country(self):
        formatted_city = city_country("santiago", "chile")
        self.assertEqual(formatted_city, "Santiago, Chile")

unittest.main()

11-2 人口数量

# cityfunctions.py
def city_country(city, country, population=0):
    info = city.title()+", "+country.title()
    if population != 0:
        info += " - population " + str(population)
    return info

# population.py
import unittest
from city_functions import city_country


class CityCountryTestCase(unittest.TestCase):
    def test_city_country(self):
        formatted_city = city_country("santiago", "chile")
        self.assertEqual(formatted_city, "Santiago, Chile")

    def test_city_country_population(self):
        formatted_city = city_country("santiago", "chile", 5000000)
        self.assertEqual(formatted_city, "Santiago, Chile - population 5000000")


unittest.main()

11-3 雇员

import unittest


class Employee:
    def __init__(self, first, last, income):
        self.first = first
        self.last = last
        self.income = income

    def give_raise(self, amount=5000):
        self.income += amount


class EmployTestCase(unittest.TestCase):
    def setUp(self):
        self.employee = Employee("James", 'Brown', 8000)

    def test_give_default_raise(self):
        income = self.employee.income
        self.employee.give_raise()
        self.assertEqual(self.employee.income, income + 5000)

    def test_give_custom_raise(self):
        income = self.employee.income
        self.employee.give_raise(3000)
        self.assertEqual(self.employee.income, income + 3000)

unittest.main()
阅读更多
文章标签: Python
个人分类: 高级程序设计
上一篇第十章课后作业
下一篇Container With Most Water
想对作者说点什么? 我来说一句

Java课后作业Java课后作业

2011年05月04日 1.71MB 下载

微机原理课后作业答案

2009年03月03日 318KB 下载

编译原理课后习题解答第十一章

2009年06月19日 261KB 下载

多媒体课后作业(本科)

2009年10月26日 40KB 下载

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

关闭
关闭