python doctest异常测试处理
我在名为 test2.txt
的文件中包含以下内容。
>>> def faulty():
... yield 5
... return 7
Traceback(most recent call last):
SyntaxError: 'return' with argument inside generator(<doctest test.txt[0]>,line 3)
我使用 python -m test2.txt
调用测试运行。下面的结果完全出乎我的意料。
我的想法是测试应该成功,因为我已经在 test2 中编写了预期的输出.txt
文件,它“几乎”与我从控制台输出中得到的内容匹配。我尝试添加 'File "G:\"'.... 行
?但测试仍然失败。
I have the following contents in a file called test2.txt
.
>>> def faulty():
... yield 5
... return 7
Traceback(most recent call last):
SyntaxError: 'return' with argument inside generator(<doctest test.txt[0]>,line 3)
I invoke the test run with python -m test2.txt
. The results below are quite out of my expectation.
My thought was that the test should be successful because I have written the expected output in my test2.txt
file and it 'almost' matches what I got from the console output. I tried adding the 'File "G:\"'.... line
? but the test still failed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
doctest 对预期异常的格式非常谨慎。您错过了一个空格:
Traceback(most最近的调用最后):
应该是Traceback (most最近的调用最后):
此外,这仍然会失败,因为您的回溯消息过度具体(并且也有不正确的空格)!对 doctest 使用
ELLIPSIS
或IGNORE_EXCEPTION_DETAIL
标志,使 doctest 对匹配异常不那么挑剔,如下所示:(ELLIPSIS
也可以在这里工作)doctest is extremely careful with the format of expected exceptions. You missed a space:
Traceback(most recent call last):
should beTraceback (most recent call last):
Moreover, this would still fail, as your traceback message is overly specific (and also has incorrect whitespace)! Use the
ELLIPSIS
orIGNORE_EXCEPTION_DETAIL
flags to doctest to make doctest less picky about matching exceptions, as so:(
ELLIPSIS
would work here too)