为什么 python unittest assertRaises 方法不注册抛出的异常?
Why does python unittest assertRaises method not register exceptions that are thrown?
我有一个类似于 的问题,但是解决方案对我没有帮助,因为我只向函数传递了一个参数。
我有以下代码:
def outer():
def tuple_unpacker( mytuple ):
try:
a,b,c = mytuple
return a+b+c
except ValueError:
print('pass tuple with exactly 3 elements')
except TypeError:
print('pass only integers in tuple')
return tuple_unpacker
我使用 python unittest 模块进行测试。但是,即使出现我的异常,断言也会失败。
class TestInner(unittest.TestCase):
def test_tuple_unpacker(self):
func = outer()
self.assertRaises(TypeError, func, (1,'a',1))
self.assertRaises(ValueError, func, (1,1,1,1))
当我运行这个的时候,traceback是:
pass only integers in tuple
F
======================================================================
FAIL: test_tuple_unpacker (__main__.TestInner)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/malte/TDDE23_git/lab5/test_test.py", line 21, in test_tuple_unpacker
self.assertRaises(TypeError, func, (1,'a',1))
AssertionError: TypeError not raised by tuple_unpacker
----------------------------------------------------------------------
Ran 1 test in 0.000s
如您所见,它打印了 pass only integers in tuple
因此抛出了异常。
如果我切换位置,问题是相同的,所以首先测试 ValueError 并失败。
奇怪的是,如果我将断言更改为:
class TestInner(unittest.TestCase):
def test_tuple_unpacker(self):
func = outer()
self.assertRaises(TypeError, func((1,'a',1)))
self.assertRaises(ValueError, func((1,1,1,1)))
它适用于 TypeError,但不适用于 ValueError。
这是回溯:
pass only integers in tuple
pass tuple with exactly 3 elements
E
======================================================================
ERROR: test_tuple_unpacker (__main__.TestInner)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/malte/TDDE23_git/lab5/test_test.py", line 22, in test_tuple_unpacker
self.assertRaises(ValueError, func((1,1,1,1)))
File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/case.py", line 731, in assertRaises
return context.handle('assertRaises', args, kwargs)
File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/case.py", line 201, in handle
callable_obj(*args, **kwargs)
TypeError: 'NoneType' object is not callable
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
感谢所有指导。
tuple_unpacker
打印然后抑制 TypeError
和 ValueError
异常。您的测试会检查这些异常,但由于未重新引发异常,因此测试失败。功能或测试有错误,需要更改。
我有一个类似于
我有以下代码:
def outer():
def tuple_unpacker( mytuple ):
try:
a,b,c = mytuple
return a+b+c
except ValueError:
print('pass tuple with exactly 3 elements')
except TypeError:
print('pass only integers in tuple')
return tuple_unpacker
我使用 python unittest 模块进行测试。但是,即使出现我的异常,断言也会失败。
class TestInner(unittest.TestCase):
def test_tuple_unpacker(self):
func = outer()
self.assertRaises(TypeError, func, (1,'a',1))
self.assertRaises(ValueError, func, (1,1,1,1))
当我运行这个的时候,traceback是:
pass only integers in tuple
F
======================================================================
FAIL: test_tuple_unpacker (__main__.TestInner)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/malte/TDDE23_git/lab5/test_test.py", line 21, in test_tuple_unpacker
self.assertRaises(TypeError, func, (1,'a',1))
AssertionError: TypeError not raised by tuple_unpacker
----------------------------------------------------------------------
Ran 1 test in 0.000s
如您所见,它打印了 pass only integers in tuple
因此抛出了异常。
如果我切换位置,问题是相同的,所以首先测试 ValueError 并失败。
奇怪的是,如果我将断言更改为:
class TestInner(unittest.TestCase):
def test_tuple_unpacker(self):
func = outer()
self.assertRaises(TypeError, func((1,'a',1)))
self.assertRaises(ValueError, func((1,1,1,1)))
它适用于 TypeError,但不适用于 ValueError。
这是回溯:
pass only integers in tuple
pass tuple with exactly 3 elements
E
======================================================================
ERROR: test_tuple_unpacker (__main__.TestInner)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/malte/TDDE23_git/lab5/test_test.py", line 22, in test_tuple_unpacker
self.assertRaises(ValueError, func((1,1,1,1)))
File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/case.py", line 731, in assertRaises
return context.handle('assertRaises', args, kwargs)
File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/case.py", line 201, in handle
callable_obj(*args, **kwargs)
TypeError: 'NoneType' object is not callable
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
感谢所有指导。
tuple_unpacker
打印然后抑制 TypeError
和 ValueError
异常。您的测试会检查这些异常,但由于未重新引发异常,因此测试失败。功能或测试有错误,需要更改。