简体   繁体   中英

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

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

My files are:

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

testtox.py contains:

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 on its own works ok:

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

In this case the plot window is briefly flashed.

tox produces an error on the DISPLAY variable:

$ 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

This error happened with the most recent version of tox (2.1.1). An older version did not produce the error.

Setting the DISPLAY environment variable in tox.ini seems to do the trick in this case:

[tox]
envlist = py27

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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