僕とコードとブルーハワイ

omega (@equal_001) の日記

toxコマンドでInvocationError食らったときの対処法

=================

環境:MacOS X 10.4

Python -V:3.4.2

=================

 

問題:

toxでUnitTest実行しようとしたら"InvocationError"になった。

 エラートレース

py34 installed: py==1.4.30,pytest==2.8.1,UNKNOWN==0.0.0,wheel==0.24.0
py34 runtests: PYTHONHASHSEED='1572653641'
py34 runtests: commands[0] | py.test tests
========================= test session starts =========================
platform darwin -- Python 3.4.2, pytest-2.8.1, py-1.4.30, pluggy-0.3.1
rootdir: /Users/OMEGA/toxtest, inifile: tox.ini
collected 0 items

=========================== in 0.00 seconds =========================
ERROR: InvocationError: '/Users/OMEGA/toxtest/.tox/py34/bin/py.test tests'
______________________________ summary ____________________________
ERROR: py34: commands failed

  

原因 :

  • tox実行時にテストファイルが一つも無い場合、InvocationErrorになる
  • pytestのテストファイルの読み込み規則の問題でtestファイルが読み込まれない罠

 

調査と解決方法:

今回の調査環境ディレクトリ構成

toxtest

├── setup.py
├── tox.ini
└── tests
    ├── check_hoge.py

    └── tests.py

 

tox.ini

[tox]

  envlist = py27, py34

[testenv]

  basepython = python3.4

  commands = py.test tests

  deps = pytest

 

この構成でtoxを実行してみたが、上記のエラートレースが発生した。

どうやらテストファイルを認識してくれていないようだ。

というかtoxってテストケースが一つも無い時ってエラー吐くんだね。。

 

というわけで調べてみたところ、そもそもpytestではデフォルトtest_*.pyしかテスト時ファイルとして読み込まないようになっていた。

以下のドキュメントを参考に、任意のテストファイル名でも認識してくれるように設定した。

Changing standard (Python) test discovery

 

以下の設定をtox.iniへ追記する。

; [pytestの設定で読み込むファイル形式を定義する。

; デフォルトはtest_*.pyのみだが、python_filesで定義するときはtest_*.pyも記述しないと省かれてしまう。

[pytest]

python_files = check_*.py test_*.py tests.py

 

tox実行結果

py34 installed: py==1.4.30,pytest==2.8.1,UNKNOWN==0.0.0,wheel==0.24.0
py34 runtests: PYTHONHASHSEED='3739634378'
py34 runtests: commands[0] | py.test tests
==================== test session starts ====================
platform darwin -- Python 3.4.2, pytest-2.8.1, py-1.4.30, pluggy-0.3.1
rootdir: /Users/OMEGA/toxtest, inifile: tox.ini
collected 2 items

tests/check_hoge.py .
tests/tests.py .

================== 2 passed in 0.03 seconds =================
____________________________ summary __________________________

py34: commands succeeded
congratulations :)

 

tests/配下のテストファイル二つが認識されて、テストが実行された。ほっ。 

 

このエラー見たとき、何故テストが認識されないのかわからなくて悩んだ。

なかなかマイナーなハマりポイントな気がする。