测试在鼻子中返回正确的对象(Learn Python the Hard Way Ex 49)

时间:2011-07-11 21:51:38

标签: python unit-testing nose

我一直致力于学习Python艰苦之路第二版,这真是太棒了。我的问题与练习49(http://learnpythonthehardway.org/book/ex49.html)有关,这是关于编写涵盖书中给出的代码的鼻子单元测试。我试图写一个涵盖这个功能的测试:

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))

但它返回时出错,因为两个Sentence对象不是 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)

我如何使用鼻子检查两个物体是否应该相同?

5 个答案:

答案 0 :(得分: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是否具有允许您直接比较对象等效性的函数。

答案 1 :(得分:1)

这里的问题相同。不确定这是否比Thomas更好,但不仅仅是比较对象的值,我在测试中创建了另一个对象,给出了我期望的确切值。

我还使用了内置函数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'))))

答案 2 :(得分:0)

或者,将以下方法添加到Sentence类中,您的原始assert_equal应该可以正常工作。换句话说,你断言两个对象是否“相等”。如果你认为如果所有实例变量的值都相同,那么两个Sentence对象彼此相等,那么下面的代码可以实现你想要的:

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__
...

希望这有帮助!

答案 3 :(得分:0)

github用户(bitsai)的这个备忘单使用了一种你也可以尝试的不同技术。 向Sentence类添加一个方法,将类属性转换为元组。值得一试。 使用它,它更有意义。

点击here

答案 4 :(得分: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