在基于数组的列表实现中为 append 方法编写单元测试 [英] Writing unit test for append method in my array based list implementation

查看:35
本文介绍了在基于数组的列表实现中为 append 方法编写单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Python and I have just started learning how to use classes. I implemented an array-based list with a max size of 50. I also have an append method where self.count refers to the next available position in the list. Now I'm trying to write a unit test for my append method but I'm wondering, how do I check for the assertion error other than appending 50 times? Is there a way to manually change my self.count?

这是我的追加方法.

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

这是我为我的单元测试所尝试的:

This is what I tried for my unit test:

def testAppend(self):
    a_list=List()
    a_list.append(2)
    self.assertEqual(a_list[0],2)
    # test for assertion error

任何帮助将不胜感激!

好吧,在所有有用的建议之后,我意识到我应该提出一个异常.

Okay after all the useful suggestions I realised I'm supposed to raise an exception instead.

 def append(self,item):
    try:
        self.array[self.count]=item
    except IndexError:
        print('Array is full')
    self.count+=1

现在这是我的单元测试,但我收到警告

Now this is my unit test but I got the warning

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))

推荐答案

与其直接调整 count 属性,不如追加 50 次即可获得完整列表.

Rather than adjust the count attribute directly, just append to it 50 times to get a full list.

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 开始计数?这个测试无关紧要;无论您决定如何提出它,它都会测试 AssertionError.

This ensures your test does not depend on any implementation-specific details of how you cap the list size. Suppose that you changed List.append to count down from 50 instead of counting up from 0? This test doesn't care; it will test for the AssertionError no matter how you decide to raise it.

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

Note that assertions can be disabled at run-time; they are more appropriate for debugging. Instead, define your own exception to be raised when an attempt is made to append to a full list:

class ListFullError(RuntimeError):
    pass


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

这篇关于在基于数组的列表实现中为 append 方法编写单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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