AsertionError al usar nosetests

On exercise 48 of Learn Python the Hard Way, I'm asked to create a module to be tested by this one,

from 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')])

So I created the module,, to be tested here:

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)))
            list1.append(('error', i))
    print list1 

def convert_number(s):
        return int(s)

    except ValueError:
        return None

Sin embargo, cuando corro nosetests in powershell I get this AssertionError:

Traceback (most recent call last):
  File "G:\Python27\lib\site-packages\nose\", line 197, in runTest
  File        "G:\Users\Charles\dropbox\programming\lexicon_test\skeleton\tests\", line   6, in test_directions
    assert_equal(lexicon.scan("north"), [('direction', 'north')])
AssertionError: None != [('direction', 'north')]
-------------------- >> begin captured stdout << ---------------------
[('direction', 'north')]

That's the same error message I get for each test run, six of them for the six functions in What does this error mean? It's been irritating be for a while now. Thanks in advance.

preguntado el 27 de noviembre de 13 a las 06:11

Por qué eres printing el scan salida en lugar de return¿Lo estás? -

I really didn't even stop to think about how that would have effect the assert functions. I had print there intitially because I was using that module independently just to see if it worked. I guess I just didn't remove it because I wrongly found it unnecessary. -

1 Respuestas

EL assert_equal function takes two arguments, and throws an error if the arguments aren't equal to each other. In this case, the result of lexicon.scan("north") is None, and since this isn't equal to [('direction', 'north')], está arrojando un error.

En otras palabras, tu lexicon.scan function isn't working properly. It might have something to do with it missing a return .

respondido 27 nov., 13:06

Holy crap that was easy, thanks. The issue was basically that the assert functions weren't having the info returned to them right? Thanks again. - Amon

@Amon Yep, that's exactly it. - Xymostech

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.