【问题标题】:Python: Why does this doc test fail?Python:为什么这个文档测试失败了?
【发布时间】:2010-01-09 23:38:37
【问题描述】:

doctest 中的这段代码在单独运行时可以工作,但在这个 doctest 中它在 10 个地方失败。我不知道为什么会这样。以下是整个模块:

class requireparams(object):
    """
    >>> @requireparams(['name', 'pass', 'code'])
    >>> def complex_function(params):
    >>>     print(params['name'])
    >>>     print(params['pass'])
    >>>     print(params['code'])
    >>> 
    >>> params = {
    >>>     'name': 'John Doe',
    >>>     'pass': 'OpenSesame',
    >>>     #'code': '1134',
    >>> }
    >>> 
    >>> complex_function(params)
    Traceback (most recent call last):
        ...
    ValueError: Missing from "params" argument: code
    """
    def __init__(self, required):
        self.required = set(required)

    def __call__(self, params):
        def wrapper(params):
            missing = self.required.difference(params)
            if missing:
                raise ValueError('Missing from "params" argument: %s' % ', '.join(sorted(missing)))
        return wrapper

if __name__ == "__main__":
    import doctest
    doctest.testmod()

【问题讨论】:

    标签: python doctest docstring


    【解决方案1】:

    doctest 要求您将... 用于续行:

    >>> @requireparams(['name', 'pass', 'code'])
    ... def complex_function(params):
    ...     print(params['name'])
    ...     print(params['pass'])
    ...     print(params['code'])
    ...
    >>> params = {
    ...     'name': 'John Doe',
    ...     'pass': 'OpenSesame',
    ...     #'code': '1134',
    ... }
    ...
    >>> complex_function(params)
    

    【讨论】:

      【解决方案2】:

      尝试完全从 python 提示符粘贴代码。这意味着它将包含一些...>>>。否则 doctest 解析器将不知道何时存在多行表达式。

      预览:Greg 所说的。

      【讨论】:

        【解决方案3】:

        这是我更正后的模块(现在可以使用):

        class requiresparams(object):
            """
        
            Used as a decorator with an iterable passed in, this will look for each item
            in the iterable given as a key in the params argument of the function being
            decorated. It was built for a series of PayPal methods that require
            different params, and AOP was the best way to handle it while staying DRY.
        
        
            >>> @requiresparams(['name', 'pass', 'code'])
            ... def complex_function(params):
            ...     print(params['name'])
            ...     print(params['pass'])
            ...     print(params['code'])
            >>> 
            >>> params = {
            ...     'name': 'John Doe',
            ...     'pass': 'OpenSesame',
            ...     #'code': '1134',
            ... }
            >>> 
            >>> complex_function(params)
            Traceback (most recent call last):
                ...
            ValueError: Missing from "params" argument: code
            """
            def __init__(self, required):
                self.required = set(required)
        
            def __call__(self, params):
                def wrapper(params):
                    missing = self.required.difference(params)
                    if missing:
                        raise ValueError('Missing from "params" argument: %s' % ', '.join(sorted(missing)))
                return wrapper
        
        if __name__ == "__main__":
            import doctest
            doctest.testmod()
        

        【讨论】:

          猜你喜欢
          • 2014-05-31
          • 2021-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多