開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.8 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 14(Object-Oriented Programming)、Exercise 2の解答を求めてみる。
コード
continent_test.py
#!/usr/bin/env python3
from unittest import TestCase, main
from test.support import captured_stdout
from country import Country
from continent import Continent
class TestContinent(TestCase):
def setUp(self):
canada = Country('Canada', 34482779, 9984670)
usa = Country('United States of America', 313914040, 9826675)
mexico = Country('Mexico', 112336538, 1943950)
self.north_america = Continent('North America', [canada, usa, mexico])
def test_name(self):
self.assertEqual(self.north_america.name, 'North America')
def test_countries(self):
with captured_stdout() as stdout:
for country in self.north_america.countries:
print(country)
s = '''Canada has a population of 34482779 and is 9984670 square km.
United States of America has a population of 313914040 and is 9826675 square km.
Mexico has a population of 112336538 and is 1943950 square km.
'''
self.assertEqual(stdout.getvalue(), s)
def test_total_population(self):
self.assertEqual(self.north_america.total_population(), 460733357)
def test_str(self):
s = '''North America
Canada has a population of 34482779 and is 9984670 square km.
United States of America has a population of 313914040 and is 9826675 square km.
Mexico has a population of 112336538 and is 1943950 square km.'''
self.assertEqual(str(self.north_america), s)
if __name__ == "__main__":
main()
continent.py
from country import Country
from typing import List
class Continent:
def __init__(self, name: str, countries: List[Country]):
self.name = name
self.countries = countries
def total_population(self) -> int:
return sum([country.population for country in self.countries])
def __str__(self) -> str:
return self.name + '\n' + \
'\n'.join([str(country) for country in self.countries])
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./continent_test.py
E
======================================================================
ERROR: test_name (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 16, in test_name
self.assertEqual(self.north_america.name, 'North America')
AttributeError: 'Continent' object has no attribute 'name'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
% ./continent_test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
% ./continent_test.py
.E
======================================================================
ERROR: test_str (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 19, in test_str
with captured_stdout as stdout:
AttributeError: __enter__
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=1)
% ./continent_test.py
.E
======================================================================
ERROR: test_str (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 20, in test_str
for country in self.north_america.countries:
AttributeError: 'Continent' object has no attribute 'countries'
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=1)
% ./continent_test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
% ./continent_test.py
..E
======================================================================
ERROR: test_total_population (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 29, in test_total_population
self.assertEqual(self.north_america.total_population(), 468733357)
AttributeError: 'Continent' object has no attribute 'total_population'
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (errors=1)
% ./continent_test.py
..F
======================================================================
FAIL: test_total_population (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 29, in test_total_population
self.assertEqual(self.north_america.total_population(), 468733357)
AssertionError: 460733357 != 468733357
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (failures=1)
% ./continent_test.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
% ./continent_test.py
..F.
======================================================================
FAIL: test_str (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 37, in test_str
self.assertEqual(self, s)
AssertionError: <__main__.TestContinent testMethod=test_str> != 'North America\nCanada has a population o[180 chars]m.\n'
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
% ./continent_test.py
..F.
======================================================================
FAIL: test_str (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 37, in test_str
self.assertEqual(self, s)
AssertionError: <__main__.TestContinent testMethod=test_str> != 'North America\nCanada has a population o[180 chars]m.\n'
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
% ./continent_test.py
..F.
======================================================================
FAIL: test_str (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 37, in test_str
self.assertEqual(self, s)
AssertionError: <__main__.TestContinent testMethod=test_str> != 'North America\nCanada has a population o[180 chars]m.\n'
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
% ./continent_test.py
..F.
======================================================================
FAIL: test_str (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 37, in test_str
self.assertEqual(str(self), s)
AssertionError: 'test_str (__main__.TestContinent)' != 'North America\nCanada has a population of[179 chars]m.\n'
- test_str (__main__.TestContinent)
+ North America
Canada has a population of 34482779 and is 9984670 square km.
United States of America has a population of 313914040 and is 9826675 square km.
Mexico has a population of 112336538 and is 1943950 square km.
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
% ./continent_test.py
..F.
======================================================================
FAIL: test_str (__main__.TestContinent)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./continent_test.py", line 37, in test_str
self.assertEqual(str(self.north_america), s)
AssertionError: 'Nort[158 chars]xico has a population of 112336538 and is 1943950 square km.' != 'Nort[158 chars]xico has a population of 112336538 and is 1943950 square km.\n'
North America
Canada has a population of 34482779 and is 9984670 square km.
United States of America has a population of 313914040 and is 9826675 square km.
- Mexico has a population of 112336538 and is 1943950 square km.+ Mexico has a population of 112336538 and is 1943950 square km.
? +
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
% ./continent_test.py
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK
% mypy continent.py
Success: no issues found in 1 source file
% ./continent_test.py -v
test_countries (__main__.TestContinent) ... ok
test_name (__main__.TestContinent) ... ok
test_str (__main__.TestContinent) ... ok
test_total_population (__main__.TestContinent) ... ok
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK
%
0 コメント:
コメントを投稿