tox利用時にpipenvを併用しながらdistへのファイル出力を外してみる

toxを利用する際に、dist以下にファイル出力をせずに単純にテストが通過するかどうかを確認したいケースがあります。pipenvを併用しつつ、手続きを記録してみました。
2019.02.20

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

toxを利用する際に、パッケージの作成が不要で単純にテストだけ行いたいケースがあります。どういった対応を取ればよいのか即結論にたどり着かなかったこともあり、記録として残してみます。

前提状況

今回は以下の通りでした。

  • サブディレクトリ内で実施
  • dist以下にパッケージを生成する必要はない
  • 対象のPythonバージョンは3系のみ
  • pipenvを利用

toxの動作指定

tox.iniのtoxセクションに、skipsdistを指定します。Python3系に絞るため、envlistも指定しています。

[tox]
skipsdist = True
envlist = py36

pipenv用の設定

pipenv上でpytestを実施する必要があり、以下の設定をいれました。

[testenv]
deps = pipenv
setenv = PYTHONPATH = {toxinidir}
         PIPENV_VERBOSITY = -1
commands = pipenv sync --dev
           pipenv run py.test tests/

実行結果です。py36で且つdistへの出力も行われていません。

% tox
py36 create: /Path/To/.tox/py36
py36 installdeps: pipenv
py36 installed: certifi==2018.11.29,pipenv==2018.11.26,virtualenv==16.4.0,virtualenv-clone==0.5.1
py36 run-test-pre: PYTHONHASHSEED='00000000000'
py36 runtests: commands[0] | pipenv sync --dev
Installing dependencies from Pipfile.lock (60bdaa)…
  �   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 22/22 — 00:00:09
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
All dependencies are now up-to-date!
py36 runtests: commands[1] | pipenv run py.test tests/
============================= test session starts ==============================
platform darwin -- Python 3.6.5, pytest-4.3.0, py-1.7.0, pluggy-0.8.1
cachedir: .tox/python/.pytest_cache
rootdir: /Path/to/subdir, inifile:
collected 5 items
tests/test_conv.py .....                                                 [100%]
===================================================================================== 5 passed in 0.08 seconds ======================================================================================
______________________________________________________________________________________________ summary ______________________________________________________________________________________________

  python: commands succeeded
    congratulations :)

なお、distへの出力を省いたことにより、setup.pyも不要になっています。

% tree
├── Pipfile
├── Pipfile.lock
├── README.md
├── paramconv
│   ├── __init__.py
│   ├── conv.py
│   ├── exception.py
│   └── params.yml
├── requirements.txt
├── tests
│   ├── __init__.py
│   ├── conftest.py
│   ├── fixtures
│   │   └── nobody.json
│   └── test_conv.py
└── tox.ini

まとめ

サブディレクトリであるかどうかは今回の前提では特に大きな影響もありませんでした。skipsdistについては、パッケージとしてのファイルを出力する必要がなければ設定しておくと時間短縮にも繋がり、ベターだと思われます。