How to run nosetests without showing of my matplotlib's graph?

99封情书 提交于 2019-12-04 06:47:34
Jay Atkinson

I assume that you're calling unittests on your code, so my recommendation would be for you to install the python Mock library. Any tests that will exercise the plt.show() function should mock it out to basically do nothing.

Here's an rough example of the idea inside your unittests:

from mock import patch


... unittest boiler plate stuff ...

@patch("matplotlib.pyplot.show")
def testMyCode(self, mock_show):
    mock_show.return_value = None  #probably not necessary here in your case
    ... rest of test code ...

The patch function will override the normal show function with this new mock_show which you could name to anything. This should basically make the show now do nothing in your tests and not have the graph show up.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!