类级别 @classmethod
函数级别
import unittest
import time
class TestDemo(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print("类级别的前置")
@classmethod
def tearDownClass(cls) -> None:
print("类级别的后置")
#函数级别的前置
def setUp(self) -> None:
print("函数级别的前置")
#函数级别的后置
def tearDown(self) -> None:
print("函数级别的后置")
def test_01(self):
time.sleep(1)
print("测试用例01")
def test_02(self):
time.sleep(1)
print("测试用例02")
def test_03(self):
try:
time.sleep(1)
self.assertEqual(1,2)
print("测试用例03")
except Exception as e:
# print("错误原因:",e)
raise AssertionError(e)
def test_04(self):
time.sleep(1)
print("测试用例04")
if __name__ == '__main__':
unittest.main()
0-9
1、用例执行按照Ascii码顺序
#打印对应字符的ascii码
print(ord("a")) #97
#讲ascii转换为对应的字符
print(chr(102))
2、测试用例类
self.assertEqual(1,1) 断言是否相等
断言的特点:
1、unittest以程序运行过程中是否抛出异常来判断用例是否执行成功
2、如果断言失败程序会抛出异常,框架就会把这个用例标记为失败
3、只要你的程序中执行出现了其他异常,框架也会去捕获异常并将用例标记为失败
1、try…except 捕获到的异常一定要抛出来给框架,否则框架就会认为用例执行成功的
2、手动抛异常:raise AssertionError(e)
def test_03(self):
try:
time.sleep(1)
self.assertEqual(1,2)
print("测试用例03")
except Exception as e:
# print("错误原因:",e)
raise AssertionError(e)