2017-02-21 16 views
12

我正在尝试做一个doctest。 '预期'和'有'结果是相同的,但我的文档测试仍然失败。这是失败的,因为打印输出中有x-axis y-axis后面有空格,我没有包含在我的文档字符串中。我如何包含它?当我手动插入空格并进行测试时,只要将光标保持在那里,它就会成功运行。如何在doctest中插入尾随空格,使其即使在实际和预期结果看起来相同时也不会失败?

X轴Y轴______________________ [这里光标]

但是,如果我有我的光标运行测试别的地方,那么后面的空格去掉获取和测试失败。

我知道这听起来很奇怪,但它是它是什么!

这是代码:

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() 

下面是我的结果,当它失败。即使在这里,您应该可以选择x-axis y-axis之后的区域,并检测是否存在尾随空格。

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 
+0

当'因为空格被删除保存脚本。“什么? –

+0

@MadPhysicist我纠正了我的问题。 – bluprince13

+0

这是你自己的代码,使桌子?如果是这样,让它抑制输出中的尾随空格。 –

回答

10

我找到了一个解决方案,使用normalize white space flag

把它无论是在文档测试作为

>>> print new_data_variable # doctest: +NORMALIZE_WHITESPACE 

或调用文档测试

doctest.testmod(optionflags= doctest.NORMALIZE_WHITESPACE) 
相关问题