2011-07-11 33 views
1

我一直在努力學習Python的艱辛之路第二版,它一直很棒。我的問題與練習49(http://learnpythonthehardway.org/book/ex49.html)有關,它是關於書寫鼻子單元測試,涵蓋書中給出的代碼的。我試圖寫一個測試,涵蓋這一功能:測試合適的對象是在鼻子中返回的(Learn Python the Hard Way Ex 49)

def parse_subject(word_list, subj): 
    verb = parse_verb(word_list) 
    obj = parse_object(word_list) 
    return Sentence(subj, verb, obj) 

我試圖運行這個測試:

from nose.tools import * 
from ex48 import engine 
def test_parse_subject(): 
    word_list = [('verb', 'verb'), 
       ('direction', 'direction')] 
    test_subj = ('noun', 'noun') 
    test_verb = ('verb', 'verb') 
    test_obj = ('direction', 'direction') 
    assert_equal(engine.parse_subject(word_list, ('noun', 'noun')), 
       engine.Sentence(test_subj, test_verb, test_obj)) 

但它返回錯誤,因爲這兩個句子的對象不是EXACT相同的對象:

⚡ nosetests     
.....F.......... 
====================================================================== 
FAIL: tests.engine_tests.test_parse_subject 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/nose/case.py", line 187, in runTest 
    self.test(*self.arg) 
File "/Users/gregburek/code/LPTHW/projects/ex48/tests/engine_tests.py", line 59, in test_parse_subject 
    engine.Sentence(test_subj, test_verb, test_obj)) 
AssertionError: <ex48.engine.Sentence object at 0x101471390> != <ex48.engine.Sentence object at 0x1014713d0> 

---------------------------------------------------------------------- 
Ran 16 tests in 0.018s 

FAILED (failures=1) 

如何我可以用鼻子來檢查兩個對象應該是一樣的嗎?

回答

2

我很喜歡在同一個確切的練習#49上工作,並遇到同樣的錯誤,並有相同的問題。由於沒有人回答,我認爲我應該拋棄我所做的,只是簡單地將「句子對象」分爲三部分,並確保它們是相同的。它看起來是這樣的:

def test_parse_subject(): 
word_list = [('verb','run'), 
      ('noun','Bear'), 
      ('verb','jump')] 
subj = ('noun', 'me') 
result = ex49.parse_subject(word_list,subj) 
assert_equal(result.subject, 'me') 
assert_equal(result.verb, 'run') 
assert_equal(result.object, 'Bear') 

我很想知道但如果nose.tools有一個功能,可以直接比較的對象等同。

0

或者,將下面的方法添加到Sentence類中,並且您的原始assert_equal應該可以工作。換句話說,你斷言兩個對象是否「相等」。如果你覺得兩個句話對象彼此相等,如果所有的實例變量的值,然後在下面的代碼相同的達到你想要的東西:

class Sentence(object): 
... 
# Python 2.X 
def __cmp__(self, other): 
    return self.__dict__ == other.__dict__ 

# Python 3.X 
def __eq__(self, other): 
    return self.__dict__ == other.__dict__ 
... 

希望這有助於!

1

同樣的問題在這裏。不知道這是否比托馬斯做得更好,而不是僅僅比較對象的值,我在測試中創建了另一個對象,並給出了我期望的確切值。

我還使用了內置函數vars()和從模塊pprint導入的pprint,所以我不必逐個比較每個值。

from nose.tools import * 
from pprint import pprint 
from ex49 import sentence_parser 

    def test_parse_sentence(): 
      # Test word list for parser to parse 
      word_list = [('verb', 'verb'), ('stop', 'stop'), ('noun', 'noun')] 

      # Here I create an object using the parse_sentence function 
      theSentence = sentence_parser.parse_sentence(word_list) 

      #Then I use assert_equal to compare the object created by the parse_sentence function to an object that I create by passing it the expected values. 
      assert_equal(pprint(theSentence), 
         pprint(sentence_parser.Sentence(('noun', 'player'), 
                ('verb', 'verb'), 
                ('noun', 'noun')))) 
0

這個github用戶(bitsai)的備忘單使用了不同的技術,你也可以嘗試。 向Sentence類添加一個方法,將類屬性轉換爲元組。值得一試。 我正在使用它,它更有意義。

點擊here

0

你還可以嘗試:

class Sentence(object): 
    def __init__(self, subject, verb, obj): 
     self.subject = subject[1] 
     self.verb = verb[1] 
     self.object = obj[1] 

    def __eq__(self, other): 
     if type(other) is type(self): 
      return self.__dict__ == other.__dict__ 
     else: 
      return False