6

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.

screenshot of terminal output

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Tracy
  • 1,988
  • 5
  • 25
  • 36

1 Answers1

11

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)

Devin Jeanpierre
  • 92,913
  • 4
  • 55
  • 79
  • thank you very much. I follow your advice to modify the initial text file __extremely__ carefully and now it works though i have not yet tried the **flags** you talk about. I might take a try as my learning moves on. – Tracy Aug 31 '11 at 15:53
  • It worth to note that this flag also cause exception message to be ignored. – ony Sep 28 '16 at 11:58