Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: assertRaises error in unit test...exception not being caught

I have a unit test written to force an exception to be thrown. The exception is thrown, but my unit test statement doesn't catch it for some reason, and fails unexpectedly.

Here is the unit test:

def test900_001_ShouldRaiseExceptionDuplicateID(self):
    hist = projecthistory.ProjectHistory()
    myProject = project.Project(id = 42, locR = 10, locP = 15, locA = 30, eP = 200, eA= 210)
    hist.addProject(myProject)
    myProject2 = project.Project(id = 42, locR = 15, locP = 25, locA = 40, eP = 300, eA = 410)
    self.assertRaises(ValueError, projecthistory.ProjectHistory, hist.addProject(myProject2))

Here is the code that this pertains to:

def addProject(self, proj):

    duplicate = False
    checkId = proj.getId()

    #check to see if that id is already in the container if so, raise ValueError
    #append project to container
    for project in self.theContainer:
        if (project.getId() == checkId):
            duplicate = True
            break
    if(duplicate == False):
        self.theContainer.append(proj)
    else:
        raise ValueError("ProjectHistory.addProject: Duplicate ID found. Project not added to repository.")

    return len(self.theContainer)

Basically, projects are added to a list called theContainer. However, if two ID's are the same, then the duplicate is not added. By forcing the addition of two projects with the same ID in the unit test to be added, an exception is raised.

Here is the traceback that I get:

Traceback (most recent call last):

  File "C:\Users\blah\workspace\blahID\CA06\test\projecthistoryTest.py", line 46, in test900_001_ShouldRaiseExceptionDuplicateID
    self.assertRaises(ValueError, projecthistory.ProjectHistory, hist.addProject(myProject2))

  File "C:\Users\blah\workspace\blahID\CA06\prod\projecthistory.py", line 38, in addProject
    raise ValueError("ProjectHistory.addProject: Duplicate ID found. Project not added to repository.")
ValueError: ProjectHistory.addProject: Duplicate ID found. Project not added to repository.

Could the problem be with the third parameter in assertRaises? (hist.addProject(myProject2))

like image 775
SwaroopGiwali Avatar asked Jan 14 '23 15:01

SwaroopGiwali


1 Answers

Your suspicion is correct and the issue lies with the call to hist.addProject().

You wrote:

self.assertRaises(ValueError, projecthistory.ProjectHistory,
    hist.addProject(myProject2))

There is a ValueError raised. But it is in

hist.addProject(myProject2)

The traceback tells you that. And so assertRaises is never actually called because the exception is raised before it gets called.

The way to think about it is that assertRaises can only catch exceptions if it actually manages to be called. If the act of preparing its arguments raises an exception, then assertRaises does not even run, and so cannot catch anything.

If you expect an exception in the call to addProject() just change your assertion:

self.assertRaises(ValueError, hist.addProject, myProject2)

Or you could postpone the call to hist.addProject() with a lambda:

self.assertRaises(ValueError, 
    lambda: projecthistory.ProjectHistory(hist.addProject(myProject2)))
like image 103
David Heffernan Avatar answered Jan 18 '23 22:01

David Heffernan