Python練習問題——2018-04-09宿題


11-1都市と国:2つのパラメータを受け入れる関数を作成します.1つの都市名と1つの国名です.この関数は、Santiago、ChileなどのフォーマットCity、Countryの文字列を返します.この関数をcityという名前で保存します.functions.pyのモジュールにあります.
test_という名前の作成cities.pyのプログラムは、作成したばかりの関数をテストします(モジュールunittestとテストする関数をインポートする必要があることを忘れないでください).test_という名前の作成city_country()の方法では,前述の関数を呼び出すために「santiago」や「chile」のような値を用いた場合に得られる文字列が正しいことを確認する.test_の実行cities.py、テストtest_の確認city_country()が通過しました.
city_functions.py
def city_country(city, country):
    city_country_name = city + ', ' + country
    return city_country_name.title()

test_cities.py
import unittest
from city_functions import city_country

class TestCityCountry(unittest.TestCase):
    def test_city_country(self):
        city_country_name = city_country('santiago', 'chile')
        self.assertEqual(city_country_name, 'Santiago, Chile')

unittest.main()

出力:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

11-2人口数:前の関数を変更して、3番目の必須パラメータpopulationを含ませ、City、Country-population xxxのフォーマットの文字列、例えばSantiago、Chile-population 5000000を返します.test_の実行cities.py、テストtest_の確認city_country()は通過しませんでした.
city_functions.py
def city_country(city, country, population):
    city_country_name = city + ', ' + country + ' - '
    city_country_name = city_country_name.title() + 'population '
    city_country_name = city_country_name + str(population)
    return city_country_name

出力:
E
======================================================================
ERROR: test_city_country (__main__.TestCityCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_cities.py", line 7, in test_city_country
    city_country_name = city_country('santiago', 'chile')
TypeError: city_country() missing 1 required positional argument: 'population'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

上記の関数を変更して、パラメータpopulationをオプションに設定します.test_を再実行cities.py、テストtest_の確認city_country()はまた通過した.
city_functions.py
def city_country(city, country, population = ''):
    if population:
        city_country_name = city + ', ' + country + ' - '
        city_country_name = city_country_name.title() + 'population '
        city_country_name = city_country_name + str(population)
    else:
        city_country_name = city + ', ' + country
        city_country_name = city_country_name.title()
    return city_country_name

出力:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

test_という名前をもう一つ書きますcity_country_population()のテストでは、「santiago」、「chile」、「population=5000000」のような値を使用してこの関数を呼び出すことができることを確認します.test_を再実行cities.py、テストtest_の確認city_country_population()が通過しました.
test_cities.py
import unittest
from city_functions import city_country


class TestCityCountry(unittest.TestCase):
    def test_city_country(self):
        city_country_name = city_country('santiago', 'chile')
        self.assertEqual(city_country_name, 'Santiago, Chile')

    def test_city_country_population(self):
        city_country_population = city_country('santiago', 'chile', 5000000)
        correct_name = 'Santiago, Chile - population 5000000'
        self.assertEqual(city_country_population, correct_name)


unittest.main()

出力:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

11-3従業員:Employeeというクラスを作成します.init__()名前、姓、年俸を受け入れ、属性に格納します.giveという名前のraise()の方法では、デフォルトで年俸を5000ドル増加しますが、他の年俸増加量も受け入れることができます.
Employeeのテスト・インスタンスを作成します.テスト・メソッドはtest_give_default_raise()とtest_give_custom_raise().メソッドsetUp()を使用して、各テストメソッドに新しい従業員インスタンスが作成されないようにします.このテスト例を実行して、両方のテストが合格したことを確認します.
employee.py
class Employee():
    def __init__(self, first_name, last_name, annual_salary):
        self.first_name = first_name
        self.last_name = last_name
        self.annual_salary = annual_salary

    def give_raise(self, money = 5000):
        self.annual_salary += money

test_employee.py
import unittest
from employee import Employee


class TestEmployee(unittest.TestCase):
    def setUp(self):
        self.my_employee = Employee('Hello', 'World', 66666)

    def test_give_default_raise(self):
        self.assertEqual(self.my_employee.first_name, 'Hello')
        self.assertEqual(self.my_employee.last_name, 'World')
        self.assertEqual(self.my_employee.annual_salary, 66666)

    def test_give_custom_raise(self):
        self.my_employee.give_raise(23333)
        self.assertEqual(self.my_employee.annual_salary, 89999)


unittest.main()

出力:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK