在Python中进行单元测试时,跨类传递异常 [英] Passing exceptions across classes while unit-testing in Python
问题描述
假设有两个Python类,第一个类(第1类)包含一个包含一些语句( some_statement )的函数( function_c1 ,如果为true,则返回自定义异常( MyException )。进一步假设第二类(
class Class1:
'''My nifty class 1.'''
def __init __(self):
pass
def function_c1(self,some_input):
'''类1的函数。'''
if some_statement )
return MyException('Some error message。')
return True
class Class2:
'''My nifty class 2.'''
def __init __(self):
pass
def function_c2(self,some_input):
'''类2的函数。'''
my_handle = Class1
try:
my_handle.function_c1(some_input)
除了MyException as e:
return MyException(e)
return True
无论Class 1是否返回异常,为什么Class 2都会返回 True b
编辑1 :在上面的例子中,我故意选择
return
而不是raise
异常,以便渲染类1适用于单元测试。self.assertRaises(Class1()。function_c1(some_input))
给定我对Python的不完全理解,我只看到两个选项来吃蛋糕(即,进行单元测试)并保持它(即,类2接收类1的异常):(a)我<或< c $ c> raise 以及
return
类1中的MyException (b)我使用不同的assert
语句。
em> EDIT 2 :感谢一些很好的反馈,我现在明白,我原来的错误是不正确使用
assertRaises
的结果。它应该如下使用:class MyException(Exception):
$ p但是,单元测试只有在类1引发了一个内置异常时才会传递,例如
pass
class Class1:
...
with self.assertRaises(MyException):
Class1()。function_c1(some_input)
ValueError code>(因此使用
with self.assertRaises(ValueError):
,当然)。我的用户定义的例外 MyException 仍然无法通过单元测试。为什么要这样呢?解决方案为了能够捕获异常,你必须
/ code> it。如果你
return
它,该异常像任何其他python对象一样(并且不捕获在try
。class Class1:
'''My nifty class 1.'''
def __init __ self):
pass
def function_c1(self,some_input):
'''类1的函数。'''
如果some_statement(some_input):
raise MyException('Some error message。')
return True
b $ b要回答下面的注释,使用python的unittest模块进行单元测试,你可能会这样做:
with self.assertRaises(MyException):
Class1()。function_c1(...)
如果你停留在python2.7之前的环境中,它将是:
self.assertRaises (MyException,Class1()。function_c1,...)
但是为了您的缘故,可以使用python2.7至少: - )。
Assume two Python classes, the first of which (Class 1) contains a function (function_c1) that encompasses some statement (some_statement), which - if true - returns a customized exception (MyException). Further assume that the second class (Class 2) executes the first class and wishes to pass on the customized exception (i.e., return the same exception itself). The following code exemplifies this situation:
class Class1: ''' My nifty class 1. ''' def __init__(self): pass def function_c1(self, some_input): ''' A function of class 1. ''' if some_statement(some_input): return MyException('Some error message.') return True class Class2: ''' My nifty class 2. ''' def __init__(self): pass def function_c2(self, some_input): ''' A function of class 2. ''' my_handle = Class1() try: my_handle.function_c1(some_input) except MyException as e: return MyException(e) return True
Why does Class 2 return 'True' irrespective of whether Class 1 returns the exception?
EDIT 1: In the example above, I had deliberately chosen to
return
instead of toraise
the exception in order to render class 1 applicable to unit-testing.self.assertRaises(Class1().function_c1(some_input))
Given my admittedly incomplete understanding of Python, I see only two options to eat the cake (i.e., conduct unit-testing) and keep it too (i.e., have class 2 receive the exception from class 1): (a) I either
raise
as well asreturn
MyException in Class 1 (if that were even possible?), or (b) I use a differentassert
statement.
EDIT 2: Thanks to some excellent feedback, I now understand that my original mistake was the result of having incorrectly used
assertRaises
. It should have been used as below:class MyException(Exception): pass class Class1: ... with self.assertRaises(MyException): Class1().function_c1(some_input)
However, the unit-test only passes when Class 1 raises a built-in exception, such as
ValueError
(and consequently usingwith self.assertRaises(ValueError):
, of course). My user-defined exception MyException still does not pass the unit-test. Why could that be?解决方案In order for an exception to be catchable, you have to
raise
it. If youreturn
it, the exception acts like any other python object (and doesn't get caught in atry
block.class Class1: ''' My nifty class 1. ''' def __init__(self): pass def function_c1(self, some_input): ''' A function of class 1. ''' if some_statement(some_input): raise MyException('Some error message.') return True
To address your comment below, for unit-testing with python's unittest module, you'd probably do something like this:
with self.assertRaises(MyException): Class1().function_c1(...)
If you're stuck in a pre-python2.7 environment, it would be:
self.assertRaises(MyException, Class1().function_c1, ...)
But for your sake, I hope you can use python2.7 at least :-).
这篇关于在Python中进行单元测试时,跨类传递异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!