LoginSignup
16
17

More than 5 years have passed since last update.

Pythonのpytest-watchモジュールでテスト駆動開発が捗った

Posted at

概要

Pythonのテストツールの定番のpytestですが、watch オプションがないので、実装するたび、コマンドをせっせと実行する必要があって面倒だったので、調べてみたら、pytest-watchという素敵ツールがありました。

pytest: helps you write better programs
https://docs.pytest.org/en/latest/

pytest-watch -- Continuous pytest runner
https://github.com/joeyespo/pytest-watch

pytest-watch a zero-config CLI tool that runs pytest, and re-runs it when a file in your project changes. It beeps on failures and can run arbitrary commands on each passing and failing test run.

テストに失敗するとビープ音を鳴らしたり、テスト後にコマンド実行もできたりするみたいです。

使い方

実行環境を用意します。Pythonは3.6.6を利用しています。
ここではpyenv を利用して仮想環境を用意していますが、なくてもおkです。
環境が構築できたら、pytestとpytest-watchをインストールします。

> python --version
Python 3.6.6

> mkdir 任意のディレクトリ
> cd 任意のディレクトリ
> python -b venv venv
> . venv/bin/activate
> pip install pytest pytest-watch

適当に実装とテストを用意します。

> cat <<EOF > hoge.py
def hoge():
  return 'hoge!!!'

EOF

> cat <<EOF > hoge_test.py
from hoge import *

def test_hoge():
  assert hoge() == 'hoge!!!'  

EOF

まずはpytest でテストを実行してみます。

> pytest

======================================= test session starts ========================================
platform darwin -- Python 3.6.6, pytest-3.8.2, py-1.7.0, pluggy-0.7.1
rootdir: 任意のディレクトリ, inifile:
collected 1 item

hoge_test.py .                                                                               [100%]

===================================== 1 passed in 0.15 seconds =====================================

はい。

次にpytest-watchで実行してみます。
ptw でもpytest-watch でもよいみたいです。

> ptw

[Sat Oct 13 14:57:58 2018] Running: py.test
======================================= test session starts ========================================
platform darwin -- Python 3.6.6, pytest-3.8.2, py-1.7.0, pluggy-0.7.1
rootdir: 任意のディレクトリ, inifile:
collected 1 item

hoge_test.py .                                                                               [100%]

===================================== 1 passed in 0.04 seconds =====================================

はい。こちらは、コマンド実行が終了せずに待ち受け状態になります。
テキストエディタでhoge.py を編集してみます。

hoge.py
def hoge():
  return 'hoge!!!!'


Change detected: hoge.py

[Sat Oct 13 14:58:22 2018] Running: py.test
======================================= test session starts ========================================
platform darwin -- Python 3.6.6, pytest-3.8.2, py-1.7.0, pluggy-0.7.1
rootdir: 任意のディレクトリ, inifile:
collected 1 item

hoge_test.py F                                                                               [100%]

============================================= FAILURES =============================================
____________________________________________ test_hoge _____________________________________________

    def test_hoge():
>     assert hoge() == 'hoge!!!'
E     AssertionError: assert 'hoge!!!!' == 'hoge!!!'
E       - hoge!!!!
E       ?        -
E       + hoge!!!

hoge_test.py:4: AssertionError
===================================== 1 failed in 0.45 seconds =====================================

ファイルの変更検知後、テストを実行してくれました。
素敵です。

参考

pytest: helps you write better programs
https://docs.pytest.org/en/latest/

pytest-watch -- Continuous pytest runner
https://github.com/joeyespo/pytest-watch

16
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17