Python 嵌套docstring的Doctest

Python 嵌套docstring的Doctest,python,doctest,docstring,Python,Doctest,Docstring,假设我有以下代码: 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

假设我有以下代码:

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.
C:\Python27>python.exe foo.py
**********************************************************************
文件“foo.py”,第5行,在_main__.foo中
失败示例:
a=''这是一个测试字符串行1
提出的例外情况:
回溯(最近一次呼叫最后一次):
文件“C:\Python27\lib\doctest.py”,第1254行,正在运行
test.globs中的compileflags,1)
文件“”,第1行
a=''这是一个测试字符串行1
^
SyntaxError:扫描三重引号字符串文字时出现EOF
**********************************************************************
文件“foo.py”,第8行,在_umain__uu.foo中
失败示例:
傅(甲)
提出的例外情况:
回溯(最近一次呼叫最后一次):
文件“C:\Python27\lib\doctest.py”,第1254行,正在运行
test.globs中的compileflags,1)
文件“”,第1行,在
傅(甲)
NameError:未定义名称“a”
**********************************************************************
1个项目出现故障:
2/2 in___main___.foo
***测试失败***2次失败。
已尝试缩进docstring(>>>a='''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''。已检查所有缩进-每个缩进4个空格)并将单引号更改为双引号(>>>>a=“''''''''''''。目前唯一可行的方法是将所有行连接到一个非常长的字符串,并用“\r\n”分隔


我错过了什么吗?

我想你需要在那里加上一些点

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

虽然我不能竖起大拇指(需要15个声誉),谢谢你的回答。你的答案很好。我只想指出,doctest需要用双引号引用。答案是docstring被引用为单引号。我把我的博士测试用一句话引述,但我不明白为什么它不起作用。@Foreigner:谢谢!顺便说一句,这个网站是合作编辑的,当你看到一个明显的错误时,请随意编辑文章并更正它。@Foreigner我有相反的想法;我的doctest结果字符串用双引号括起来,而它必须用单引号括起来。