当前位置:网站首页>Unittest use
Unittest use
2022-06-21 07:07:00 【sl01224318】
Catalog
Introduce
unittest yes python Unit test framework , Be similar to Junit frame , Among them is 5 A more important concept , Let's share with you .
unittest Medium 5 Usage are test fixture,test case,test suite,test runner,test loader, Mastering these methods can better help us use unittest.
Conceptual analysis
TestCase
One TestCase The example of is a test case . A test case includes the setup of the pre test environment (setup), Execute test code , And the restoration of the environment after testing , unit testing (unittest) And that's the essence of it , A test case is a complete test unit , By running this test unit , You can verify a problem . for example :
class MyTestCase03(unittest.TestCase): # quote unittest.Testcase
def test01(self):
print('test01')
def test02(self):
print('test02')TestSuite
Multiple test cases come together , Namely TestSuite, It can also be understood as a collection of multiple test cases , and TestSuite You can also nest TestSuite.
TestRunner
TestRunner Is used to execute test cases , Among them run(test) Will execute TestSuite/TestCase Medium run(result) Method , for example :
class MyTestCase04(unittest.TestCase):
def test03(self):
print('test03')
def test04(self):
print('test04')
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite) # Use run() Method to execute the test case TestLoader
TestLoader It's used to load TestCase To TestSuite Medium , Some of them loadTestFrom Method , It's looking for it from all over the place TestCase, Create instances of them , then add To TestSuite in , Return to one more TserSuite example .
class MyTestCase04(unittest.TestCase):
def test03(self):
print('test03')
def test04(self):
print('test04')
if __name__ == '__main__':
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite.addTest(loader.loadTestsFromTestCase(MyTestCase04)) # Load test cases
Test fixture
Test Fixture Used before the test method , Or after the test method , The main function is to provide some devices for testing , These devices can be data , It can be an environment configuration or a pre run state , By covering TestCase() Of setUp() and tearDown() Method to implement , For example, we define a before the test case setup、teardown, In this way, each use case will be executed first setup、tewrdown Method .
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
print('setup...')
def tearDown(self) -> None:
print('teardown...')
def test01(self):
print('test01')
def test02(self):
print('test02')
if __name__ == '__main__':
unittest.main()
边栏推荐
- kubernetes集群搭建详细教程
- 【osg】OSG开发(03)——构建MSVC版的osgQt库
- 微信小程序_5,全局配置
- MySQL使用什么作为主键比较好
- Necessary free artifact for remote assistance todesk remote control software (defense, remote, debugging, office) necessary remote tools
- 怎么看小程序是谁开发的(查看小程序开发公司方法)
- Dynamic planning exercises (II)
- Product manager proficient in Axure tools
- js操作Cookie,js设置Cookie值,js读取Cookie值
- 微信小程序_4,WXSS模板样式
猜你喜欢

Google Earth Engine(GEE)——全球农田有机土壤碳和氮排放(1992-2018年度)数据集
![[FPGA wavelet transform] Verilog implementation of image 9/7 integer wavelet transform based on FPGA](/img/e2/c33848f3952ce0a3bf0d315fb0f78c.png)
[FPGA wavelet transform] Verilog implementation of image 9/7 integer wavelet transform based on FPGA

Introduction to exceptions

Ztmao theme cat WordPress Theme classic lost version /wp website template download station source code + global SEO function setting

动态规划习题(二)

EasyExcel-排除展示字段-02

Feature scaling for machine learning

ADEX governance voting: pledge reward halved

使用Loupe Cell Browser查看10X单细胞转录组分析结果

【Qt】一文总结QtCreator中MSVC编译套件
随机推荐
ADEX governance voting: pledge reward halved
缺失数据填补数据集介绍(2)——多种数据集介绍及数据集预处理(mushroom、news、spam、wine-red和yeast)
Markdown mathematical grammar [detailed summary]
Minesweeping - C language - Advanced (recursive automatic expansion + chess mark)
[mapbox] Basics
[middle order traversal of binary tree based on stack] middle order traversal of binary tree + stack, spatial complexity of O (H)
微信小程序_4,WXSS模板样式
Ztmao theme cat WordPress Theme classic lost version /wp website template download station source code + global SEO function setting
Analyse des données: indicateurs communs pour différentes industries
Bol Bohr's original dual currency driving model leads the new hotspot of dfi+nft+web3.0
Argo CD 使用
EasyExcel-简介-01
微信小程序_6,网络数据请求
Configuring the eigen3 development environment for vs2017 on win10
Kubernetes cluster setup detailed tutorial
Superparameters and model parameters
IDM移动端功能升级说明
数据分析之:不同行业的常见指标
Google Earth Engine(GEE)——全球农田有机土壤碳和氮排放(1992-2018年度)数据集
On June 13, 2022, interview questions were asked