单元测试用例代码实例
# -*- coding: utf-8 -*-
# @Date : 2018-12-21
# @Author : Peng Shiyu
import unittest
# 继承unittest.TestCase
class MyTest(unittest.TestCase):
# 必须使用@classmethod 装饰器,所有test运行前运行一次
@classmethod
def setUpClass(cls):
print(\"类测试开始...\")
# 必须使用 @ classmethod装饰器, 所有test运行完后运行一次
@classmethod
def tearDownClass(cls):
print(\"类测试结束\")
# 每个测试用例执行之前做操作
def setUp(self):
print(\"方法测试开始...\")
# 每个测试用例执行之后做操作
def tearDown(self):
print(\"方法测试结束\")
# 测试用例
def test_print(self):
print(\"测试输出\")
# 测试用例
def test_equal(self):
print(\"测试相等\")
self.assertEqual(\"a\", \"a\")
if __name__ == \'__main__\':
# 运行所有的测试用例
unittest.main()
\"\"\"
类测试开始...方法测试开始...
测试相等
方法测试结束
方法测试开始...
测试输出
方法测试结束
类测试结束
\"\"\"
继续阅读与本文标签相同的文章
上一篇 :
Django的缓冲机制
下一篇 :
4个隐藏的微信功能,超级实用但可能你还不知道!
-
Mybatis之discriminator(鉴别器)详解
2026-05-18栏目: 教程
-
前端进阶|第十一天 当全局变量,块变量,函数叫了同一个名字。。
2026-05-18栏目: 教程
-
Leetcode 542:01 矩阵 01 Matrix
2026-05-18栏目: 教程
-
LeetCode 733: 图像渲染 flood-fill
2026-05-18栏目: 教程
-
Spring Cloud Alibaba 实战(二) - 关于Spring Boot你不可不知道的实情
2026-05-18栏目: 教程
