python - 如何在 docutils 中处理空值

标签 python doctest

我正在尝试在处理空值的函数上运行 doctest。但是 doctest 似乎不喜欢空值......

def do_something_with_hex(c):
    """
    >>> do_something_with_hex('\x00')
    '\x00'
    """
return repr(c)

import doctest
doctest.testmod()

我看到了这些错误

Failed example:
    do_something_with_hex(' ')
Exception raised:
    Traceback (most recent call last):
      File "C:\Python27\lib\doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
    TypeError: compile() expected string without null bytes
**********************************************************************

我该怎么做才能在这样的测试用例中允许空值?

最佳答案

您可以转义所有反斜杠,或者将您的文档字符串更改为 raw string literal :

def do_something_with_hex(c):
    r"""
    >>> do_something_with_hex('\x00')
    '\x00'
    """
    return repr(c)

如果字符串上有 r 前缀,则反斜杠后面的字符会原封不动地包含在字符串中,并且所有反斜杠都保留在字符串中

关于python - 如何在 docutils 中处理空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710987/

相关文章:

python - Pandas - 根据多种条件更新列 - 按方法分组

python - 在另一个装饰器之前使用 PyQt 4's ' pyqtSlot' 装饰器的奇怪行为

python - win32gui:如何获取窗口的状态栏文本?

python - 禁用 Nose 运行设置()

c++ - 如何对assert()失败进行单元测试?

python-3.x - 为什么 doctest 跳过对导入方法的测试?

python - 在Python中添加新的字符串方法

python - python doctest 可以忽略一些输出行吗?

Python 文档测试 : Skip entire block?

python - pysvn 给出错误 : Not a working copy