2015-07-12 86 views
-3

我在Python中做了一個類來執行一些基本的操作。爲什麼doctest失敗?

class perform(object): 

    def add(self, first, second): 
     """Adds two numbers 
     >>>perform().add(1, 2) 
     3 
     Also adds string 
     >>>perform().add('some', 'thing') 
     'something' 
     """ 
     return first+second 

我不明白爲什麼doctest的add函數失敗。

+2

不文檔測試確切地告訴你它爲什麼失敗? – juanchopanza

+0

請注意,文檔中涵蓋了:https://docs.python.org/2/library/doctest.html#how-are-docstring-examples-recognized – jonrsharpe

回答

2

你需要一些空間和一個空行添加到文檔字符串

class perform(object): 
    def add(self, first, second): 
     """Adds two numbers 
     >>> perform().add(1, 2) 
     3 

     Also adds string 
     >>> perform().add('some', 'thing') 
     'something' 
     """ 
     return first+second 
+0

謝謝Alik。這是我想指出的。 –

+0

@HimanshuMishra https://docs.python.org/2/library/doctest.html#how-are-docstring-examples-recognized doctest將查找空行或以>>>開頭的行以確定何時預期的輸出結束 – Alik