当前位置:网站首页>Unittest框架中引入TestFixture
Unittest框架中引入TestFixture
2022-07-04 12:49:00 【大田测试录】
TestFixture是Unittest框架中的固件,写在要运行的测试用例所在类中,作为该类的类方法去使用的。
1、4个常见方法有:setUp(),tearDown(),setUpClass(),tearDownClass()
2、4个常见方法的运行规则:
setUp():运行测试用例之前都会运行的函数,用于设置配置信息、静态属性
setUpClass():需要和@classmethod装饰器一块使用,实例化类后,会自动运行的方法,主要用于实例化类、设置某些环境变量如数据库连接配置
使用unittest.main()方法运行时就会先运行setUpClass()方法中程序
tearDown():每运行完一个测试用例后,会运行,作用就是销毁每个测试用例之间的数据、释放资源、还原数据
tearDownClass():类中所有测试用例全部运行完后,会自动运行的方法,作用是销毁类级别的资源、还原数据
3、下图为每次运行一个测试用例的顺序,循环往复

4、例子
test_Fixture_demo.py
# 实例演示fixture 案例
import unittest
# 测试类
class SumDemo:
def plus(self, x, y):
return x + y
# 创建unittest测试类
class TestSum(unittest.TestCase):
add = None
# 编写测试构建
def setUp(self):
self.x = 1
self.y = 1
print("运行了setUp")
@classmethod
def setUpClass(cls):
#实例化SumDemo
cls.add = SumDemo()
print("运行了setUpClass")
def tearDown(self):
del self.x
del self.y
print("运行了tearDown")
@classmethod
def tearDownClass(cls):
del cls.add
print("运行了teardownClass")
def test_01(self):
result = self.add.plus(2, 3)
print(result)
def test_02(self):
result = self.add.plus(3, 3)
print(result)
运行结果:

边栏推荐
- SQL language
- 锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东
- 2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
- Source code compilation and installation of MySQL
- 英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
- CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
- 一次 Keepalived 高可用的事故,让我重学了一遍它
- Interviewer: what is the internal implementation of hash data type in redis?
- sharding key type not supported
- 结合案例:Flink框架中的最底层API(ProcessFunction)用法
猜你喜欢

Summary of recent days (non-technical article)

锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东

中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东

sharding key type not supported

JVM series - stack and heap, method area day1-2

小程序直播 + 电商,想做新零售电商就用它吧!

. Net delay queue

【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对

Interviewer: what is the internal implementation of hash data type in redis?

SCM polling program framework based on linked list management
随机推荐
Lick the dog until the last one has nothing (state machine)
Introduction to reverse debugging PE structure resource table 07/07
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
Three schemes to improve the efficiency of MySQL deep paging query
常见 content-type对应表
学习项目是自己找的,成长机会是自己创造的
数据库公共字段自动填充
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
qt 怎么检测鼠标在不在某个控件上
博士申请 | 西湖大学学习与推理系统实验室招收博后/博士/研究实习等
读取 Excel 表数据
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
C語言宿舍管理查詢軟件
源码编译安装MySQL
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
安装trinity、解决报错
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
逆向调试入门-PE结构-资源表07/07
js中的变量提升和函数提升
xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)