【问题标题】:Doctest: Splitting a docstring for Python function call over multiple linesDoctest:将 Python 函数调用的文档字符串拆分为多行
【发布时间】:2015-02-25 01:53:42
【问题描述】:

我想知道是否有一种方法可以将函数调用拆分为多行以进行 doctest。例如,对于类似的东西

>>> result = some_func(some_param=1, another_param=4, a_third_param='super_long_string')

我试过了

>>> result = some_func(some_param=1, another_param=4, 
    ...     a_third_param='super_long_string')


>>> result = some_func(some_param=1, another_param=4, \
                       a_third_param='super_long_string')

或者

>>> result = some_func(#doctest: +NORMALIZE_WHITESPACE
             some_param=1, 
             another_param=4, 
             a_third_param='super_long_string')

但两者都行不通。有什么想法或提示吗?

编辑: 我正在通过nosetests -sv --with-doctest 运行文档测试

【问题讨论】:

    标签: python nosetests docstring doctest


    【解决方案1】:

    您的第二个示例非常接近对我有用的示例。在我放置一组省略号后,我添加了五个空格。

    之所以我加五个空格,是因为第一个空格匹配提示符,其他四个匹配缩进级别。

    这是一个使用 python 2.7 和您提供的函数对我有用的示例。

    def some_func(some_param, another_param, a_third_param):
        """
        Does something at least some of the time.
    
        Examples
        --------
    
        >>> some_func(
        ...     some_param=1,
        ...     another_param=4,
        ...     a_third_param='super_long_string')
        'Worked'
        """
        return 'Worked'
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    

    【讨论】:

    • 对不起,这是我上面例子中的错字。我还尝试了... 之后的 5 个空格,但has inconsistent leading whitespace 仍然是同样的问题,但感谢您的帮助!
    • 我猜这个问题是nosetests -sv --with-doctest 命令特有的。我正在使用 python 3.4.0 和鼻子 1.3.4
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多