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
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.
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With