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))