Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert trailing spaces in a doctest, so that it doesn't fail even when actual and expected result look the same?

I'm trying to do a doctest. The 'Expected' and 'Got' results are identical, but my doctest still fails. It's failing because there are trailing spaces after x-axis y-axis in the printout which I haven't included in my docstring. How do I include it though? When I insert the spaces manually, and do the test, it runs successfully as long as I keep the cursor there.

x-axis y-axis______________________[cursor here]

However, if I run the test with my cursor somewhere else, then the trailing spaces get removed and the test fails.

I know that sounds really strange, but it is what it is!

This is the code:

import pandas as pd
import doctest


class NewDataStructure(pd.DataFrame):
    """
    >>> arrays = [[1, 1, 2, 2], [10, 20, 10, 20]]
    >>> index = pd.MultiIndex.from_arrays(arrays, names=('x-axis', 'y-axis'))
    >>> data_input = {"Pressure (Pa)": [1+1j, 2+2j, 3+3j, 4+4j],
    ...               "Temperature": [1, 2, 3, 4]}
    >>> new_data_variable = NewDataStructure(data=data_input, index=index, title="Pressures and temperatures")
    >>> print new_data_variable
    New Data Structure Pressures and temperatures:
                   Pressure (Pa)  Temperature
    x-axis y-axis                            
    1      10             (1+1j)            1
           20             (2+2j)            2
    2      10             (3+3j)            3
           20             (4+4j)            4

    """
    def __init__(self, data, index, title):
        super(NewDataStructure, self).__init__(data=data, index=index)
        self.title = title

    def __str__(self):
        return "New Data Structure {}:\n{}".format(self.title, super(NewDataStructure, self).__str__())

doctest.testmod()

Below is my result when it fails. Even on here you should be able to select the area after x-axis y-axis and detect whether there are trailing spaces or not.

Failed example:
    print new_data_variable
Expected:
    New Data Structure Pressures and temperatures:
                   Pressure (Pa)  Temperature
    x-axis y-axis
    1      10             (1+1j)            1
           20             (2+2j)            2
    2      10             (3+3j)            3
           20             (4+4j)            4
Got:
    New Data Structure Pressures and temperatures:
                   Pressure (Pa)  Temperature
    x-axis y-axis                            
    1      10             (1+1j)            1
           20             (2+2j)            2
    2      10             (3+3j)            3
           20             (4+4j)            4
like image 833
bluprince13 Avatar asked Feb 21 '17 16:02

bluprince13


People also ask

What is the correct way to run all the doctest in a given file?

Right click on a blank space in the python code, and there is a menu option to run all the Doctests found in the file, not just the tests for one function.


1 Answers

I found a solution, using the normalize white space flag

put it either in the doctest as

    >>> print new_data_variable # doctest: +NORMALIZE_WHITESPACE

or when calling the doctest

doctest.testmod( optionflags= doctest.NORMALIZE_WHITESPACE ) 
like image 198
Copperfield Avatar answered Oct 07 '22 16:10

Copperfield