zoukankan      html  css  js  c++  java
  • Python_单元测试

    Stack.py

     1 class Stack:
     2     def __init__(self, size = 10):
     3         self._content = []                 #使用列表存放栈的元素
     4         self._size = size                  #初始栈大小
     5         self._current = 0                  #栈中元素个数初始化为0
     6         
     7     def empty(self):
     8         self._content = []
     9         self._current = 0
    10         
    11     def isEmpty(self):
    12         if not self._content:
    13             return True
    14         else:
    15             return False
    16 
    17     def setSize(self, size):
    18         #如果缩小栈空间,则删除指定大小之后的已有元素
    19         if size < self._current:
    20             for i in range(size, self._current)[::-1]:
    21                 del self._content[i]
    22             self._current = size
    23         self._size = size
    24     
    25     def isFull(self):
    26         if self._current == self._size:
    27             return True
    28         else:
    29             return False
    30         
    31     def push(self, v):
    32         if len(self._content) < self._size:
    33             self._content.append(v)
    34             self._current = self._current+1  #栈中元素个数加1
    35         else:
    36             print('Stack Full!')
    37             
    38     def pop(self):
    39         if self._content:
    40             self._current = self._current-1 #栈中元素个数减1
    41             return self._content.pop()
    42         else:
    43             print('Stack is empty!')
    44             
    45     def show(self):
    46         print(self._content)
    47 
    48     def showRemainderSpace(self):
    49         print('Stack can still PUSH ', self._size-self._current, ' elements.')
    50 
    51 if __name__ == '__main__':
    52     print('Please use me as a module.')

    单元测试:pTest.py

     1 '''Python单元测试
     2  3 1.软件测试对于保证软件质量非常重要,尤其是系统升级过程中对代码的改动不影响原有功能,是未来重构代码的信心保证。
     4 2.几乎所有软件公司都有专门的测试团队来保证软件质量,但作为程序员,首先应该保证自己编写的代码准确无误地实现了预定功能,单元测试是保证模块质量的重要手段之一
     5 **从软件工程角度来讲
     6 3.白盒测试主要通过阅读程序代码来判断是否符合功能要求,对于复杂的业务逻辑白盒测试难度非常大,一般以黑盒测试为主,白盒测试为辅
     7 4.黑盒测试不关系模块的内部实现方式,只关心其功能是否正确,通过精心设计一些测试用例检验模块的输入和输出是否正确来判断是否符合预定的功能需求
     8 '''
     9 #导入要测试的模块
    10 import Stack
    11 #Python单元测试标准库
    12 import unittest
    13 class TestStack(unittest.TestCase):
    14     def setUp(self):
    15         self.fp = open('sample.txt','a+')
    16 
    17     def tearDown(self):
    18         self.fp.close()
    19 
    20     def test_isEmpty(self):
    21         try:
    22             s=Stack.Stack()
    23             #确保函数返回结果为True
    24             self.assertTrue(s.isEmpty())
    25             self.fp.write('isEmpty passed
    ')
    26         except Exception as e:
    27             self.fp.write('isEmpty failed
    ')
    28 
    29     def test_isFull(self):
    30         try:
    31             s = Stack.Stack(3)
    32             s.push(1)
    33             s.push(2)
    34             s.push(3)
    35             self.assertTrue(s.isFull())
    36             self.fp.write('isFull passed
    ')
    37         except Exception as e:
    38             self.fp.write('isFull failed
    ')
    39 
    40     def test_pushpop(self):
    41         try:
    42             s = Stack.Stack()
    43             s.push(3)
    44             #确保入栈后立刻出栈得到原来的元素
    45             self.assertEqual(s.pop(),3)
    46             s.push('a')
    47             self.assertEqual(s.pop(),'a')
    48             self.fp.write('push and pop passed
    ')
    49         except Exception as  e:
    50             self.fp.write('push or pop failed
    ')
    51 
    52     def test_setSize(self):
    53         try:
    54             s = Stack.Stack(8)
    55             for i in range(8):
    56                 s.push(i)
    57             self.assertTrue(s.isFull())
    58             #测试扩大栈空间是否正常工作
    59             s.setSize(9)
    60             s.push(8)
    61             self.assertTrue(s.isFull())
    62             self.assertEqual(s.pop(),3)
    63             #测试缩小空间是否正常工作
    64             s.setSize(4)
    65             self.assertEqual(s.pop(),3)
    66             self.fp.write('setSize passed
    ')
    67         except Exception as e:
    68             self.fp.write('setSize failed
    ')
    69 if __name__ == '__main__':
    70     unittest.main()
    71 '''
    72 输出
    73 ....
    74 ----------------------------------------------------------------------
    75 Ran 4 tests in 0.023s
    76 
    77 OK
    78 '''
  • 相关阅读:
    Java文件读取
    Java继承
    JAVA程序提示错误:需要class,interface或enum解决方法
    SQL 修改列名
    转 父表字表统计查询的sql练习
    powerdesigner12.5入门教程
    现实世界
    oracle添加联合主键
    hashtable的用法
    JQ 1
  • 原文地址:https://www.cnblogs.com/cmnz/p/7049673.html
Copyright © 2011-2022 走看看