即使提出异常,AssertRaises也会失败 [英] AssertRaises fails even though exception is raised

查看:235
本文介绍了即使提出异常,AssertRaises也会失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下相当奇怪的问题:

I am running into the following rather strange problem:

我正在开发一个django应用程序,在我的模型类中,我定义了一个异常,当一个验证失败:

I am developing a django app and in my models class I am defining an exception that should be raised when a validation fails:

class MissingValueException(Exception):
"""Raise when a required attribute is missing."""
def __init__(self, message):
    super(MissingValueException, self).__init__()
    self.message = message

def __str__(self):
    return repr(self.message)

此代码从一个发布类中调用验证方法:

This code is called from a publication class in a validation method:

def validate_required_fields(self):
    # Here is the validation code.
    if all_fields_present:
        return True
    else:
        raise MissingValueException(errors)

在我的单元测试中,我创建了一个应该提出异常的情况:

In my unit test I create a case where the exception should be raised:

def test_raise_exception_incomplete_publication(self):
    publication = Publication(publication_type="book")
    self.assertRaises(MissingValueException, publication.validate_required_fields)

这产生以下输出:

======================================================================
ERROR: test_raise_exception_incomplete_publication (core_knowledge_platform.core_web_service.tests.logic_tests.BusinessLogicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/media/data/Dokumente/Code/master_project/core_knowledge_platform/../core_knowledge_platform/core_web_service/tests/logic_tests.py", line 45, in test_raise_exception_incomplete_publication
    self.assertRaises(MissingValueException, method, )
File "/usr/lib/python2.7/unittest/case.py", line 465, in assertRaises
    callableObj(*args, **kwargs)
File "/media/data/Dokumente/Code/master_project/core_knowledge_platform/../core_knowledge_platform/core_web_service/models.py", line 150, in validate_required_fields
    raise MissingValueException(errors)
MissingValueException: 'Publication of type book is missing field publisherPublication of type book is missing field titlePublication of type book is missing field year'

所以看起来异常被提出(情况就是这样 - 我甚至在交互式IPython会话中检查它),但是似乎assertRaises是没有抓住它。

So it looks like the exception is raised (which is the case - I even checked it in an interactive IPython session), but it seems that assertRaises is not catching it.

任何人都知道为什么会发生这种情况?

Anyone has any idea why this might happen?

谢谢

推荐答案

可能会发生如果您的测试和产品代码通过两个不同的路径导入您的异常类,则asserRais不知道您遇到的异常是您正在寻找的异常。

This could happen if your tests and your product code are importing your exception class through two different paths, so asserRaises doesn't realize that the exception you got was the one you were looking for.

看看你的进口,确保它们在两个地方是一样的。在PYTHONPATH中以两种不同的方式提供相同的目录可以使这种情况发生。这些条目中的符号链接也可能会混淆事物。

Look at your imports, make sure that they are the same in both places. Having the same directories available in two different ways in your PYTHONPATH can make this happen. Symbolic links in those entries can also confuse things.

这篇关于即使提出异常,AssertRaises也会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆