当前位置:网站首页>Introducing testfixture into unittest framework
Introducing testfixture into unittest framework
2022-07-04 14:09:00 【Field test record】
TestFixture yes Unittest Firmware in the frame , Write it in the class of the test case to be run , Used as the class method of this class .
1、4 There are four common methods :setUp(),tearDown(),setUpClass(),tearDownClass()
2、4 Operation rules of common methods :
setUp(): Functions that will run before running test cases , Used to set configuration information 、 Static attribute
setUpClass(): Need and @classmethod The decorator is used together , After instantiating the class , Methods that will run automatically , Mainly used for instantiating classes 、 Set some environment variables, such as database connection configuration
Use unittest.main() Method will run first setUpClass() Procedure in method
tearDown(): After running a test case , Will run , The function is to destroy the data between each test case 、 Release resources 、 Restore data
tearDownClass(): After all the test cases in the class are run , Methods that will run automatically , The function is to destroy class level resources 、 Restore data
3、 The following figure shows the sequence of running one test case at a time , cycle

4、 Example
test_Fixture_demo.py
# Examples demonstrate fixture Case study
import unittest
# Test class
class SumDemo:
def plus(self, x, y):
return x + y
# establish unittest Test class
class TestSum(unittest.TestCase):
add = None
# Write test build
def setUp(self):
self.x = 1
self.y = 1
print(" It's running setUp")
@classmethod
def setUpClass(cls):
# Instantiation SumDemo
cls.add = SumDemo()
print(" It's running setUpClass")
def tearDown(self):
del self.x
del self.y
print(" It's running tearDown")
@classmethod
def tearDownClass(cls):
del cls.add
print(" It's running 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)
Running results :

边栏推荐
- 1200. Minimum absolute difference
- MySQL5免安装修改
- CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
- Go 语言入门很简单:Go 实现凯撒密码
- Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
- golang fmt.printf()(转)
- OPPO Find N2产品形态首曝:补齐各项短板
- MySQL version 8 installation Free Tutorial
- find命令报错: paths must precede expression(转)
- 逆向调试入门-PE结构-资源表07/07
猜你喜欢

【R语言数据科学】:交叉验证再回首

免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy

2022 Shandong Province safety officer C certificate examination question bank and online simulation examination

Unity shader learning (3) try to draw a circle

基于PaddleX的智能零售柜商品识别

安装Mysql

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

Byte interview algorithm question

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

面试拆解:系统上线后Cpu使用率飙升如何排查?
随机推荐
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
源码编译安装MySQL
Summary of recent days (non-technical article)
MySQL 5 installation and modification free
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
Understanding and difference between viewbinding and databinding
JVM series - stack and heap, method area day1-2
2022 Shandong Province safety officer C certificate examination question bank and online simulation examination
392. Judgement subsequence
sharding key type not supported
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
MySQL5免安装修改
美国土安全部部长警告移民“不要踏上危险的旅程”
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
Five "potential errors" in embedded programming
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
Install MySQL
美国土安全部长:国内暴力极端主义是目前美面临的最大恐怖主义威胁之一