在 doctest 中包含原始制表符文字字符
Include raw tab literal character in doctest
我不知道如何避免这个 doctest 错误:
Failed example:
print(test())
Expected:
output
<BLANKLINE>
Got:
output
<BLANKLINE>
对于此代码
def test():
r'''Produce string according to specification.
>>> print(test())
output
<BLANKLINE>
'''
return '\toutput\n'
我在源代码第 5 行 output
.
前面放了一个制表符文字
看起来 doctest(或 python 文档字符串?)忽略了制表符文字并将其转换为四个空格。
所谓的“预期”值实际上不是我的来源指定的值。
有什么解决方案?
我不想将打印语句替换为
>>> test()
'\toutput\n'
因为我通常喜欢 doctests 的很大一部分原因是因为它们演示了示例,而我正在编写的这个函数最重要的部分是输出的 shape。
文档字符串中的制表符扩展为 8 个空格,但输出中的制表符未扩展。
来自doctest documentation(强调已添加):
All hard tab characters are expanded to spaces, using 8-column tab stops. Tabs in output generated by the tested code are not modified. Because any hard tabs in the sample output are expanded, this means that if the code output includes hard tabs, the only way the doctest can pass is if the NORMALIZE_WHITESPACE
option or directive is in effect. Alternatively, the test can be rewritten to capture the output and compare it to an expected value as part of the test. This handling of tabs in the source was arrived at through trial and error, and has proven to be the least error prone way of handling them. It is possible to use a different algorithm for handling tabs by writing a custom DocTestParser
class.
不幸的是,在 doctest 文档中使用指令的所有示例都是 mangled by Sphinx。以下是您将如何使用 NORMALIZE_WHITESPACE
:
def test():
r'''Produce string according to specification.
>>> print(test()) # doctest: +NORMALIZE_WHITESPACE
output
<BLANKLINE>
'''
return '\toutput\n'
请注意,这会将所有空格视为相等,而不是禁用制表符处理。禁用选项卡处理非常麻烦,并且通过通常的 doctest 接口是不可能的 - 您需要子类化 DocTestParser
,手动重新实现 doctest 解析,然后构造 DocTestFinder
、DocTestRunner
和的实例您的 DocTestParser
子类并手动调用它们的方法。
我不知道如何避免这个 doctest 错误:
Failed example:
print(test())
Expected:
output
<BLANKLINE>
Got:
output
<BLANKLINE>
对于此代码
def test():
r'''Produce string according to specification.
>>> print(test())
output
<BLANKLINE>
'''
return '\toutput\n'
我在源代码第 5 行 output
.
看起来 doctest(或 python 文档字符串?)忽略了制表符文字并将其转换为四个空格。
所谓的“预期”值实际上不是我的来源指定的值。
有什么解决方案?
我不想将打印语句替换为
>>> test()
'\toutput\n'
因为我通常喜欢 doctests 的很大一部分原因是因为它们演示了示例,而我正在编写的这个函数最重要的部分是输出的 shape。
文档字符串中的制表符扩展为 8 个空格,但输出中的制表符未扩展。
来自doctest documentation(强调已添加):
All hard tab characters are expanded to spaces, using 8-column tab stops. Tabs in output generated by the tested code are not modified. Because any hard tabs in the sample output are expanded, this means that if the code output includes hard tabs, the only way the doctest can pass is if the
NORMALIZE_WHITESPACE
option or directive is in effect. Alternatively, the test can be rewritten to capture the output and compare it to an expected value as part of the test. This handling of tabs in the source was arrived at through trial and error, and has proven to be the least error prone way of handling them. It is possible to use a different algorithm for handling tabs by writing a customDocTestParser
class.
不幸的是,在 doctest 文档中使用指令的所有示例都是 mangled by Sphinx。以下是您将如何使用 NORMALIZE_WHITESPACE
:
def test():
r'''Produce string according to specification.
>>> print(test()) # doctest: +NORMALIZE_WHITESPACE
output
<BLANKLINE>
'''
return '\toutput\n'
请注意,这会将所有空格视为相等,而不是禁用制表符处理。禁用选项卡处理非常麻烦,并且通过通常的 doctest 接口是不可能的 - 您需要子类化 DocTestParser
,手动重新实现 doctest 解析,然后构造 DocTestFinder
、DocTestRunner
和的实例您的 DocTestParser
子类并手动调用它们的方法。