Python 在基于数组的列表实现中为append方法编写单元测试

Python 在基于数组的列表实现中为append方法编写单元测试,python,unit-testing,Python,Unit Testing,我是Python新手,刚刚开始学习如何使用类。我实现了一个最大大小为50的基于数组的列表。我还有一个append方法,其中self.count引用列表中的下一个可用位置。现在我正试图为我的append方法编写一个单元测试,但我想知道,除了追加50次之外,如何检查断言错误?有没有办法手动更改我的自我计数 这是我的append方法 def append(self,item): assert self.count<=50 if self.count>50:

我是Python新手,刚刚开始学习如何使用类。我实现了一个最大大小为50的基于数组的列表。我还有一个append方法,其中self.count引用列表中的下一个可用位置。现在我正试图为我的append方法编写一个单元测试,但我想知道,除了追加50次之外,如何检查断言错误?有没有办法手动更改我的自我计数

这是我的append方法

def append(self,item):
    assert self.count<=50
    if self.count>50:
        raise AssertionError("Array is full")
    self.array[self.count]=item
    self.count+=1
任何帮助都将不胜感激

编辑:好吧,在所有有用的建议之后,我意识到我应该提出一个例外

 def append(self,item):
    try:
        self.array[self.count]=item
    except IndexError:
        print('Array is full')
    self.count+=1
现在这是我的单元测试,但我得到了警告

Warning (from warnings module):
  File "C:\Users\User\Desktop\task1unitTest.py", line 57
   self.assertRaises(IndexError,a_list.append(6))
 DeprecationWarning: callable is None

def testAppend(self):
    a_list=List()
    a_list.append(2)
    self.assertEqual(a_list[0],2)
    a_list.count=51
    self.assertRaises(IndexError,a_list.append(6))

如果您只想测试self.count高于50的时刻,只需将self.count设置为51:

a_list=List()
a_list.count = 51
a_list.append(2)

您的objects
count
属性设置为51,将引发异常。

如果您只想测试self.count高于50的时刻,只需将self.count设置为51:

a_list=List()
a_list.count = 51
a_list.append(2)

您的objects
count
属性设置为51,将引发异常。

不要直接调整
count
属性,只需将其附加50次即可获得完整列表

def test_append_full(self):
    a = List()
    for i in range(50):
        a.append(i)
    with self.assertRaises(AssertionError):
        a.append(0)
这确保您的测试不依赖于您如何限制列表大小的任何特定于实现的细节。假设您更改了
List.append
从50开始倒计时,而不是从0开始倒计时?这个测试不在乎;无论您决定如何提出,它都将测试断言错误


请注意,可以在运行时禁用断言;它们更适合于调试。相反,请定义您自己在尝试附加到完整列表时引发的异常:

class ListFullError(RuntimeError):
    pass


def append(self,item):
    if self.count > 50:
        raise ListFullError
    self.array[self.count] = item
    self.count += 1

不要直接调整
count
属性,只需将其追加50次即可获得完整列表

def test_append_full(self):
    a = List()
    for i in range(50):
        a.append(i)
    with self.assertRaises(AssertionError):
        a.append(0)
这确保您的测试不依赖于您如何限制列表大小的任何特定于实现的细节。假设您更改了
List.append
从50开始倒计时,而不是从0开始倒计时?这个测试不在乎;无论您决定如何提出,它都将测试断言错误


请注意,可以在运行时禁用断言;它们更适合于调试。相反,请定义您自己在尝试附加到完整列表时引发的异常:

class ListFullError(RuntimeError):
    pass


def append(self,item):
    if self.count > 50:
        raise ListFullError
    self.array[self.count] = item
    self.count += 1

请注意,
AssertionError
将永远不会被提升,因为
AssertionError
将首先被提升。此外,可以禁用断言。不要用它们来强制数据结构中的不变量。@chepner,正如pythonwiki所建议的那样(对于检查数据结构不变量,这可能不是强制的)。你能详细说明一下为什么不使用断言来实现这个目的吗?是的,它们在检查不变量时很有用,直到有人发现为止。它们对于强制执行不变量没有那么大的用处。
assertRaise
将调用的函数及其参数作为单独的参数,而不是调用它的结果
self.assertRaises(IndexError,a_list.append,6)
。请注意,
AssertionError
将永远不会被提升,因为
AssertionError
将首先被提升。此外,可以禁用断言。不要用它们来强制数据结构中的不变量。@chepner,正如pythonwiki所建议的那样(对于检查数据结构不变量,这可能不是强制的)。你能详细说明一下为什么不使用断言来实现这个目的吗?是的,它们在检查不变量时很有用,直到有人发现为止。它们对于强制执行不变量没有那么大的用处。
assertRaise
将调用的函数及其参数作为单独的参数,而不是调用它的结果<代码>self.assertRaises(索引器,a_list.append,6)。