Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctest NORMALIZE_WHITESPACE does not work

Tags:

python

doctest

Failed example:
    p.parse_name('Adams, Michael') 
    # doctest: +NORMALIZE_WHITESPACE
Expected:
    {'first_name': 'Michael', 'last_name': 'Adams','initials': 'MA'}
Got:
    {'first_name': 'Michael', 'last_name': 'Adams', 'initials': 'MA'}

The docstring is -

>>> p.parse_name('Adams, Michael') 
... # doctest: +NORMALIZE_WHITESPACE
{'first_name': 'Michael', 'last_name': 'Adams','initials': 'MA'}
like image 547
Kshitiz Sharma Avatar asked Jul 14 '13 14:07

Kshitiz Sharma


People also ask

Can doctest be used to test Docstrings?

The Doctest Module finds patterns in the docstring that looks like interactive shell commands. The input and expected output are included in the docstring, then the doctest module uses this docstring for testing the processed output.

Can we handle unpredictable output using doctest?

When the tests include values that are likely to change in unpredictable ways, and where the actual value is not important to the test results, you can use the ELLIPSIS option to tell doctest to ignore portions of the verification value.


1 Answers

From the docs:

When specified, all sequences of whitespace (blanks and newlines) are treated as equal. Any sequence of whitespace within the expected output will match any sequence of whitespace within the actual output

',' contains no sequence of whitespace, so is not treated as equal to ', '.


You might want to read the warnings section of the docs:

Python doesn’t guarantee that the key-value pairs will be printed in any particular order, so a test like

>>> foo()
{"Hermione": "hippogryph", "Harry": "broomstick"}

is vulnerable! One workaround is to do

>>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"}
True
like image 67
Eric Avatar answered Sep 23 '22 23:09

Eric