繁体   English   中英

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

[英]How should one test matplotlib plotting with tox and py.test?

如何使用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)发生了此错误。 较旧的版本未产生此错误。

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

[tox]
envlist = py27

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM