如何使用tox和py.test测试matplotlib绘图?

时间:2015-07-09 12:31:23

标签: python matplotlib pytest tox

如何使用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)发生此错误。旧版本没有产生错误。

1 个答案:

答案 0 :(得分:0)

tox.ini中设置DISPLAY环境变量似乎可以解决这个问题:

[tox]
envlist = py27

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