Possible to include \n chars in doctest code samples or output?

Thomas Jollans thomas at jollans.com
Thu Jul 15 17:26:59 EDT 2010


On 07/15/2010 11:14 PM, python at bdurham.com wrote:
> I'm working with some Python 2.6 code that is using the doctest module
> for unittests.
>  
> I tried adding tests whose code and results required newlines (\n).
> Apparently newlines in string constants breaks the doctest code.
>  
> Any workarounds other than using another char for newlines and wrapping
> all my strings with .replace( someChar, chr(10) )?

Recall that doctest doesn't parse the code, it extracts the docstrings.
And docstrings are just strings, and are parsed like strings.

Remember that the two following strings are identical:

"""a
b"""

"""a\nb"""

As are these two:

r"\n"
"\\n"

Anyway, you need to escape the backslash if you want backslash-n to be
in your docstring. Otherwise you'll get a newline like any other
newline, not a character sequence doctest can eval() to a newline while
processing.



More information about the Python-list mailing list