2011-08-01 72 views
0

下面是單元測試的一部分:我不明白這個單元測試是怎麼失敗

from nose.tools import * 
from testing import * 


def test_directions(): 
     assert_equal(scan("north"), [('direction', 'north')]) 
     result = scan("north south east") 
     assert_equal(result, [('direction', 'north'), ('direction', 'south'), ('direction', 'east')]) 

這裏是我的代碼:

import string 

def convert_number(s): 
    try: 
     return int(s) 
    except ValueError: 
     return None 

def scan(s): 

    direction_words= ("north", "south", "east", "west", "down", "up", "left", "right", "back", "forwards", "backwards") 
    verbs= ("go", "stop", "kill", "eat", "shoot", "run", "hide", "dodge") 
    stop_words= ("the", "in", "of", "from", "at", "it") 
    nouns= ("door", "bear", "princess", "cabinet", "gold", "money", "chest", "gun", "sword")   
    numbers= s.split() 
    i=0 
    j=0 
    g=0 
    m=0 
    a=0 
    b=0 

    while a < len(numbers): 
     if type(convert_number(numbers[a])) == int: 
      print [('number', int(numbers[a]))] 
      a += 1 
     else: 
      a += 1 


    while i < len(direction_words): 
     if direction_words[i] in s.split(): 
      s= string.lower(s) 
      print [('direction', direction_words[i]) ] 
      i+=1 
     else: 
      i+=1     

    while j < len(verbs): 
     if verbs[j] in s.split(): 
      s= string.lower(s) 
      print [('verb', verbs[j])] 
      j+=1 
     else: 
      j+=1  

    while g < len(stop_words): 
     if stop_words[g] in s.split(): 
      s= string.lower(s) 
      print [('stop', stop_words[g])] 
      g+=1 
     else: 
      g+=1 

    while m < len(nouns): 
     if nouns[m] in s.split(): 
      s= string.lower(s) 
      print [('noun', nouns[m])] 
      m+=1 

     else: 
      m+=1    

    while b< len(s.split()):  
     if numbers[b] not in nouns: 
      if numbers[b] not in stop_words: 
       if numbers[b] not in verbs: 
        if numbers[b] not in direction_words: 
         if type(convert_number(numbers[b])) != int: 
          print [('error', numbers[b])] 
          b += 1 

         else: 
          b+=1  
        else: b+=1 
       else: b+=1 
      else: b+=1 
     else: b+=1 


    else: 
     return 

當我運行單元測試是這裏我得到:

F 
====================================================================== 
FAIL: tests.ex48_tests.test_directions 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest 
    self.test(*self.arg) 
    File "/Users/Adam/Desktop/projects/ex48/tests/ex48_tests.py", line 6, in test_directions 
    assert_equal(scan("north"), [('direction', 'north')]) 
AssertionError: None != [('direction', 'north')] 
-------------------- >> begin captured stdout << --------------------- 
[('direction', 'north')] 

--------------------- >> end captured stdout << ---------------------- 

---------------------------------------------------------------------- 
Ran 1 test in 0.015s 

FAILED (failures=1) 

我不明白如何測試失敗。當我在終端中運行程序時,它會產生與單元測試中完全相同的結果。我不明白爲什麼它不通過。

回答

4

因爲你的函數打印的結果,而不是返回

while i < len(direction_words): 
    if direction_words[i] in s.split(): 
     s= string.lower(s) 
     print [('direction', direction_words[i]) ] # <--- HERE 
     i+=1 

我沒有通過所有LPTHW的閱讀,所以我不能告訴你到底是什麼的最佳途徑爲了解決這個問題在本書的背景下...但如果我寫的話,我會返回一個列表,像這樣:

def scan(s): 
    result = [] 
    ... 

    while i < len(direction_words): 
     if direction_words[i] in s.split(): 
      s= string.lower(s) 
      # change the `print` statements to `result += …` 
      result += [('direction', direction_words[i]) ] 
      i+=1 

    ... 
    return result 
+0

真棒非常有意義。必須改變測試,因爲現在掃描函數的結果是按照我創建的元組的順序而不是句子的順序。不要認爲這會有所作爲。 – Adam

+0

很酷。如果返回值的順序不重要(例如,'[''direction','north'),('direction','south')]'與'[('direction', ''south'),('direction','north')]'),那麼你可以使用['set' built in](http://docs.python.org/library/sets.html)([Wikipedia ](http://en.wikipedia.org/wiki/Set_(computer_science))),當你比較列表。例如:'assert_equal(set(result),set([('direction','north'),...])'這會工作,因爲sets是無序的,所以'set([1,2,3])==設置([3,2,1])'。 –

0

您的scan()函數將文本輸出到屏幕並不返回任何內容,但您的單元測試正在檢查函數的返回值。

您應該更改scan()函數,以便它返回您感興趣的輸出,而不僅僅是打印它。

0

你的代碼有很多錯誤,但最明顯的是scan從不返回任何東西,因此result總是None

+0

我知道這是非常非常糟糕的代碼。我對編程非常陌生,現在已經在這個問題上工作了大約3天。它經歷了很多變化和編輯,我相信可以以更加優雅的方式完成。感謝您的幫助。 – Adam

1

功能掃描()返回無。所以斷言:

assert_equal(scan("north"), [('direction', 'north')]) 

正確失敗。

+0

Ahhh我明白了,非常感謝。 – Adam