2017-02-26 63 views
0

今天學習如何使用doctest。之前我發現不得不添加<BLANKLINE>。現在我怎樣才能讓剩下的失敗測試通過?對於所有三種存在的預期,得到了沒有什麼區別:預計==有文檔測試失敗

C:\Windows\System32>python C:\Users\George\AppData\Local\Programs\Python\Python3 
5-32\Lib\exemplarypy.py 
********************************************************************** 
File "C:\Users\George\AppData\Local\Programs\Python\Python35-32\Lib\exemplarypy. 
py", line 27, in __main__.find_name 
Failed example: 
    print(exemplarypy.find_name("")) 
Expected: 
    First cell must contain '.' xor '(' 
    None 
Got: 
    First cell must contain '.' xor '(' 
    None 
********************************************************************** 
File "C:\Users\George\AppData\Local\Programs\Python\Python35-32\Lib\exemplarypy. 
py", line 38, in __main__.find_name 
Failed example: 
    print(exemplarypy.find_name("(.")) 
Expected: 
    First cell cannot contain both '.' and '(' 
    None 
Got: 
    First cell cannot contain both '.' and '(' 
    None 
********************************************************************** 
File "C:\Users\George\AppData\Local\Programs\Python\Python35-32\Lib\exemplarypy. 
py", line 41, in __main__.find_name 
Failed example: 
    print(exemplarypy.find_name("x(.")) 
Expected: 
    First cell cannot contain both '.' and '(' 
    None 
Got: 
    First cell cannot contain both '.' and '(' 
    None 
********************************************************************** 
1 items had failures: 
    3 of 11 in __main__.find_name 
***Test Failed*** 3 failures. 

C:\Windows\System32> 

文件正在運行:

import sys 

class HaltException(Exception): pass 

def find_name(cell_value): 
    """ 
    Return the (denuded) token that comes after "[example " in cell_value. 

    cell_value's parenthetical argument or ".py" will be excluded: 
     [example FizzBuzz(x, y, z)] => FizzBuzz 
     [example FizzBuzz.py]  => FizzBuzz 

    Doctesting: 
>>> import exemplarypy 
>>> print(exemplarypy.find_name("t((")) 
t 
>>> print(exemplarypy.find_name("((")) 
<BLANKLINE> 
>>> print(exemplarypy.find_name("(")) 
<BLANKLINE> 
>>> print(exemplarypy.find_name("")) 
First cell must contain '.' xor '(' 
None 
>>> print(exemplarypy.find_name("x(adsf)")) 
x 
>>> print(exemplarypy.find_name("asdf.txt")) 
asdf 
>>> print(exemplarypy.find_name(".asdf.txt")) 
<BLANKLINE> 
>>> print(exemplarypy.find_name("x.asdf.txt")) 
x 
>>> print(exemplarypy.find_name("(.")) 
First cell cannot contain both '.' and '(' 
None 
>>> print(exemplarypy.find_name("x(.")) 
First cell cannot contain both '.' and '(' 
None 

    """ 
    try: 
     if cell_value.find(".") > -1: 
      if cell_value.find("(") > -1: 
       raise HaltException("First cell cannot contain both '.' and '(' ") 
      boundary = cell_value.find(".") 
      return cell_value[0:boundary] 
     if cell_value.find("(") > -1: 
      boundary = cell_value.find("(") 
      return cell_value[0:boundary] 
     raise HaltException("First cell must contain '.' xor '(' ") 
    except HaltException as h: 
     print(h) 

# To test the embedded docstring documentation: 
if __name__ == "__main__": 
    import doctest 
    doctest.testmod() 

附錄:相比預期的,得到了​​與文本顯示在記事本中的所有字符++;他們的空白是相同的。

+2

它與您的錯誤消息結尾的空格有關嗎? – jonrsharpe

+0

[Doctest失敗,儘管有正確的輸出]可能的重複(http://stackoverflow.com/questions/1650184/doctest-failing-inspite-of-having-correct-output) – mkrieger1

+0

@ mkrieger1這兩個問題的確是有的空白解決方案,但這不是重複的,因爲在這裏,需要刪除的空間在程序本身,而不是在文檔測試中。 – gherson

回答

0

Good call,@jonrsharpe,謝謝:我只是不得不刪除這些字符串末尾的空格,即在最後一個單引號和雙引號之間。

raise HaltException("First cell cannot contain both '.' and '(' ") 

raise HaltException("First cell must contain '.' xor '(' ") 

現在,所有的測試都通過了。