我有一个 doctest 测试浮点转换:
>>> float('fish')
在 Python < 2.7 中,这会导致:
ValueError: invalid literal for float(): fish
在 Python 2.7 中,结果是
ValueError: could not convert string to float: fish
我可以在我的 doctest 中使这两个结果都可以接受吗?
您正在寻找doctest.IGNORE_EXCEPTION_DETAIL选项。该文档有一个很好的例子来说明如何使用它。您还可以像通配符一样在 doctest 中使用省略号常量。
像这样的东西作为doctest:
>>> float('fish')
ValueError:...
您可以在此处查看 Alex Martellis 发布的关于此内容的帖子。
是的,像这样:
>>> float('fish') #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
ValueError:
在这里寻找原因。