Matplotlib plots during continuous integration

Plotting on continuous integration systems such as Travis CI, AppVeyor, GitLab, etc. can be tricky, since in many cases the CI doesn’t have an X11 display server. Workaround include:

  • Pytest conditional tests that detect CI via environment variable, totally avoiding generating plots
  • generate plots using xvfb dummy X11 display server

Xvfb

This method uses X server virtual framebuffer (Xvfb) on Travis-CI.

Add to your .travis.yml:

services: xvfb

Detect CI inside Python

Pytest handles conditional tests well.

import os
import pytest

CI = bool(os.environ.get('CI'))


@pytest.mark.skipif(CI, reason="no plots for CI")
def test_myfun():
    from matplotlib.pyplot import figure,show

    ...

Notes

CI Environment variables

These CI’s and more set the environment variable CI as a de facto standard for easy CI detection. Here are a few listings of CI system environment variables for reference: