2013-06-13 4 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.