Skip to content

Commit 73e55f6

Browse files
authored
Merge pull request #38 from dopplershift/set-backend
ENH: Support setting the backend per-test. (Fixes #35)
2 parents 8b1e15e + 9f73275 commit 73e55f6

File tree

13 files changed

+20
-17
lines changed

13 files changed

+20
-17
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ env:
1313
- CONDA_DEPENDENCIES="pytest matplotlib nose coverage"
1414
- PIP_DEPENDENCIES="pytest-cov coveralls"
1515
matrix:
16-
- PYTHON_VERSION=2.6 MATPLOTLIB_VERSION=1.4 NUMPY_VERSION=1.9
17-
- PYTHON_VERSION=2.7 MATPLOTLIB_VERSION=1.4 NUMPY_VERSION=1.9
18-
- PYTHON_VERSION=3.3 MATPLOTLIB_VERSION=1.4 NUMPY_VERSION=1.9
19-
- PYTHON_VERSION=3.4 MATPLOTLIB_VERSION=1.4 NUMPY_VERSION=1.9
16+
- PYTHON_VERSION=3.4 MATPLOTLIB_VERSION=1.5
2017
- PYTHON_VERSION=2.7 MATPLOTLIB_VERSION=1.5
2118
- PYTHON_VERSION=3.5 MATPLOTLIB_VERSION=1.5
2219
- PYTHON_VERSION=2.7 MATPLOTLIB_VERSION=2.0
@@ -40,7 +37,7 @@ install:
4037
script:
4138
- python -c 'import pytest_mpl.plugin'
4239
- py.test --mpl --cov pytest_mpl tests
43-
- if [[ $PYTHON_VERSION != 2.6 ]]; then python setup.py check --restructuredtext; fi
40+
- python setup.py check --restructuredtext
4441

4542
after_success:
4643
- coveralls

appveyor.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@ environment:
1313
CONDA_DEPENDENCIES: "pytest matplotlib nose"
1414

1515
matrix:
16-
- PYTHON_VERSION: "2.6"
17-
MATPLOTLIB_VERSION: "1.4"
18-
1916
- PYTHON_VERSION: "2.7"
2017
MATPLOTLIB_VERSION: "1.5"
2118

22-
- PYTHON_VERSION: "3.3"
23-
MATPLOTLIB_VERSION: "1.5"
24-
2519
- PYTHON_VERSION: "3.4"
2620
MATPLOTLIB_VERSION: "1.5"
2721

pytest_mpl/plugin.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from functools import wraps
3232

33+
import contextlib
3334
import os
3435
import sys
3536
import shutil
@@ -95,6 +96,19 @@ def pytest_configure(config):
9596
results_dir=results_dir))
9697

9798

99+
@contextlib.contextmanager
100+
def switch_backend(backend):
101+
import matplotlib
102+
import matplotlib.pyplot as plt
103+
prev_backend = matplotlib.get_backend().lower()
104+
if prev_backend != backend.lower():
105+
plt.switch_backend(backend)
106+
yield
107+
plt.switch_backend(prev_backend)
108+
else:
109+
yield
110+
111+
98112
class ImageComparison(object):
99113

100114
def __init__(self, config, baseline_dir=None, generate_dir=None, results_dir=None):
@@ -123,6 +137,7 @@ def pytest_runtest_setup(self, item):
123137
savefig_kwargs = compare.kwargs.get('savefig_kwargs', {})
124138
style = compare.kwargs.get('style', 'classic')
125139
remove_text = compare.kwargs.get('remove_text', False)
140+
backend = compare.kwargs.get('backend', 'agg')
126141

127142
if MPL_LT_15 and style == 'classic':
128143
style = os.path.join(os.path.dirname(__file__), 'classic.mplstyle')
@@ -144,7 +159,7 @@ def item_function_wrapper(*args, **kwargs):
144159

145160
baseline_remote = baseline_dir.startswith('http')
146161

147-
with plt.style.context(style):
162+
with plt.style.context(style), switch_backend(backend):
148163

149164
# Run test and get figure object
150165
import inspect

tests/baseline/1.4.x/test_dpi.png

-3.04 KB
Binary file not shown.
-17.2 KB
Binary file not shown.
-4.39 KB
Binary file not shown.
-5.14 KB
Binary file not shown.
-7.81 KB
Binary file not shown.
-12.4 KB
Binary file not shown.
-17.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)