Skip to content

テストコード例をわかりやすくする #5

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

Closed
wants to merge 15 commits into from
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

60 changes: 42 additions & 18 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,64 @@ TDDBC for Python with Pytest
動作確認環境
============

- Python 2.7.6
- Python 3.3.1
- PyPy 1.9.0
- PyPy 2.2.0
- Python 3.8.5
- pytest 6.2.4

※Python2は2020年1月にサポート終了(EOL)していますので、Python3をご利用ください。

セットアップ
============

以下のように実行して、環境を構築してください。

.. code-block:: sh

$ pip install -r requirements.txt
$ git clone https://github.com/yattom/python_pytest.git
$ cd python_pytest
$ pip3 install -r requirements.txt

**setup.py** を実行し
テストを実行するには **pytest** コマンドを使います。

.. code-block:: sh

$ python setup.py test

$ pytest
...

# Output sample
======================== test session starts =================================
platform linux2 -- Python 2.7.3[pypy-2.2.0-final] -- pytest-2.4.2
-- ~/.virtualenvs/tddbc_python_pytest_pypy22/bin/python
plugins: cov, xdist
collected 3 items
============================= test session starts ==============================
platform linux -- Python 3.8.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /root/work/python_pytest
plugins: cov-2.11.1
collected 5 items

tests/acme/test_acme.py:28: TestPython.test_be_out_of_question PASSED
tests/acme/test_acme.py:41: TestMontyPython.test_say_name[Monty Python] PASSED
tests/acme/test_acme.py:41: TestMontyPython.test_say_name[John Smith] PASSED
======================== 3 passed in 0.10 seconds ============================
tests/acme/test_snake.py ..... [100%]

============================== 5 passed in 0.04s ===============================

のように正常終了すればOKです

実行したテストを1つずつ表示するには **-v** オプションを使います。

.. code-block:: sh

$ pytest -v
...
# Output sample
============================= test session starts ==============================
platform linux -- Python 3.8.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /root/work/python_pytest/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /root/work/python_pytest
plugins: cov-2.11.1
collected 5 items

tests/acme/test_snake.py::test_python_hisses PASSED [ 20%]
tests/acme/test_snake.py::TestMontyPython::test_say_name_guido PASSED [ 40%]
tests/acme/test_snake.py::TestMontyPython::test_say_name_kent PASSED [ 60%]
tests/acme/test_snake.py::TestMontyPython::test_say_name[Terry-Hello Terry] PASSED [ 80%]
tests/acme/test_snake.py::TestMontyPython::test_say_name[John-Hello John] PASSED [100%]

============================== 5 passed in 0.05s ===============================


ライセンス
==========

Expand Down
12 changes: 3 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
cov-core==1.15.0
coverage==4.4.1
execnet==1.4.1
py==1.4.34
pytest==3.1.3
pytest-cov==2.5.1
pytest-xdist==1.18.0
tox==2.7.0
virtualenv==15.1.0
-e .
pytest-cov==2.11.1
pytest==6.2.4
9 changes: 0 additions & 9 deletions setup.cfg

This file was deleted.

16 changes: 0 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand
import sys


class PyTest(TestCommand):
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = []
self.test_suite = True

def run_tests(self):
#import here, cause outside the eggs aren't loaded
import pytest
errno = pytest.main(self.test_args)
sys.exit(errno)

setup(
name='skeleton_for_pytest',
Expand All @@ -25,7 +10,6 @@ def run_tests(self):
license='MIT',
packages=find_packages(exclude=['tests']),
tests_require=['pytest'],
cmdclass={'test': PyTest},
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
Expand Down
60 changes: 29 additions & 31 deletions tests/acme/test_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,37 @@
サンプルテスト
"""

import re


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]
)
import pytest
from acme.snake import Python, MontyPython


def test_python_hisses():
sut = Python()
actual = sut.say()
assert 'Hello' not in actual, 'PythonはHelloと言わない'

class TestPython:
def test_be_out_of_question(self):
from acme.snake import Python
assert re.match(r'^(Hiss\!)+$', Python().say()), 'シャー'

@pytest.fixture
def monty_python():
return MontyPython()


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
def test_say_name_guido(self, monty_python):
actual = monty_python.say('Guido')
assert actual == 'Hello Guido'

def test_say_name_kent(self, monty_python):
actual = monty_python.say('Kent')
assert actual == 'Hello Kent'

@pytest.mark.parametrize(
"name, expected",
[
('Terry', 'Hello Terry'),
('John', 'Hello John'),
]
)
def test_say_name(self, monty_python, name, expected):
actual = monty_python.say(name)
assert actual == expected
13 changes: 0 additions & 13 deletions tox.ini

This file was deleted.