title: How should one test matplotlib plotting with tox and py.test?
body: <p>How should one test matplotlib plotting with tox and py.test?</p>
  
  <p>My files are:</p>
  
  <pre><code>testtox /
    - testtox.py
    - tox.ini
    - setup.py
  </code></pre>
  
  <p>testtox.py contains:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  
  def plotting():
      """Plot.
  
      Examples
      --------
      &gt;&gt;&gt; plt.ion()
      &gt;&gt;&gt; plotting()
  
      """
      plt.plot([1, 2, 1, 3])
  </code></pre>
  
  <p>tox.ini:</p>
  
  <pre><code>[tox]
  envlist = py27
  
  [testenv:py27]
  sitepackages = True
  commands = py.test --doctest-modules testtox.py
  deps = pytest
  </code></pre>
  
  <p>setup.py:</p>
  
  <pre><code>from setuptools import setup
  
  setup(name='testtox', py_modules=['testtox'])
  </code></pre>
  
  <p>py.test on its own works ok: </p>
  
  <pre><code>$ py.test --doctest-modules testtox.py
  </code></pre>
  
  <p>In this case the plot window is briefly flashed.</p>
  
  <p>tox produces an error on the DISPLAY variable: </p>
  
  <pre><code>$ tox
  [...cut ...]
  =================================================== FAILURES ====================================================
  __________________________________________ [doctest] testtox.plotting ___________________________________________
  005     """Plot.
  006 
  007     Examples
  008     --------
  009     &gt;&gt;&gt; plt.ion()
  010     &gt;&gt;&gt; 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
  </code></pre>
  
  <p>This error happened with the most recent version of tox (2.1.1). An older version did not produce the error. </p>