2017年1月15日日曜日

開発環境

Python Crash Course (Eric Matthes (著)、No Starch Press)のPART1(BASICS)、Chapter 1.(TESTING YOUR CODE)のTRY IT YOURSELF 11-1.(City, Country)、2(Population)(No.6199)を取り組んでみる。

TRY IT YOURSELF 11-1.(City, Country)、2(Population)(No.6199)

Unit Test

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import unittest

from city_functions import *


class CityFunctionsTest(unittest.TestCase):

    def setUp(self):
        self.city = 'santiago'
        self.country = 'chile'
        self.population = 5000000

    def tearDown(self):
        pass

    def test_city_country(self):
        res = city_country(self.city, self.country)
        self.assertEqual(res, 'Santiago, Chile')

    def test_city_country_population(self):
        res = city_country_population(self.city, self.country, self.population)
        self.assertEqual(res, 'Santiago, Chile - population 5000000')

    def test_city_country_population_optional(self):
        res = city_country_population(self.city, self.country)
        self.assertEqual(res, 'Santiago, Chile - population 5000000')

if __name__ == '__main__':
    unittest.main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def city_country(city, country):
    return '{0}, {1}'.format(city.capitalize(), country.capitalize())


def city_country_population(city, country, population=5000000):
    return '{0}, {1} - population {2}'.format(
        city.capitalize(), country.capitalize(), population)

入出力結果(Terminal, IPython)

$ ./test_cities.py -v
test_city_country (__main__.CityFunctionsTest) ... ok
test_city_country_population (__main__.CityFunctionsTest) ... ok
test_city_country_population_optional (__main__.CityFunctionsTest) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK
$

0 コメント:

コメントを投稿