如何在不显示matplotlib图的情况下进行鼻子测试? [英] How to run nosetests without showing of my matplotlib's graph?

查看:99
本文介绍了如何在不显示matplotlib图的情况下进行鼻子测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试运行测试,但主程序中未显示任何消息.我只希望显示来自鼻子测试的详细消息.

I try to run my test without any messages displaying from my main program. I only want verbose messages from nosetests to display.

例如:

nosetests -v --nologcapture

nosetests -v --nologcapture

我所有来自主程序的打印输出消息都将消失.

All of my printout messages from my main program will be gone.

但是,我在主程序(来自matplotlib的plt.show())中调用的图形仍然显示.

However, the graph that I call in my main program (plt.show() from matplotlib) still shows up.

如何在不显示matplotlib图的情况下运行测试?

How do I run the tests without matplotlib's graph showing up?

推荐答案

我假设您正在对代码调用unittests,所以我的建议是为您安装python

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 ...

patch函数将使用此新的mock_show替代常规的show函数,您可以将其命名为任何内容.基本上,这应该使该展示现在在您的测试中不执行任何操作,并且不会显示该图.

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.

这篇关于如何在不显示matplotlib图的情况下进行鼻子测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆