Adam
Adam

Reputation: 43

I don't understand how this unit test is failing

Here is part of the unit test:

from nose.tools import *
from testing import *


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

And here is my code:

import string

def convert_number(s):
    try: 
        return int(s)
    except ValueError:
        return None

def scan(s):

    direction_words= ("north", "south", "east", "west", "down", "up", "left", "right", "back", "forwards", "backwards")
    verbs= ("go", "stop", "kill", "eat", "shoot", "run", "hide", "dodge")
    stop_words= ("the", "in", "of", "from", "at", "it")
    nouns= ("door", "bear", "princess", "cabinet", "gold", "money", "chest", "gun", "sword")        
    numbers= s.split()
    i=0
    j=0
    g=0
    m=0
    a=0
    b=0

    while a < len(numbers):
        if  type(convert_number(numbers[a])) == int:
            print [('number', int(numbers[a]) )]
            a += 1
        else:
            a += 1


    while i < len(direction_words):
        if direction_words[i] in s.split():
            s= string.lower(s)
            print [('direction', direction_words[i] ) ]
            i+=1 
        else:
            i+=1                

    while j < len(verbs):
        if verbs[j] in s.split():
            s= string.lower(s)
            print [('verb', verbs[j] )]
            j+=1
        else:
            j+=1    

    while g < len(stop_words):
        if stop_words[g] in s.split():
            s= string.lower(s)
            print [('stop', stop_words[g] )]
            g+=1
        else:
            g+=1

    while m < len(nouns):
        if nouns[m] in s.split():
            s= string.lower(s)
            print [('noun', nouns[m] )] 
            m+=1

        else:
            m+=1            

    while b< len(s.split()):      
        if numbers[b] not in nouns:
            if numbers[b] not in stop_words:
                if numbers[b] not in verbs:
                    if numbers[b] not in direction_words:
                        if  type(convert_number(numbers[b])) != int:   
                            print [('error', numbers[b] )]
                            b += 1

                        else:
                            b+=1    
                    else: b+=1
                else: b+=1
            else: b+=1
        else: b+=1


    else:
        return

When I run the unit tests here is what I get:

F
======================================================================
FAIL: tests.ex48_tests.test_directions
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/Adam/Desktop/projects/ex48/tests/ex48_tests.py", line 6, in test_directions
    assert_equal(scan("north"), [('direction', 'north')])
AssertionError: None != [('direction', 'north')]
-------------------- >> begin captured stdout << ---------------------
[('direction', 'north')]

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 1 test in 0.015s

FAILED (failures=1)

I don't understand how the test is failing. When I run the program in terminal it produces the exact same result as what is in the unit tests. I don't understand why it isn't passing.

Upvotes: 0

Views: 225

Answers (4)

John Gaines Jr.
John Gaines Jr.

Reputation: 11534

The function scan() returns None. So the assertation:

assert_equal(scan("north"), [('direction', 'north')])

is correctly failing.

Upvotes: 1

David Wolever
David Wolever

Reputation: 154494

Because your function is printing the result instead of returning it:

while i < len(direction_words):
    if direction_words[i] in s.split():
        s= string.lower(s)
        print [('direction', direction_words[i] ) ] # <--- HERE
        i+=1 

I haven't read through all of LPTHW yet, so I can't tell you exactly what the best way to fix this code in the context of the book is… But if I were writing it, I would return a list, like this:

def scan(s):
    result = []
    ...

    while i < len(direction_words):
        if direction_words[i] in s.split():
            s= string.lower(s)
            # change the `print` statements to `result += …`
            result += [('direction', direction_words[i] ) ]
            i+=1 

    ...
    return result

Upvotes: 4

Daniel Roseman
Daniel Roseman

Reputation: 599590

There are many things wrong with your code, but the most obvious one is that scan never returns anything, so result is always None.

Upvotes: 0

thomson_matt
thomson_matt

Reputation: 7691

Your scan() function prints the text out to the screen and returns nothing, but your unit test is checking the return value of the function.

You should change the scan() function so that it returns the output you're interested in, rather than just printing it.

Upvotes: 0

Related Questions