2013-06-13 1 views
6

doctestでは、省略記号(...)は任意の文字列に一致します。したがって、以下のコードの場合、doctestを実行しているときにエラーを起こすべきではありません。python doctestを呼び出すときに省略記号を有効にする方法

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

しかし、

$ 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. 

ellipisを有効にするにはどうすればよいですか?私が言うことができる限り、それはデフォルトでは無効になっています。

私は、下記のコードのように# doctest: +ELLIPSISを追加することで解決しますが、すべてのテストで省略記号を有効にしたいと思います。

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

答えて

10

あなたがtestmod方法にoptionflagsに渡すことができますが、これはモジュール自体の代わりに、doctestモジュールを実行する必要があります:

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

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

出力:

$ 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. 
関連する問題