Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024年の状況にアップデート #8

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
25 changes: 13 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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をご利用ください。

Expand All @@ -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です

Expand Down
17 changes: 11 additions & 6 deletions acme/snake.py
Original file line number Diff line number Diff line change
@@ -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}'
30 changes: 17 additions & 13 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
54 changes: 26 additions & 28 deletions tests/acme/test_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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