有没有办法只执行 doctests,而忽略打印函数调用? [英] Is there way to only perform the doctests, ignoring print function calls?

查看:32
本文介绍了有没有办法只执行 doctests,而忽略打印函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设地说,我的函数返回一个值并且有很多打印语句(可能有 100 个或更多).

Hypothetically speaking, my function returns a value and has lot of print statements (maybe 100 or more).

有没有办法运行 doctest这样可以忽略/跳过所有其他打印工作(我熟悉 +SKIP 指令,用于跳过 doctest 示例),即当我执行我的函数时(或将我的模块作为脚本运行)与 doctests:

Is there a way to run doctest such that all the other printing work can be ignored/skipped (I am familiar with the +SKIP directive, which is for skipping doctest examples), i.e. when I execute my function (or run my module as a script) with doctests:

python mymodule.py

或者:

python -m doctest mymodule.py

我应该得到:

  • 没有,如果成功;或
  • 任何测试示例失败时的错误消息;

没有别的.运行 doctest 不应该给我一个充满来自那些 print 函数调用的输出/文本的终端窗口.

and nothing else. Running doctest should not give me a terminal window full of outputs / text from those print function calls.

请不要建议使用单元测试(例如 unittest) 因为它会扼杀问题的本质.

Please don't suggest using unit testing (e.g. unittest) as it'll kill the essence of the question.

推荐答案

doctest 使用 stdoutnot stderr>, 显示来自任何失败测试的消息.因此,您不能像这个答案最初建议的那样修补 stdout - 这将抑制您的 print 调用 来自 doctest.

doctest uses stdout, not stderr, to show messages from any failing tests. Therefore you cannot patch out stdout as this answer originally suggested - this will suppress your print calls and any messages from doctest.

一种选择是定义带有附加 verbose 参数的 print 函数,以便您可以在必要时抑制这种情况.

One option is to define functions that print with an additional verbose parameter, so that you can suppress this when necessary.

def foo(verbose=True):
    """Does whatever.

        >>> foo(verbose=False)

    """
    if verbose:
        print('Hello world')

尽管您必须更改功能,但这也为您在不测试时提供了有用的选项.

Although you have to change the functions, this also gives you useful options when not testing.

另一个是明确地为使用它的函数提供适当的print函数,允许你在运行时传递一个NOOP:

Another is to explicitly supply the appropriate print function to the functions that use it, allowing you to pass a NOOP at runtime:

def bar(print=print):
    """Does whatever.

        >>> bar(print=lambda *args, **kwargs: None)

    """
    print('Hello world')

这也需要对函数定义进行更改,但至少可以避免对这些函数的主体进行更改.

This also requires changes to function definitions, but at least avoids changes in the bodies of those functions.

第三个选项是为整个被测模块修补print,例如:

A third option is to patch out print for the whole module under test, for example:

def baz():
    """Does whatever.

        >>> baz()

    """
    print('Hello world')

if __name__ == '__main__':

    import doctest

    print = lambda *args, **kwargs: None

    doctest.testmod()

请注意,这也会影响 doctest 看到的输出,因此您不要在文档字符串中包含任何 print 输出(我认为这是个好消息!) 但是它不适用于 python -m doctest mymodule.py.

Note that this affects the outputs that doctest sees, too, so you don't include any of the print output in the docstring (I assume this is good news!) It won't work with python -m doctest mymodule.py, though.

这篇关于有没有办法只执行 doctests,而忽略打印函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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