diff --git a/README.rst b/README.rst index 110816f..9ca2e14 100644 --- a/README.rst +++ b/README.rst @@ -4,13 +4,13 @@ TDDBC for Python with Pytest これは、TDDBCのPythonista向け Pytest_ プロジェクトです -.. _Pytest: http://pytest.org/latest-ja/ +.. _Pytest: https://docs.pytest.org/ 動作確認環境 ============ -- 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です diff --git a/acme/snake.py b/acme/snake.py index 0a3f04a..57ba69b 100644 --- a/acme/snake.py +++ b/acme/snake.py @@ -1,13 +1,18 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from random import randint +def ultimate_answer() -> int: + return 42 -class Python(object): - def say(self, greeting=None): - return 'Hiss!' * randint(1, 9) + +class Python: + def say(self) -> str: + return 'Hiss!' class MontyPython(Python): - def say(self, greeting): - return 'Hello %s' % greeting + def __init__(self, name: str): + self.name = name + + def say(self) -> str: + return f'Hello {self.name}' 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 diff --git a/tests/acme/test_snake.py b/tests/acme/test_snake.py index 33a20d8..d3956a2 100644 --- a/tests/acme/test_snake.py +++ b/tests/acme/test_snake.py @@ -5,39 +5,37 @@ サンプルテスト """ -import re +import pytest +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 class TestPython: - def test_be_out_of_question(self): - from acme.snake import Python - assert re.match(r'^(Hiss\!)+$', Python().say()), 'シャー' + def test_say(self): + # Arrange + # sut = System Under Test + sut = Python() + # Act + actual = sut.say() + # Assert + assert actual == 'Hiss!' class TestMontyPython: - params = { - 'test_say_name': [ - dict(name='Monty Python'), - dict(name='John Smith'), - ], - } - - def test_say_name(self, name): - from acme.snake import MontyPython - assert MontyPython().say(name) == 'Hello ' + name + @pytest.mark.parametrize( + 'name, expected', + [ + ('Monty Python', 'Hello Monty Python'), + ('John Smith', 'Hello John Smith'), + ]) + def test_say_name(self, name, expected): + # Arrange + sut = MontyPython(name) + # Act + actual = sut.say() + # Assert + assert actual == expected