2

tox と py.test を使用して matplotlib プロットをテストするにはどうすればよいですか?

私のファイルは次のとおりです。

testtox /
  - testtox.py
  - tox.ini
  - setup.py

testtox.py には以下が含まれます。

import matplotlib.pyplot as plt


def plotting():
    """Plot.

    Examples
    --------
    >>> plt.ion()
    >>> plotting()

    """
    plt.plot([1, 2, 1, 3])

tox.ini:

[tox]
envlist = py27

[testenv:py27]
sitepackages = True
commands = py.test --doctest-modules testtox.py
deps = pytest

setup.py:

from setuptools import setup

setup(name='testtox', py_modules=['testtox'])

py.test 自体は正常に動作します:

$ py.test --doctest-modules testtox.py

この場合、プロット ウィンドウが一瞬点滅します。

tox は、DISPLAY 変数でエラーを生成します。

$ tox
[...cut ...]
=================================================== FAILURES ====================================================
__________________________________________ [doctest] testtox.plotting ___________________________________________
005     """Plot.
006 
007     Examples
008     --------
009     >>> plt.ion()
010     >>> plotting()
UNEXPECTED EXCEPTION: RuntimeError(u'Invalid DISPLAY variable',)
Traceback (most recent call last):
[...]

  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')

RuntimeError: Invalid DISPLAY variable

このエラーは、tox の最新バージョン (2.1.1) で発生しました。古いバージョンではエラーは発生しませんでした。

4

1 に答える 1