|
Size: 1535
Comment: Solution needed
|
← Revision 3 as of 2008-11-15 13:59:45 ⇥
Size: 1852
Comment: converted to 1.6 markup
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 3: | Line 3: |
| <<TableOfContents>> |
|
| Line 4: | Line 6: |
| '''*** Solution Needed ***''' |
|
| Line 36: | Line 36: |
| The solution is to add the command continuation characters: {{{#!python >>> html = ''' ... <html> ... <head> ... <title>This is the page title</title> ... </head> ... <body> ... This is the body ... </body> ... </html> ... ''' >>> title = get_html_title(html) >>> assert(title == "This is the page title") }}} |
The DocTest module requires special actions / processing for multi-line strings.
Multi-Line Strings in commands
Consider a function get_html_title designed to extract the title from an HTML page. Here is some python code to test the function:
Putting this directly into a doctest results in an exception, such as:
Failed example:
html = '''
Exception raised:
Traceback (most recent call last):
File "C:\Python24\lib\doctest.py", line 1243, in __run
compileflags, 1) in test.globs
File "<doctest sample.tests[1]>", line 1
html = '''
^
SyntaxError: EOF while scanning triple-quoted stringThe solution is to add the command continuation characters:
Multi-Line Strings in output
Blank lines in the output need to be specially handled. For example, the following doctest will fail:
This is because the blank line is used to seperate commands and comments. With Python 2.4, a <BLANKLINE> keyword was added, so the proper doctest is now:
