简体   繁体   English

Python:assertRaises在单元测试中出错...异常未被捕获

[英]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. 基本上,项目被添加到名为theContainer的列表中。 However, if two ID's are the same, then the duplicate is not added. 但是,如果两个ID相同,则不会添加副本。 By forcing the addition of two projects with the same ID in the unit test to be added, an exception is raised. 通过在要添加的单元测试中强制添加具有相同ID的两个项目,会引发异常。

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? 问题可能在于assertRaises中的第三个参数吗? (hist.addProject(myProject2)) (hist.addProject(myProject2))

Your suspicion is correct and the issue lies with the call to hist.addProject() . 你的怀疑是正确的,问题在于对hist.addProject()的调用。

You wrote: 你写了:

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

There is a ValueError raised. 引发了一个ValueError 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. 所以assertRaises实际上从未被调用,因为异常在被调用之前被引发。

The way to think about it is that assertRaises can only catch exceptions if it actually manages to be called. 考虑它的方法是assertRaises只能捕获实际设法被调用的异常。 If the act of preparing its arguments raises an exception, then assertRaises does not even run, and so cannot catch anything. 如果准备其参数的行为引发异常,则assertRaises甚至不会运行,因此无法捕获任何内容。

If you expect an exception in the call to addProject() just change your assertion: 如果你期望在调用addProject()出现异常,只需更改你的断言:

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

Or you could postpone the call to hist.addProject() with a lambda: 或者你可以用lambda推迟对hist.addProject()的调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM