【发布时间】:2019-04-07 00:37:10
【问题描述】:
根据主题,如何为matplotlib中处理pick event handling的函数编写测试?
特别是,给定以下最小工作示例,如何编写可提供 100% 覆盖率的测试?
import numpy as np
import matplotlib.pyplot as plt
def onpick(event):
ind = event.ind
print('you clicked on point(s):', ind)
def attach_handler_to_figure(figure):
figure.canvas.mpl_connect('pick_event', onpick)
def main():
plt.ion()
x, y, c, s = np.random.rand(4, 100)
fig, ax = plt.subplots()
ax.scatter(x, y, 100*s, c, picker=True)
attach_handler_to_figure(fig)
main()
对我来说,关键部分是为函数 onpick 和 attach_handler_to_figure 编写测试。关于绘图,我觉得this answer 令人满意!
更多信息:我不喜欢测试控制台输出的方法。我所追求的是测试功能,某种test_onpick 和test_attach_handler_to_figure 和test_main(嗯,主要挑战是测试attach_handler_to_figure(fig) 行),可以由pytest 或任何其他测试使用框架。
【问题讨论】:
-
@ImportanceOfBeingErnest 不是真的,我正在寻找一种模拟点击的方法!但感谢您的意见!
标签: python unit-testing matplotlib testing