Disabling graphs when starting unittests

I am testing my module using a library unittest. This includes charting using the library matplotlib. The problem at the moment is that testing is paused every time the chart is applied to the chart, and it resumes only after the chart is closed. How can i avoid this?

+6
source share
3 answers

I will model my answer after a simple code example from the matplotlib tutorial: http://matplotlib.org/users/pyplot_tutorial.html

Suppose we are testing the following module plot_graph.py::

import matplotlib.pyplot as plt

def func_plot():
    plt.plot([1,2,3,4])
    plt.ylabel('some numbers')
    plt.show()

if __name__ == "__main__":
    func_plot()

show :

from plot_graph import func_plot
from unittest.mock import patch

@patch("plot_graph.plt.show")
def test_plot(mock_show):
    assert func_plot() == None

, pyplot.show(). : https://docs.python.org/3/library/unittest.mock.html.

, : https://docs.python.org/3/library/unittest.mock.html#where-to-patch

, : nosetests matplotlib?

+6

pyplot.show(), . , block=False show.

+1

Instead of hacking, you can try to use the principle of single responsibility from SOLID and write test code so that you do not encounter such problems. Just an idea ...

0
source

Source: https://habr.com/ru/post/1015989/


All Articles