python - 用 Nose 比较物体

标签 python unit-testing nose

我正在努力学习 Python 练习 49 (http://learnpythonthehardway.org/book/ex49.html),我需要比较两个不同的对象。这是我需要测试的代码:

class ParserError(Exception):
    pass


class Sentence(object):

def __init__(self, subject, verb, object):
    # remember we take ('noun','princess') tuples and convert them
    self.subject = subject[1]
    self.verb = verb[1]
    self.object = object[1]


def peek(word_list):
    if word_list:
        word = word_list[0]
        return word[0]
    else:
        return None


def match(word_list, expecting):
    if word_list:
        word = word_list.pop(0) #Here it's returning 'noun' from the noun/princess  tuple

        if word[0] == expecting: #Here it's testing whether or not the new item in the       0 position ('noun' in this case) = expecting
            return word
        else:
            return None
    else:
        return None


def skip(word_list, word_type):
    while peek(word_list) == word_type:
        match(word_list, word_type)


def parse_verb(word_list):
    skip(word_list, 'stop')

    if peek(word_list) == 'verb':
        return match(word_list, 'verb')
    else:
        raise ParserError("Expected a verb next.")


def parse_object(word_list):
    skip(word_list, 'stop')
    next = peek(word_list)

    if next == 'noun':
        return match(word_list, 'noun')
    if next == 'direction':
        return match(word_list, 'direction')
    else:
        raise ParserError("Expected a noun or direction next.")


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

    return Sentence(subj, verb, obj)


def parse_sentence(word_list):
    skip(word_list, 'stop')

    start = peek(word_list)

    if start == 'noun':
        subj = match(word_list, 'noun')
        return parse_subject(word_list, subj)
    elif start == 'verb':
        # assume the subject is the player then
        return parse_subject(word_list, ('noun', 'player'))
    else:
        raise ParserError("Must start with subject, object, or verb not: %s" % start)

我的问题是函数parse_sentence,我应该在其中创建一个句子对象。因此,我需要在测试代码中创建另一个句子对象并确保它是相同的:

def test_parse_subject():
    word_list = [('verb', 'kill'), ('direction', 'north')]

    subj = ('noun', 'princess')
    verb = ('verb', 'kill')
    obj = ('direction', 'north')
    obj_sent = Sentence(subj, verb, obj)

    assert_equal(parser.parse_subject(word_list, subj), obj_sent)

但我不断收到此回溯错误:

Traceback (most recent call last):
  File "G:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
  File    "G:\Users\Charles\dropbox\programming\parsing_test\skeleton\tests\parser_tests.py", line  45, in test_parse_subjec
t
    assert_equal(parser.parse_subject(word_list, subj), obj_sent)
AssertionError: <ex49.parser.Sentence object at 0x02651C50> != <ex49.parser.Sentence   object at 0x02651C30>

所以它不会返回相同的对象,但我很确定它们是相同的。我给了他们正确的论据。如果对象相同,我如何确认这一点?提前致谢

最佳答案

这是失败的,因为您正在比较两个实例化的 Sentence 对象,而不是它们各自的属性。

检查“Elegant ways to support equivalence ("equality") in Python classes ”以获取可以比较属性的方法。

关于python - 用 Nose 比较物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20738217/

相关文章:

Python:锁定目录

python - 使用 PIL 在终端中显示图像 (png)

java - 如何模拟特定的阅读文件?

unit-testing - Rails 4 - 单元测试无法创建模型对象

python - 模拟 flask.request 在 python nosetests

python - 在 Nose 中运行与单元测试子类无关的单个测试函数

python - Pandas:将具有 DatetimeIndex 的列表字典转换为 DataFrame

Python列表弹出

python - 在 Python 中进行单元测试的适当文件层次结构

Python nose 关键测试与非关键测试