嵌套文档字符串的 Doctest

发布于 2024-12-15 02:52:52 字数 1780 浏览 3 评论 0原文

假设我有以下代码:

def foo(s):
    """A dummy function foo. For example:

>>> a = '''This is a test string line 1
This is a test string line 2
This is a test string line 3'''
>>> foo(a)
This is a test string line 1
This is a test string line 2
This is a test string line 3
>>>
    """
    print s

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

我们将其保存为 foo.py。当我运行时:

C:\Python27>python.exe foo.py
**********************************************************************
File "foo.py", line 5, in __main__.foo
Failed example:
    a = '''This is a test string line 1
Exception raised:
    Traceback (most recent call last):
      File "C:\Python27\lib\doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.foo[0]>", line 1
        a = '''This is a test string line 1
                                          ^
    SyntaxError: EOF while scanning triple-quoted string literal
**********************************************************************
File "foo.py", line 8, in __main__.foo
Failed example:
    foo(a)
Exception raised:
    Traceback (most recent call last):
      File "C:\Python27\lib\doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.foo[1]>", line 1, in <module>
        foo(a)
    NameError: name 'a' is not defined
**********************************************************************
1 items had failures:
   2 of   2 in __main__.foo
***Test Failed*** 2 failures.

已尝试缩进文档字符串(>> a = '''...'''。已检查所有缩进 - 每个缩进 4 个空格)并将单引号更改为双引号(>> ;> a = """..."""),错误不同,文档测试不会成功。目前唯一可行的方法是将所有行连接到一个极长的字符串并用 '\r\n' 分隔。

我错过了什么吗?

Suppose I have following code:

def foo(s):
    """A dummy function foo. For example:

>>> a = '''This is a test string line 1
This is a test string line 2
This is a test string line 3'''
>>> foo(a)
This is a test string line 1
This is a test string line 2
This is a test string line 3
>>>
    """
    print s

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

And let's save it as foo.py. When I run:

C:\Python27>python.exe foo.py
**********************************************************************
File "foo.py", line 5, in __main__.foo
Failed example:
    a = '''This is a test string line 1
Exception raised:
    Traceback (most recent call last):
      File "C:\Python27\lib\doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.foo[0]>", line 1
        a = '''This is a test string line 1
                                          ^
    SyntaxError: EOF while scanning triple-quoted string literal
**********************************************************************
File "foo.py", line 8, in __main__.foo
Failed example:
    foo(a)
Exception raised:
    Traceback (most recent call last):
      File "C:\Python27\lib\doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.foo[1]>", line 1, in <module>
        foo(a)
    NameError: name 'a' is not defined
**********************************************************************
1 items had failures:
   2 of   2 in __main__.foo
***Test Failed*** 2 failures.

Have tried indent the docstring ( >>> a = '''...'''. Have checked all indents - 4 spaces for each indent) and changed single quote to double quote (>>> a = """...."""), the errors are different and the doctest just won't go successfully. Currently the only thing work is to join all lines to a extreme long string and separate with '\r\n'.

Do I miss something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-12-22 02:52:52

我认为你需要在那里放一些点

>>> a = """This is a test string line 1
... This is a test string line 2
... This is a test string line 3"""

I think you need to put some dots there

>>> a = """This is a test string line 1
... This is a test string line 2
... This is a test string line 3"""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文