Possible to include \n chars in doctest code samples or output?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Jul 16 02:06:47 EDT 2010
On Fri, 16 Jul 2010 01:26:40 -0400, python wrote:
> Thomas,
>
>> Recall that doctest doesn't parse the code, it extracts the docstrings.
>> And docstrings are just strings, and are parsed like strings.
>
> I understand what you're saying, but I'm struggling with how to
> represent the following strings in doctest code and doctest results. No
> matter what combination of backslashes or raw strings I use, I am unable
> to find a way to code the following.
>
>>>> encode( '", \, \t, \n' )
> '\", \\, \\t, \\n'
I don't believe that this is a valid example. The interactive interpreter
prints the repr() of objects unless you explicitly call print, which you
have not done. I don't believe that there is any string whose repr() is
what you have given. Consequently, there's no way to get that output in
the interpreter (without using print), and the doctest MUST fail.
But even if I'm wrong... when you run up against the limitations of
doctests, don't use doctests. Is there any reason why you need to show
that specific example out of the infinite number of possible examples?
Remember that doctests aren't intended for a comprehensive test suite.
Use unit tests for that. Or put the tests in a plain text file, not a
docstring, and point doctest at that. That will reduce the amount of
awkward escaping needed.
If you insist on using that example, including it in the docstring should
be simple. Call up the interactive interpreter, and run this:
>>> from mymodule import encode # whatever your module happens to be
>>> encode( '", \, \t, \n' )
something prints here
Now copy the last two lines (the encode line, and the output). Paste it
into your docstring, adjusting the indentation if needed. Then edit the
two lines, doubling EVERY backslash. That should do it.
If it doesn't, please show us the code of the encode function, the
doctest failure, and the two lines copied from the interactive
interpreter.
--
Steven
More information about the Python-list
mailing list