I am learning Python through LPTHW. There is an exercise where we need to create test scenarios. The following is the code for whom Test Case have to be made:

#############################
#   Sentence Parser         #
#############################

class ParserError(Exception):
    pass

class Sentence(Object):

    def __init__(self, subject, verb, object):
        # remember, we take ('noun','princess') 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)

        if word[0] == expecting:
            return word
        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 nour or direction next")

def pars_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')

    if start == 'noun':
        subj = match(word_list, 'noun') 
        return parse_subject(word_list, subj)
    elif start == 'verb':
        # assume the subject is a player then
        return parse_subject(word_list,('noun', 'player'))
    else:

raise ParserError("Must start with subject, object, or verb not:%s" % start)

The Following the test script:

---------------------------------------------------------------------------------------------------------------

from nose.tools import *
from ex48.lexicon import lexicon
from ex49.ex49 import *

def test_parse_subject():      
    testsentence = "princess go east"     
    result = lexicon.scan(testsentence)     
    Sent = parse_sentence(result)     
    ResultSent = Sentence(('subject', 'princess'),            
                          ('verb', 'go'),                       
                          ('object', 'east'))     
    print ResultSent.subject     
    print ResultSent.verb     
    print ResultSent.object     
    print Sent.subject     
    print Sent.verb     
    print Sent.object     

assert_equal(Sent, ResultSent)

When i run nosetests command to execute this, it gives an error message:

PS C:\Users\rk\projects\parser> nosetests
E
======================================================================
ERROR: Failure: NameError (name 'Object' is not defined)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\nose\loader.py", line 390, in loadTestsFro
mName
    addr.filename, addr.module)
  File "C:\Python27\lib\site-packages\nose\importer.py", line 39, in importFromP
ath
    return self.importFromDir(dir_path, fqname)
  File "C:\Python27\lib\site-packages\nose\importer.py", line 86, in importFromD
ir
    mod = load_module(part_fqname, fh, filename, desc)
  File "C:\Users\rk\projects\parser\tests\ex49_test.py", line 3, in <module>
    from ex49.ex49 import *
  File "C:\Users\rk\projects\parser\ex49\ex49.py", line 8, in <module>
    class Sentence(Object):
NameError: name 'Object' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.031s

FAILED (errors=1)

but still the error is there

PS C:\Users\rk\projects\parser> nosetests
E
======================================================================
ERROR: tests.ex49_test.test_parse_subject
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
  File "C:\Users\rk\projects\parser\tests\ex49_test.py", line 9, in test_parse_s
ubject
    Sent = parse_sentence(result)
  File "C:\Users\rk\projects\parser\ex49\ex49.py", line 78, in parse_sentence
    raise ParserError("Must start with subject, object, or verb not:%s" % start)

ParserError: Must start with subject, object, or verb not:direction

----------------------------------------------------------------------
Ran 1 test in 0.281s

FAILED (errors=1)
PS C:\Users\rk\projects\parser>

i found out that error is due to this:

PS C:\Users\rk\projects\parser> nosetests
E
======================================================================
ERROR: tests.ex49_test.test_parse_subject
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
  File "C:\Users\rk\projects\parser\tests\ex49_test.py", line 16, in test_parse_
subject
    print Sent.subject
AttributeError: 'NoneType' object has no attribute 'subject'
-------------------- >> begin captured stdout << ---------------------
princess
go
east

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

----------------------------------------------------------------------
Ran 1 test in 0.016s

FAILED (errors=1)
PS C:\Users\rk\projects\parser>

The sent variable in test script is not returning any value. This is the cause of error. Any suggestions on this?

This article has been dead for over six months. Start a new discussion instead.