Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping python doctest results that are longer than 80 characters

I'm trying to keep my source code under the 80 character guideline width that PEP8 recommends, but can't figure out how to wrap my doctest which has results longer than 80 characters.

A noddy example:

def long_string():     """     Returns a string which is wider than the recommended PEP8 linewidth      >>> print long_string()     0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789      """     return '0123456789' * 10 

I've tried a couple of combinations, including using # doctest: +NORMALIZE_WHITESPACE and trying to simply wrap the line with a newline.

like image 708
pelson Avatar asked Nov 15 '12 10:11

pelson


People also ask

Can we handle unpredictable output using doctest in Python?

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.

Can doctest be used to test Docstrings True False?

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.

What is a doctest script?

Introduction. Doctest is a simple but useful testing method for Python programs. Compared with unit test, doctest doesn't require an independent script to write test cases. Test cases can just be written in the doc information (contents within triple single/double quotes) of a Python function.


2 Answers

Just figured out:

def long_string():     """     Returns a string which is wider than the recommended PEP8 linewidth      >>> print long_string()     01234567890123456789012345678901234567890123456789012345678901234567890\ 12345678901234567890123456789      """     return '0123456789' * 10 

Hope that helps somebody else out.

like image 179
pelson Avatar answered Oct 14 '22 02:10

pelson


As suggested by davitenio and qris, I would recommend using the #doctest: +ELLIPSIS directive, like so.

>>> from test.test_ppp import MockForm >>> form = MockForm(mock_file='no-errors.xlsx') >>> form.get_languages(settings_default='English', survey_header= ... form.metadata['raw_data']['survey'][0])  #doctest: +ELLIPSIS ['Ateso', 'English', 'Luganda', ... 'Runyoro-Rutoro'] 
like image 27
Joe Flack Avatar answered Oct 14 '22 04:10

Joe Flack