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])

毒物.ini:

[tox]
envlist = py27

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

设置.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

0

在这种情况下,设置 DISPLAY 环境变量tox.ini似乎可以解决问题:

[tox]
envlist = py27

[testenv:py27]
setenv = 
    DISPLAY = :0
sitepackages = True
commands = py.test --doctest-modules testtox.py
deps = pytest
于 2015-07-09T12:37:37.887 回答