2013-07-13 93 views
0
class Test: 
    def __init__(self): 
     pass 

    ''' 
    This function parses a name according to following rules - 

    >>> Test().name('Kshitiz Sharma') 
    Sharma, Kshitiz 

    ''' 
    def name(self, name): 
     name = name.split() 
     return name[1] + ", " + name[0] 

if __name__ == "__main__": 
    import doctest 
    doctest.testmod() 

輸出是 -爲什麼doctest沒有檢測到我的測試?

4 items had no tests: 
    __main__ 
    __main__.Test 
    __main__.Test.__init__ 
    __main__.Test.name 
0 tests in 4 items. 
0 passed and 0 failed. 
Test passed. 

哪裏是我的測試?

回答

3

將docstring放入方法中。

def name(self, name): 
    ''' 
    This function parses a name according to following rules - 

    >>> name('Kshitiz Sharma') 
    <BLANKLINE> 

    ''' 
    pass 

而且上運行的方法測試無誤,您需要實際實例化類測試本身期間有機會獲得方法:

>>> t = Test(); t.name('Kshitiz Sharma') 
    <BLANKLINE> 
+0

如果我有一個文檔字符串類? –

+0

@KshitizSharma [Python:使用doctests for classes](http://stackoverflow.com/questions/2708178/python-using-doctests-for-classes) –

相關問題