[英]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. 我正在尝试做doctest。 The 'Expected' and 'Got' results are identical, but my doctest still fails. '预期'和'得到'结果是相同的,但我的doctest仍然失败。 It's failing because there are trailing spaces after x-axis y-axis
in the printout which I haven't included in my docstring. 它失败了,因为打印输出中的x-axis y-axis
后面有尾随空格,我没有包含在我的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] x轴y轴______________________ [光标在这里]
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. 即使在这里,您也应该能够在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
I found a solution, using the normalize white space flag 我找到了一个使用normalize white space标志的解决方案
put it either in the doctest as 把它放在doctest中
>>> print new_data_variable # doctest: +NORMALIZE_WHITESPACE
or when calling the doctest 或者在调用doctest时
doctest.testmod( optionflags= doctest.NORMALIZE_WHITESPACE )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.