开发者

python doctest exception test handling

开发者 https://www.devze.com 2023-04-01 13:58 出处:网络
I have the following contents in a file called test2.txt. >>> def faulty(): ... yield 5 ... return 7

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.

python doctest exception test handling

My thought was th开发者_JAVA技巧at 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.


doctest is extremely careful with the format of expected exceptions. You missed a space:

Traceback(most recent call last): should be Traceback (most recent call last):

Moreover, this would still fail, as your traceback message is overly specific (and also has incorrect whitespace)! Use the ELLIPSIS or IGNORE_EXCEPTION_DETAIL flags to doctest to make doctest less picky about matching exceptions, as so:

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

(ELLIPSIS would work here too)

0

精彩评论

暂无评论...
验证码 换一张
取 消