1

我一直在努力学习 Python 第 2 版,这真是太棒了。我的问题与练习 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 对象不是完全相同的对象

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

我如何使用鼻子来检查两个对象是否应该相同?

4

5 回答 5

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 是否具有允许您直接比较对象等效性的功能。

于 2011-11-22T02:32:37.060 回答
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'))))
于 2013-11-01T17:58:33.130 回答
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__
...

希望这可以帮助!

于 2013-06-04T23:10:22.473 回答
0

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

点击这里

于 2014-03-07T10:31:05.317 回答
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
于 2014-08-15T06:14:22.220 回答