Python单元测试
程序员文章站
2024-02-26 23:20:28
...
- 继承unittest.TestCase
import unittest
class JustForTest(unittest.TestCase):
def runTest(self):
length = 10
self.assertEqual(10, length)
- 断言方法的使用
In [1]: a = [1, 2, 3]
In [2]: b = 2
In [3]: assert b in a
In [4]: assert b not in a , 'False'
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-4-a05ac47c78d0> in <module>()
----> 1 assert b not in a , 'False'
AssertionError: False
-
常用断言方法
assertEqual 如果两个值相等,则pass
assertNotEqual 如果两个值不相等,则pass
assertTrue 判断bool值为True,则pass
assertFalse 判断bool值为False,则pass
assertIn 判断a是否in b
assertNotIn 判断a是否 not in b
assertIsNone 不存在,则pass
assertIsNotNone 存在,则pass -
单元测试基本写法
import unittest
class TestClass(unittest.TestCase):
#该方法会首先执行,相当于做测试前的准备工作
def setUp(self):
pass
#该方法会在测试代码执行完后执行,相当于做测试后的扫尾工作
def tearDown(self):
pass
#测试代码
def test_app_exists(self):
pass