File: test_classes.py

package info (click to toggle)
pytest-mpl 0.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,132 kB
  • sloc: python: 2,514; javascript: 179; makefile: 16
file content (93 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import matplotlib.pyplot as plt
import pytest

from pytest_mpl.plugin import switch_backend

from .helpers import bar, figure_test


class TestClass:

    @figure_test
    def test_hmatch_imatch_testclass(self):
        return bar([1, 2, 3, 4])

    @figure_test
    def test_hdiff_idiff_testclass(self):
        return bar([1, 3, 2, 4])


class TestClassWithSetupMethod:

    def setup_method(self, method):
        self.x = [4, 2, 3, 1]

    @figure_test
    def test_hmatch_imatch_testclasswithsetupmethod(self):
        return bar(self.x)

    @figure_test
    def test_hdiff_idiff_testclasswithsetupmethod(self):
        return bar(self.x)


class TestClassWithSetupClass:

    @classmethod
    def setup_class(cls):
        cls.x = [1, 0, 3, 2]

    @figure_test
    def test_hmatch_imatch_testclasswithsetupclass(self):
        return bar(self.x)

    @figure_test
    def test_hdiff_idiff_testclasswithsetupclass(self):
        return bar(self.x)


@pytest.fixture()
def figure_axes():
    with plt.style.context("classic", after_reset=True), switch_backend("agg"):
        fig = bar([3, 0, 4, 1])
    yield fig.gca()
    # Should not appear in test result
    fig.gca().plot([3, 0, 4, 1], c="yellow")


class TestClassWithFixture:
    @figure_test
    def test_hmatch_imatch_testclasswithfixture(self, figure_axes):
        figure_axes.plot([4, 1, 5, 2], c="red")
        return figure_axes.get_figure()


def generate_two_figures():
    for num, line in ((1, [1, 0, 1, 0]), (2, [0, 1, 0, 1])):
        plt.close(num)
        fig = plt.figure(num)
        ax = fig.add_subplot(1, 1, 1)
        ax.plot(line)


class TestMultipleFigures:
    """
    Test figures are accessible, and can be passed to individual functions.

    See https://github.com/matplotlib/pytest-mpl/issues/133
    """
    @classmethod
    def setup_class(cls):
        # setting style and backend inside decorator has no effect
        # because figures are generated outside the test function
        with plt.style.context("classic", after_reset=True), switch_backend("agg"):
            generate_two_figures()
            cls.figs = [plt.figure(i) for i in (1, 2)]

    @figure_test
    def test_hmatch_imatch_multiplefigures_first(self):
        return self.figs[0]

    @figure_test
    def test_hmatch_imatch_multiplefigures_second(self):
        return self.figs[1]