比较两个看似相同的 IndexErrors 时 AssertEqual 失败
AssertEqual failing when comparing two seemingly identical IndexErrors
我有一个如下所示的 DynamicArray class。 (我只收录了相关的方法,其他的可以看https://www.geeksforgeeks.org/implementation-of-dynamic-array-in-python/)
import ctypes
class DynamicArray:
'''
Dynamic Array class
'''
def __init__(self):
self.n = 0 # count number of elements
self.capacity = 1 # default capacity
self.A = self.make_array(self.capacity)
def __len__(self):
"""
Return number of elements in the array
"""
return self.n
def __getitem__(self,k):
"""
Return element at k index
"""
#Check if K index is out of bounds#
if not 0 <= k < self.n:
return IndexError('{} is out of bounds'.format(k))
return self.A[k] #Retrieve from the array at index k#
那我下面还有一个单元测试文件
from DynamicArray import DynamicArray
import unittest
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
self.a = DynamicArray() # empty array
self.b = DynamicArray()
self.b.append(0)
self.c = DynamicArray()
self.c.append(0)
self.c.append(1)
def test_getitem(self):
self.assertEqual(self.a.__getitem__(0),IndexError('0 is out of bounds'))
当我 运行 我希望 self.a.__getitem__(0)
抛出 IndexError('0 is out of bounds')
的测试时,我不明白为什么断言会失败?唯一的区别是 self.a.__getitem__(0)
将产生 IndexError('{} is out of bounds'.format(0))
,这在我看来与 IndexError('0 is out of bounds')
相同
我尝试了 运行 宁下面的代码来查看字符串本身是否有任何不同
if '{} is out of bounds'.format(0) == '0 is out of bounds':
print('str equal')
if '{} is out of bounds'.format(0).__len__() == '0 is out of bounds'.__len__():
print('len equal')
if IndexError('{} is out of bounds'.format(0)) == IndexError('0 is out of bounds'):
print('IndexError equal')
并确认只有第三个if语句没有打印出来
下面是控制台的照片
提前致谢。欢迎建设性的批评和反馈。
异常无法与assertEqual
相提并论。
with self.assertRaises(IndexError, msg='0 is out of bounds'):
self.a[0]
并且异常必须 raise
ed 才能被捕获。
你回来了 IndexError
raise IndexError('{} is out of bounds'.format(k))
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises
我有一个如下所示的 DynamicArray class。 (我只收录了相关的方法,其他的可以看https://www.geeksforgeeks.org/implementation-of-dynamic-array-in-python/)
import ctypes
class DynamicArray:
'''
Dynamic Array class
'''
def __init__(self):
self.n = 0 # count number of elements
self.capacity = 1 # default capacity
self.A = self.make_array(self.capacity)
def __len__(self):
"""
Return number of elements in the array
"""
return self.n
def __getitem__(self,k):
"""
Return element at k index
"""
#Check if K index is out of bounds#
if not 0 <= k < self.n:
return IndexError('{} is out of bounds'.format(k))
return self.A[k] #Retrieve from the array at index k#
那我下面还有一个单元测试文件
from DynamicArray import DynamicArray
import unittest
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
self.a = DynamicArray() # empty array
self.b = DynamicArray()
self.b.append(0)
self.c = DynamicArray()
self.c.append(0)
self.c.append(1)
def test_getitem(self):
self.assertEqual(self.a.__getitem__(0),IndexError('0 is out of bounds'))
当我 运行 我希望 self.a.__getitem__(0)
抛出 IndexError('0 is out of bounds')
的测试时,我不明白为什么断言会失败?唯一的区别是 self.a.__getitem__(0)
将产生 IndexError('{} is out of bounds'.format(0))
,这在我看来与 IndexError('0 is out of bounds')
我尝试了 运行 宁下面的代码来查看字符串本身是否有任何不同
if '{} is out of bounds'.format(0) == '0 is out of bounds':
print('str equal')
if '{} is out of bounds'.format(0).__len__() == '0 is out of bounds'.__len__():
print('len equal')
if IndexError('{} is out of bounds'.format(0)) == IndexError('0 is out of bounds'):
print('IndexError equal')
并确认只有第三个if语句没有打印出来
下面是控制台的照片
提前致谢。欢迎建设性的批评和反馈。
异常无法与assertEqual
相提并论。
with self.assertRaises(IndexError, msg='0 is out of bounds'):
self.a[0]
并且异常必须 raise
ed 才能被捕获。
你回来了 IndexError
raise IndexError('{} is out of bounds'.format(k))
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises