如何使用绘图通过Python doctest

如何使用绘图通过Python doctest,python,matplotlib,doctest,Python,Matplotlib,Doctest,当输出为matplotlib对象时,是否有方法或技巧通过doctest?我还将Doctest框架用于代码示例(不仅用于测试输出) 所以我的问题是这样的: plt.plot(grid, pdf); plt.title('Random Normal 1D using Kernel1D.kde function'); plt.grid(); plt.show() Expected nothing Got: [<matplotlib.lines.Line2D object at 0

当输出为matplotlib对象时,是否有方法或技巧通过doctest?我还将Doctest框架用于代码示例(不仅用于测试输出)

所以我的问题是这样的:

    plt.plot(grid, pdf); plt.title('Random Normal 1D using Kernel1D.kde function'); plt.grid(); plt.show()
Expected nothing
Got:
    [<matplotlib.lines.Line2D object at 0x00000208BDA8E2B0>]
    Text(0.5, 1.0, 'Random Normal 1D using Kernel1D.kde function')
plt.plot(网格,pdf);plt.title('Random Normal 1D using Kernel1D.kde function');plt.grid();plt.show()
什么也不期望
得到了:
[]
文本(0.5、1.0,‘使用Kernel1D.kde函数的随机法线1D’)
我想做的是,当我策划任何事情时,通过博士测试。谢谢。

您可以使用它使字符串与任何内容匹配

如果希望避免看到绘图并直接转到评估报告,则代码仍将在
plt.show()
中显示问题。你可以用它。检查以下示例:

import matplotlib.pyplot as plt

def test():
    """
    Code example:

    >>> 1 + 1
    2
    >>> plt.plot([1, 2, 3])
    [...
    >>> plt.show() #doctest: +SKIP
    >>> plt.close()
    """
    pass

if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True, optionflags=doctest.ELLIPSIS)
这将返回以下报告:

Trying:
    1 + 1
Expecting:
    2
ok
Trying:
    plt.plot([1, 2, 3])
Expecting:
    [...
ok
Trying:
    plt.close()
Expecting nothing
ok
1 items had no tests:
    __main__
1 items passed all tests:
   3 tests in __main__.test
3 tests in 2 items.
3 passed and 0 failed.
Test passed.