2016-12-16 4 views
1

Unterstützt doctest, dass Ausgabe und Ausnahme zusammengemischt werden?Doctest schlägt fehl, wenn normale Ausgabe und Ausnahme zusammengemischt werden?

Ein Beispiel dafür ist:

>>> def foo(): 
... print 'hello world!' 
>>> foo() 
hello world! 
>>> def bar(): 
...  raise Exception() 
>>> bar() 
Traceback (most recent call last): 
    ... 
Exception 

>>> def foo_bar(): 
...  foo() 
...  bar() 
>>> foo_bar() 
hello world! 
Traceback (most recent call last): 
    ... 
Exception 

Ich erwarte, dass alle drei Fälle sollte erfolgreich sein, aber nur zwei von ihnen nicht finden

$ python -m doctest -v /tmp/1.py 
Trying: 
    def foo(): 
     print 'hello world!' 
Expecting nothing 
ok 
Trying: 
    foo() 
Expecting: 
    hello world! 
ok 
Trying: 
    def bar(): 
     raise Exception() 
Expecting nothing 
ok 
Trying: 
    bar() 
Expecting: 
    Traceback (most recent call last): 
     ... 
    Exception 
ok 
Trying: 
    def foo_bar(): 
     foo() 
     bar() 
Expecting nothing 
ok 
Trying: 
    foo_bar() 
Expecting: 
    hello world! 
    Traceback (most recent call last): 
     ... 
    Exception 
********************************************************************** 
File "/tmp/1.py", line 16, in 1 
Failed example: 
    foo_bar() 
Exception raised: 
    Traceback (most recent call last): 
     File "/usr/lib/python2.7/doctest.py", line 1315, in __run 
     compileflags, 1) in test.globs 
     File "<doctest 1[5]>", line 1, in <module> 
     foo_bar() 
     File "<doctest 1[4]>", line 3, in foo_bar 
     bar() 
     File "<doctest 1[2]>", line 2, in bar 
     raise Exception() 
    Exception 
********************************************************************** 
1 items had failures: 
    1 of 6 in 1 
6 tests in 1 items. 
5 passed and 1 failed. 
***Test Failed*** 1 failures. 

Antwort

1

Die docs sagen Sie das nicht tun können:

Beispiele, die sowohl die erwartete Ausgabe als auch eine Ausnahme enthalten, werden nicht unterstützt. Der Versuch, zu erraten, wo der eine endet und der andere beginnt, ist zu fehleranfällig, und das führt auch zu einem verwirrenden Test.

Verwandte Themen