gpt4 book ai didi

python doctest异常测试处理

转载 作者:太空狗 更新时间:2023-10-29 22:23:41 27 4
gpt4 key购买 nike

我在名为 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 调用测试运行。下面的结果完全出乎我的意料。

screenshot of terminal output

我的想法是测试应该会成功,因为我已经在我的 test2.txt 文件中写入了预期的输出,它“几乎”与我从控制台输出中得到的匹配。我尝试添加 'File "G:\"'.... 行?但测试仍然失败。

最佳答案

doctest 对预期异常的格式非常谨慎。你错过了一个空间:

Traceback(most recent call last): 应该是 Traceback (most recent call last):

此外,这仍然会失败,因为您的回溯消息过于具体(并且还有不正确的空格)!将 ELLIPSISIGNORE_EXCEPTION_DETAIL 标志用于 doctest 以降低 doctest 对匹配异常的挑剔程度,如下所示:

>>> def faulty(): # doctest: +IGNORE_EXCEPTION_DETAIL  
... yield 5
... return 7
Traceback (most recent call last):
SyntaxError: 'return' with argument inside generator (...)

(ELLIPSIS 也可以在这里工作)

关于python doctest异常测试处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7245698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com