Python Doctest 中的特殊字符和换行符

Special Characters and Line Breaks in Python Doctest

我有一个带有文档字符串的函数,如下所示,我想测试该文档字符串是否正确。我目前正在使用 doctest 模块来这样做。但是,我找不到一种方法来表示文档字符串中的新行字符和换行符而不会崩溃。这是一个复制问题的示例:

def foo():
    r"""
    >>> foo() == ['1\n2\n',\
    '3']
    True
    """
    return ['1\n2\n', '3']

import doctest
doctest.testmod()

这会导致错误:

Failed example:
foo() == ['1\n2\n',\
Exception raised:
    Traceback (most recent call last):
      File "C:\Python34\lib\doctest.py", line 1318, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.foo[0]>", line 1
        foo() == ['1\n2\n',\
                           ^
    SyntaxError: unexpected EOF while parsing

我该如何完成?

使用省略号...:

def foo():
    r"""
    >>> foo() == ['1\n2\n',
    ... '3']
    True
    """
    return ['1\n2\n', '3']

import doctest
doctest.testmod()

(source)