如何在文档测试中插入尾随空格,以便即使实际结果和预期结果看起来相同也不会失败?



我正在尝试做医生。"预期"one_answers"获得"结果是相同的,但是我的医生仍然失败。之所以失败,是因为在打印输出中有x-axis y-axis之后的落后空间,我在docstring中还没有包含。我如何包括它?当我手动插入空间并进行测试时,只要我将光标保留在那里,它就会成功运行。

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

我使用归一化的白色空间标志

找到了解决方案

将其放在医生中

>>> print new_data_variable  # doctest: +NORMALIZE_WHITESPACE

或致电医生

doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)

相关内容

最新更新