【问题标题】:AssertionError when using nosetests使用鼻子测试时出现 AssertionError
【发布时间】:2013-11-27 06:00:41
【问题描述】:

在 Learn Python the Hard Way 的练习 48 中,我被要求创建一个模块供这个模块测试,lexicon_tests.py

from nose.tools import *
from ex48 import lexicon


def test_directions():
    assert_equal(lexicon.scan("north"), [('direction', 'north')])
    result = lexicon.scan("north south east")
    assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east')])

def test_verbs():
    assert_equal(lexicon.scan("go"), [('verb', 'go')])
    result = lexicon.scan("go kill eat")
    assert_equal(result, [('verb', 'go'),
                          ('verb', 'kill'),
                          ('verb', 'eat')])


def test_stops():
    assert_equal(lexicon.scan("the"), [('stop', 'the')])
    result = lexicon.scan("the in of")
    assert_equal(result, [('stop', 'the'),
                          ('stop', 'in'),
                          ('stop', 'of')])


def test_nouns():
    assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
    result = lexicon.scan("bear princess")
    assert_equal(result, [('noun', 'bear'),
                          ('noun', 'princess')])

def test_numbers():
    assert_equal(lexicon.scan("1234"), [('number', 1234)])
    result = lexicon.scan("3 91234")
    assert_equal(result, [('number', 3),
                          ('number', 91234)])


def test_errors():
    assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
    result = lexicon.scan("bear IAS princess")
    assert_equal(result, [('noun', 'bear'),
                          ('error', 'IAS'),
                          ('noun', 'princess')])

所以我创建了模块lexicon.py,在这里进行测试:

def scan(words):
    directions = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back']
    verbs = ['go', 'stop', 'kill', 'eat']
    stop_words = ['the', 'in', 'of', 'from', 'at', 'it']
    nouns = ['door', 'bear', 'princess', 'cabinet']


    lex = words.split()
    list1 = []

    for i in lex:
        if i in directions:
            list1.append(('direction', i))
        elif i in verbs:
            list1.append(('verb', i))
        elif i in stop_words:
             list1.append(('stop-word', i))
        elif i in nouns:
            list1.append(('noun', i))
        elif i.isdigit():
            list1.append(('number', convert_number(i)))
        else:
            list1.append(('error', i))
    print list1 

def convert_number(s):
    try:
        return int(s)

    except ValueError:
        return None

但是,当我在 powershell 中运行 nosetests 时,我得到了这个 AssertionError:

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\lexicon_test\skeleton\tests\lexicon_tests.py", line   6, in test_directions
    assert_equal(lexicon.scan("north"), [('direction', 'north')])
AssertionError: None != [('direction', 'north')]
-------------------- >> begin captured stdout << ---------------------
[('direction', 'north')]

这与我在每次测试运行时收到的错误消息相同,其中六个用于lexicon_tests.py 中的六个函数。这个错误是什么意思?一段时间以来一直很烦人。提前致谢。

【问题讨论】:

  • 为什么你printing scan 输出而不是returning 它?
  • 我真的没有停下来思考这会对assert 函数产生什么影响。我最初在那里有print,因为我独立使用该模块只是为了看看它是否有效。我想我只是没有删除它,因为我错误地认为它没有必要。

标签: python nose assertions


【解决方案1】:

assert_equal 函数接受两个参数,如果参数不相等则抛出错误。在这种情况下,lexicon.scan("north") 的结果是None,由于这不等于[('direction', 'north')],所以它抛出了一个错误。

换句话说,您的lexicon.scan 函数无法正常工作。可能与缺少return 语句有关。

【讨论】:

  • 天哪,这很简单,谢谢。问题基本上是assert 函数没有将信息返回给它们,对吗?再次感谢。
  • @Amon 是的,就是这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2016-09-09
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
相关资源
最近更新 更多