2012-02-16 3 views
2

assert_equalを使ってオブジェクトを比較することは可能ですか?私はこのエラーを見ておいてください。困難な方法でPythonを学ぶ例49:assert_equalを使ってオブジェクトを比較する

AssertionError: <ex49.parser.Sentence object at 0x01F1BAF0> != 
<ex49.parser.Sentence object at 0x01F1BB10> 

は、関連するコードフラグメント:

def test_parse_subject(): 

    testsentence = "princess go east" 
    result = lexicon.scan(testsentence) 
    Sent = parse_sentence(result) 
    ResultSent = Sentence(('subject', 'princess'), 
         ('verb', 'go'), 
         ('object', 'east')) 
    print ResultSent.subject 
    print ResultSent.verb 
    print ResultSent.object 
    print Sent.subject 
    print Sent.verb 
    print Sent.object 
    assert_equal(Sent, ResultSent) 

画面上の印刷出力は、オブジェクトが同じ内容を持っていることを示唆している - まだアサーションエラーが表示されます。どうしてこれなの? assert_equalを使ってこれをオーバーライドする方法はありますか?

答えて

6

私はあなたがSentenceクラスで__eq__メソッドを実装する必要があると信じています。

assertEqual(first, second, msg=None)¶ Test that first and second are equal. If the values do not compare equal, the test will fail.

In addition, if first and second are the exact same type and one of list, tuple, dict, set, frozenset or unicode or any type that a subclass registers with addTypeEqualityFunc() the type-specific equality function will be called in order to generate a more useful default error message (see also the list of type-specific methods).

Python unittest documentation

The correspondence between operator symbols and method names is as follows: xlt(y), x<=y calls x.le(y), x==y calls x.eq(y), x!=y and x<>y call x.ne(y), x>y calls x.gt(y), and x>=y calls x.ge(y).

Python data model documentation

例:

import unittest 


class A: 

    def __init__(self, num): 
     self.num = num 

    def __eq__(self, other): 
     return self.num == other.num 


class Test(unittest.TestCase): 

    def test(self): 
     a1 = A(1) 
     a12 = A(1) 
     a2 = A(2) 

     self.assertEqual(a1, a1, 'a1 != a1') 
     self.assertEqual(a1, a12, 'a1 != a12') 
     self.assertEqual(a1, a2, 'a1 != a2') 

def main(): 
    unittest.TestRunner(Test()) 

if __name__ == '__main__': 
    unittest.main() 

は今__eq__方法をコメントし、違いを参照してください。

0

私もLPTHW ex49で作業しています。具体的には以下のように、この例の文脈のために、私は、__eqの__()文のクラスにメソッドを追加することで動作するようにそれを得ることができました:

Class Sentence(object): 

    def __init__(self, subject, verb, object_) 
     ... 

    def __eq__(self, other): 
     return (self.subject == other.subject and 
       self.verb == other.verb and 
       self.object_ == other.object_) 

を次に、テストファイルでは、私がやった:

# where LIST5 is defined above to give list of two tuples, [('verb', 'go'), ('direction', 'east')] 
def test_parse_subject(): 
    wordlist = list(LIST5) 
    sent = parse.Sentence(('noun', 'person'), ('verb'), ('go'), ('direction', 'east)) 
    newsent = parse.parse_subject(wordlist, ('noun', 'person')) 
    assert_equal(newsent, sent) 

noseとunittestを使ってassert_equalが存在する場合、__eq __()メソッドを呼び出すことができます。この場合、2つのオブジェクトがsubject、verb、object_の3つの同じ値を持つ限り、テストはOKです。しかし、私のコードにはバグがあり、鼻だけが提供する唯一のものは、私が__eq __()メソッドを持っていたとしても、あなたが受け取ったのと同じエラーメッセージです。つまり、AssertionError:... object at 0x ...!= ... object at 0x ... "これは、__eq __()メソッドが動作していないと思ってしまった。アドレスを比較する。これを行うより良い方法があるかどうかはわかりません。

注:geditがオブジェクトをpythonキーワードとして強調表示していたため、objectをobject_に変更しました。末尾のアンダースコアを使用することをお勧めしますか。

1

これは、良い情報である私にとって、私はので、私はちょうど下記のように2つのオブジェクトの変数を比較して検索するのが面倒だった:

def test_parse_subject(): 
    word_list_a = lexicon.scan("eat the bear") 
    Sentence1 = Sentence(('noun','player'),('verb', 'eat'),('noun', 'bear')) 
    Sentence2 = parse_subject(word_list_a,('noun','player')) 

    assert_equal(Sentence2.subject, Sentence1.subject) 
    assert_equal(Sentence2.verb, Sentence1.verb) 
    assert_equal(Sentence2.object, Sentence1.object) 
関連する問題