From 9fe3b40d5abfc037622b9236b73b953fe05d5f53 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:26:58 +0900 Subject: [PATCH 01/16] Update requirements.txt --- requirements.txt | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/requirements.txt b/requirements.txt index 8f8cbf6..bdf8454 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,18 @@ -e . -appdirs==1.4.4 -cov-core==1.15.0 -coverage==5.5 -distlib==0.3.1 -execnet==1.8.0 -filelock==3.0.12 -iniconfig==1.1.1 -py==1.10.0 -pytest-cov==2.11.1 -pytest-xdist==2.2.1 -pytest==6.2.4 -tox==3.23.1 -virtualenv==20.4.6 +cachetools==5.3.3 +chardet==5.2.0 +colorama==0.4.6 +coverage==7.5.4 +distlib==0.3.8 +execnet==2.1.1 +filelock==3.15.4 +iniconfig==2.0.0 +packaging==24.1 +platformdirs==4.2.2 +pluggy==1.5.0 +pyproject-api==1.7.1 +pytest==8.2.2 +pytest-cov==5.0.0 +pytest-xdist==3.6.1 +tox==4.16.0 +virtualenv==20.26.3 From d7e49493822a6bccb03d61e6538404936a6082c0 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:32:33 +0900 Subject: [PATCH 02/16] Update README.rst --- README.rst | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index 110816f..6fe6469 100644 --- a/README.rst +++ b/README.rst @@ -9,8 +9,8 @@ TDDBC for Python with Pytest 動作確認環境 ============ -- Python 3.8.5 -- pytest 6.2.4 +- Python 3.12.4 +- pytest 8.2.2 ※Python2は2020年1月にサポート終了(EOL)していますので、Python3をご利用ください。 @@ -34,18 +34,19 @@ TDDBC for Python with Pytest ... # Output sample - ============================= test session starts ============================== - platform linux -- Python 3.8.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3 + ====================================== test session starts ====================================== + platform linux -- Python 3.12.4, pytest-8.2.2, pluggy-1.5.0 -- /usr/local/bin/python cachedir: .pytest_cache - rootdir: /root/work/python_pytest, configfile: setup.cfg - plugins: forked-1.3.0, cov-2.11.1, xdist-2.2.1 + rootdir: /root/python_pytest + configfile: setup.cfg + plugins: cov-5.0.0, xdist-3.6.1 collected 3 items - tests/acme/test_snake.py::TestPython::test_be_out_of_question PASSED [ 33%] - tests/acme/test_snake.py::TestMontyPython::test_say_name[Monty Python] PASSED [ 66%] - tests/acme/test_snake.py::TestMontyPython::test_say_name[John Smith] PASSED [100%] - - ============================== 3 passed in 0.04s =============================== + tests/acme/test_snake.py::TestPython::test_be_out_of_question PASSED [ 33%] + tests/acme/test_snake.py::TestMontyPython::test_say_name[Monty Python] PASSED [ 66%] + tests/acme/test_snake.py::TestMontyPython::test_say_name[John Smith] PASSED [100%] + + ======================================= 3 passed in 0.02s ======================================= のように正常終了すればOKです From 051e6b74268c0df23efb567d219d6df45cfdf770 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:48:42 +0900 Subject: [PATCH 03/16] add simple test example --- acme/snake.py | 5 +++++ tests/acme/test_snake.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/acme/snake.py b/acme/snake.py index 0a3f04a..cffe4ac 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -3,11 +3,16 @@ from random import randint +def ultimate_answer(): + return 42 + + class Python(object): def say(self, greeting=None): return 'Hiss!' * randint(1, 9) + class MontyPython(Python): def say(self, greeting): return 'Hello %s' % greeting diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index 33a20d8..408f025 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -24,6 +24,11 @@ def pytest_generate_tests(metafunc): ) +def test_ultimate_answer(): + from acme.snake import ultimate_answer + assert ultimate_answer() == 42 + + class TestPython: def test_be_out_of_question(self): from acme.snake import Python From 8518bfcc574f45bef91ca578caef36fd6e9585b9 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:49:37 +0900 Subject: [PATCH 04/16] organize import --- tests/acme/test_snake.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index 408f025..eacffc4 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -7,6 +7,7 @@ import re +from acme.snake import ultimate_answer, Python, MontyPython def pytest_generate_tests(metafunc): """ @@ -25,13 +26,11 @@ def pytest_generate_tests(metafunc): def test_ultimate_answer(): - from acme.snake import ultimate_answer assert ultimate_answer() == 42 class TestPython: def test_be_out_of_question(self): - from acme.snake import Python assert re.match(r'^(Hiss\!)+$', Python().say()), 'シャー' @@ -44,5 +43,4 @@ class TestMontyPython: } def test_say_name(self, name): - from acme.snake import MontyPython assert MontyPython().say(name) == 'Hello ' + name From 81706a19fbd96b9bb0c96376397e1b2c94809aea Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:52:10 +0900 Subject: [PATCH 05/16] retrofit to use pytest.mark.parametrize --- tests/acme/test_snake.py | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index eacffc4..f1c7314 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -5,25 +5,11 @@ サンプルテスト """ +import pytest import re from acme.snake import ultimate_answer, Python, MontyPython -def pytest_generate_tests(metafunc): - """ - Parametrizing test methods through per-class configuration - http://pytest.org/latest-ja/example/parametrize.html#id5 - """ - try: - funcarglist = metafunc.cls.params[metafunc.function.__name__] - except AttributeError: - return - argnames = list(funcarglist[0]) - metafunc.parametrize( - argnames, - [[funcargs[name] for name in argnames] for funcargs in funcarglist] - ) - def test_ultimate_answer(): assert ultimate_answer() == 42 @@ -35,12 +21,10 @@ def test_be_out_of_question(self): class TestMontyPython: - params = { - 'test_say_name': [ - dict(name='Monty Python'), - dict(name='John Smith'), - ], - } - + @pytest.mark.parametrize( + 'name', + [ + 'Monty Python', 'John Smith' + ]) def test_say_name(self, name): assert MontyPython().say(name) == 'Hello ' + name From 373e4ffa8920757ca85b91104d25644e33f112d9 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:54:01 +0900 Subject: [PATCH 06/16] clarify test by removing logic --- tests/acme/test_snake.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index f1c7314..b24f684 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -22,9 +22,10 @@ def test_be_out_of_question(self): class TestMontyPython: @pytest.mark.parametrize( - 'name', + 'name, expected', [ - 'Monty Python', 'John Smith' + ('Monty Python', 'Hello Monty Python'), + ('John Smith', 'Hello John Smith'), ]) - def test_say_name(self, name): - assert MontyPython().say(name) == 'Hello ' + name + def test_say_name(self, name, expected): + assert MontyPython().say(name) == expected From c7b67c3859225063ba1502ea84635dfb8a4292e9 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:55:44 +0900 Subject: [PATCH 07/16] remove unnecessary inheritance --- acme/snake.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acme/snake.py b/acme/snake.py index cffe4ac..49ca860 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -7,7 +7,7 @@ def ultimate_answer(): return 42 -class Python(object): +class Python: def say(self, greeting=None): return 'Hiss!' * randint(1, 9) From 7c4a36d1aefddfed77e26071482e1a57024a3d91 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 08:59:48 +0900 Subject: [PATCH 08/16] remove compllication in random --- acme/snake.py | 5 +---- tests/acme/test_snake.py | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/acme/snake.py b/acme/snake.py index 49ca860..6e94c58 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -1,7 +1,5 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from random import randint - def ultimate_answer(): return 42 @@ -9,8 +7,7 @@ def ultimate_answer(): class Python: def say(self, greeting=None): - return 'Hiss!' * randint(1, 9) - + return 'Hiss!' class MontyPython(Python): diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index b24f684..19bfd7d 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -17,7 +17,7 @@ def test_ultimate_answer(): class TestPython: def test_be_out_of_question(self): - assert re.match(r'^(Hiss\!)+$', Python().say()), 'シャー' + assert Python().say() == 'Hiss!' class TestMontyPython: From 02b52c33774c338bb1d8e26f8a498fe7f9e4b198 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:02:17 +0900 Subject: [PATCH 09/16] populate with properly named tests --- tests/acme/test_snake.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index 19bfd7d..4dc2495 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -16,9 +16,12 @@ def test_ultimate_answer(): class TestPython: - def test_be_out_of_question(self): + def test_say(self): assert Python().say() == 'Hiss!' + def test_say_greeting_is_ignored(self): + assert Python().say('a greeting') == 'Hiss!' + class TestMontyPython: @pytest.mark.parametrize( From 5da16eef975584f6f75933c8de465c0738cf16e2 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:06:54 +0900 Subject: [PATCH 10/16] add type hint and fix wrong override --- acme/snake.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/acme/snake.py b/acme/snake.py index 6e94c58..4a6fb6f 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -1,15 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -def ultimate_answer(): +def ultimate_answer() -> int: return 42 class Python: - def say(self, greeting=None): + def say(self, greeting: str = '') -> str: return 'Hiss!' class MontyPython(Python): - def say(self, greeting): + def say(self, greeting: str = '') -> str: return 'Hello %s' % greeting From afd07d82f06981238b8a79af191e37c65ffda567 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:09:11 +0900 Subject: [PATCH 11/16] use f-string instead of % --- acme/snake.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acme/snake.py b/acme/snake.py index 4a6fb6f..3707893 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -12,4 +12,4 @@ def say(self, greeting: str = '') -> str: class MontyPython(Python): def say(self, greeting: str = '') -> str: - return 'Hello %s' % greeting + return f'Hello {greeting}' From 44f7fcda51ff1cca5928a0c5d15bf5915b2f5c06 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:13:41 +0900 Subject: [PATCH 12/16] change spec for a bit more versatile tests --- acme/snake.py | 9 ++++++--- tests/acme/test_snake.py | 6 ++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/acme/snake.py b/acme/snake.py index 3707893..e50dec5 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -6,10 +6,13 @@ def ultimate_answer() -> int: class Python: - def say(self, greeting: str = '') -> str: + def say(self) -> str: return 'Hiss!' class MontyPython(Python): - def say(self, greeting: str = '') -> str: - return f'Hello {greeting}' + def __init__(self, greeting: str): + self.greeting = greeting + + def say(self) -> str: + return f'Hello {self.greeting}' diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index 4dc2495..7761ebc 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -19,9 +19,6 @@ class TestPython: def test_say(self): assert Python().say() == 'Hiss!' - def test_say_greeting_is_ignored(self): - assert Python().say('a greeting') == 'Hiss!' - class TestMontyPython: @pytest.mark.parametrize( @@ -31,4 +28,5 @@ class TestMontyPython: ('John Smith', 'Hello John Smith'), ]) def test_say_name(self, name, expected): - assert MontyPython().say(name) == expected + sut = MontyPython(name) + assert sut.say() == expected From 182dcbc7462cc4fc833e0bd87ac4e22ab1e8a9b6 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:16:15 +0900 Subject: [PATCH 13/16] 3A comments in test --- tests/acme/test_snake.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index 7761ebc..8771b03 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -17,7 +17,13 @@ def test_ultimate_answer(): class TestPython: def test_say(self): - assert Python().say() == 'Hiss!' + # Arrange + # sut = System Under Test + sut = Python() + # Act + actual = sut.say() + # Assert + assert actual == 'Hiss!' class TestMontyPython: @@ -28,5 +34,9 @@ class TestMontyPython: ('John Smith', 'Hello John Smith'), ]) def test_say_name(self, name, expected): + # Arrange sut = MontyPython(name) - assert sut.say() == expected + # Act + actual = sut.say() + # Assert + assert actual == expected From c66fec41475f985f5254661fd8537a559d501904 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:22:13 +0900 Subject: [PATCH 14/16] refactor variable name --- acme/snake.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/acme/snake.py b/acme/snake.py index e50dec5..57ba69b 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -11,8 +11,8 @@ def say(self) -> str: class MontyPython(Python): - def __init__(self, greeting: str): - self.greeting = greeting + def __init__(self, name: str): + self.name = name def say(self) -> str: - return f'Hello {self.greeting}' + return f'Hello {self.name}' From 2d82796bc9cfe16961cb2919d84a62f3ece3e5aa Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:29:48 +0900 Subject: [PATCH 15/16] remove unused import --- tests/acme/test_snake.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index 8771b03..d3956a2 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -6,7 +6,6 @@ """ import pytest -import re from acme.snake import ultimate_answer, Python, MontyPython From 2d26efe26fdc0a0bc8bdef549ccbbc4896219963 Mon Sep 17 00:00:00 2001 From: yattom Date: Wed, 10 Jul 2024 09:33:39 +0900 Subject: [PATCH 16/16] update pytest URL --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 6fe6469..9ca2e14 100644 --- a/README.rst +++ b/README.rst @@ -4,7 +4,7 @@ TDDBC for Python with Pytest これは、TDDBCのPythonista向け Pytest_ プロジェクトです -.. _Pytest: http://pytest.org/latest-ja/ +.. _Pytest: https://docs.pytest.org/ 動作確認環境 ============