tomahawkはPython 2.4/2.5/2.6/2.7 対応を謳っている。ただ、テストするたびに virtualenv で切り替え→テスト実行が非常に手間だった。そんなところにこのスライドでtoxという複数のバージョンのPythonでテストできるツールがあることを知った。実際使ってみたところ、今までやっていた作業が完全に自動化されてウハウハになったので紹介したい。
toxの実行時の流れ
toxはvirtualenvを使って環境を作っては切り替えてくれる。実行時のログを見た限り、流れとしては下記のようになっているようだ。
- テストしたいソフトウェアのディレクトリ上にtox.iniを作成
- 該当ディレクトリで tox コマンドを実行
- python setup.py sdist が実行される
- tox.iniで定義された環境をvirtualenvで作成し、必要なライブラリをインストール
- tox.ini内のcommandsでテストコマンドを実行
tox.ini
depsにはテストで必要なライブラリなどを書いておく。テスト対象のソフトウェアが依存しているライブラリはtoxがpipを使うので、setup.pyで依存をしっかり書いておけば自動でインストールされる。
[tox]
envlist = py25,py26,py27
[testenv]
deps=nose
commands = nosetests -sdv tests/internal
[testenv:py25]
basepython = /usr/local/pythonbrew/pythons/Python-2.5.6/bin/python2.5
[testenv:py26]
basepython = /usr/local/pythonbrew/pythons/Python-2.6.8/bin/python2.6
[testenv:py27]
basepython = /usr/local/pythonbrew/pythons/Python-2.7.3/bin/python2.7
toxを実行してみる
$ pwd
/Users/kazuhiro/work/tomahawk
$ tox
________________________________________ [tox sdist] ________________________________________
[TOX] ***creating sdist package
[TOX] /Users/kazuhiro/work/tomahawk$ /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python setup.py sdist --formats=zip --dist-dir .tox/dist >.tox/log/0.log
[TOX] ***copying new sdistfile to '/Users/kazuhiro/.tox/distshare/tomahawk-0.5.0.zip'
____________________________________ [tox testenv:py25] _____________________________________
[TOX] ***creating virtualenv py25
[TOX] /Users/kazuhiro/work/tomahawk/.tox$ /opt/local/pythonbrew/pythons/Python-2.5.6/bin/python2.5 /Library/Python/2.7/site-packages/virtualenv.py --distribute --no-site-packages py25 >py25/log/0.log
[TOX] ***installing dependencies: nose
[TOX] /Users/kazuhiro/work/tomahawk/.tox/py25/log$ ../bin/pip install --download-cache=/Users/kazuhiro/work/tomahawk/.tox/_download nose >1.log
[TOX] ***installing sdist
[TOX] /Users/kazuhiro/work/tomahawk/.tox/py25/log$ ../bin/pip install --download-cache=/Users/kazuhiro/work/tomahawk/.tox/_download /Users/kazuhiro/work/tomahawk/.tox/dist/tomahawk-0.5.0.zip >2.log
[TOX] /Users/kazuhiro/work/tomahawk$ .tox/py25/bin/nosetests -sdv tests/internal
test_command.test_01_execute ... ok
test_command.test_02_execute_error ... ok
test_command.test_03_execute_timeout ... ok
test_command.test_04_execute_option_host_files ... ok
test_command.test_05_execute_option_continue_on_error ... ok
test_command.test_06_execute_option_ssh_options ... ok
test_command.test_07_output_format ... ok
test_rsync.test_01_execute ... ok
test_rsync.test_02_execute_error ... ok
test_rsync.test_03_execute_timeout ... ok
test_rsync.test_04_execute_option_rsync_options ... ok
test_rsync.test_05_execute_option_mirror_mode_pull ... ok
test_rsync.test_06_execute_option_continue_on_error ... ok
----------------------------------------------------------------------
Ran 13 tests in 12.750s
OK
____________________________________ [tox testenv:py26] _____________________________________
[TOX] ***creating virtualenv py26
[TOX] /Users/kazuhiro/work/tomahawk/.tox$ /opt/local/pythonbrew/pythons/Python-2.6.8/bin/python2.6 /Library/Python/2.7/site-packages/virtualenv.py --distribute --no-site-packages py26 >py26/log/0.log
[TOX] ***installing dependencies: nose
[TOX] /Users/kazuhiro/work/tomahawk/.tox/py26/log$ ../bin/pip install --download-cache=/Users/kazuhiro/work/tomahawk/.tox/_download nose >1.log
[TOX] ***installing sdist
[TOX] /Users/kazuhiro/work/tomahawk/.tox/py26/log$ ../bin/pip install --download-cache=/Users/kazuhiro/work/tomahawk/.tox/_download /Users/kazuhiro/work/tomahawk/.tox/dist/tomahawk-0.5.0.zip >2.log
[TOX] /Users/kazuhiro/work/tomahawk$ .tox/py26/bin/nosetests -sdv tests/internal
(snip)
----------------------------------------------------------------------
Ran 13 tests in 13.355s
OK
____________________________________ [tox testenv:py27] _____________________________________
[TOX] ***creating virtualenv py27
[TOX] /Users/kazuhiro/work/tomahawk/.tox$ /opt/local/pythonbrew/pythons/Python-2.7.3/bin/python2.7 /Library/Python/2.7/site-packages/virtualenv.py --distribute --no-site-packages py27 >py27/log/0.log
[TOX] ***installing dependencies: nose
[TOX] /Users/kazuhiro/work/tomahawk/.tox/py27/log$ ../bin/pip install --download-cache=/Users/kazuhiro/work/tomahawk/.tox/_download nose >1.log
[TOX] ***installing sdist
[TOX] /Users/kazuhiro/work/tomahawk/.tox/py27/log$ ../bin/pip install --download-cache=/Users/kazuhiro/work/tomahawk/.tox/_download /Users/kazuhiro/work/tomahawk/.tox/dist/tomahawk-0.5.0.zip >2.log
[TOX] /Users/kazuhiro/work/tomahawk$ .tox/py27/bin/nosetests -sdv tests/internal
(snip)
----------------------------------------------------------------------
Ran 13 tests in 14.168s
OK
_______________________________________ [tox summary] _______________________________________
[TOX] py25: commands succeeded
[TOX] py26: commands succeeded
[TOX] py27: commands succeeded
[TOX] congratulations :)
というわけで、toxオススメです。