2013-06-13 7 views
6

In Python (3.3.2) Doctest, Ellipsen (...) kann eine beliebige Zeichenfolge übereinstimmen. Also für den Code unterWie aktiviert Ellipsen beim Aufruf von Python doctest

def foo(): 
    """ 
    >>> foo() 
    hello ... 
    """ 
    print("hello world") 

wenn Doctest ausgeführt wird, sollte es keinen Fehler auslösen. Aber

$ python -m doctest foo.py 
********************************************************************** 
File "./foo.py", line 3, in foo.foo 
Failed example: 
    foo() 
Expected: 
    hello ... 
Got: 
    hello world 
********************************************************************** 
1 items had failures: 
    1 of 1 in foo.foo 
***Test Failed*** 1 failures. 

Was muss ich tun, um die Ellipse zu aktivieren? Soweit ich das beurteilen kann, ist es standardmäßig deaktiviert.

Ich weiß, dass hinzufügen # doctest: +ELLIPSIS, wie im folgenden Code, es zu lösen, aber ich möchte Ellipsen für alle Tests aktivieren.

def foo(): 
    """ 
    >>> foo() # doctest: +ELLIPSIS 
    hello ... 
    """ 
    print("hello world") 

Antwort

10

Sie in optionflags zum testmod Methode übergeben kann, aber dies erfordert, dass Sie das Modul selbst, anstatt die doctest Modul auszuführen:

def foo(): 
    """ 
    >>> foo() 
    hello ... 
    """ 
    print("hello world") 

if __name__ == "__main__": 
    import doctest 
    doctest.testmod(verbose=True, optionflags=doctest.ELLIPSIS) 

Ausgang:

$ python foo.py 
Trying: 
    foo() 
Expecting: 
    hello ... 
ok 
1 items had no tests: 
    __main__ 
1 items passed all tests: 
    1 tests in __main__.foo 
1 tests in 2 items. 
1 passed and 0 failed. 
Test passed. 
Verwandte Themen