2011-08-01 9 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

Cool。戻り値の順序が重要でない場合(例: '( 'direction'、 'north')、( 'direction'、 'south')]'は '[( 'direction' 'south')、( 'direction'、 'north')] ')を使用すると、[' set'組み込み](http://docs.python.org/library/sets.html)([Wikipedia ](http://en.wikipedia.org/wiki/Set_(computer_science)))リストを比較しています。 set([1,2,3])==このようにすると、set([(direction)、 'north')、...]) 'セット([3,2,1]) '。 –

0

scan()関数は、テキストを画面に出力して何も返しませんが、ユニットテストは関数の戻り値をチェックしています。

scan()関数を変更して、印刷するのではなく、興味のある出力を返すようにする必要があります。

0

コードには多くの不具合がありますが、最も明白なのはscanは何も返さないため、resultは常にNoneです。

+0

私はそれが非常に非常に悪いコードだと知っています。私はプログラミングに本当に慣れていて、今この問題を約3日間作業しています。それは多くの変更と編集を経て、はるかにエレガントな方法でやり遂げられると確信しています。しかし、助けてくれてありがとう。 – Adam

1

機能のスキャンを()Noneを返します。したがって、アサーション:

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

は正しく失敗しています。

+0

ああ、ありがとうございました。 – Adam