当前位置:网站首页>[automated test] what you need to know about unittest
[automated test] what you need to know about unittest
2022-06-13 04:14:00 【YZL40514131】
One The unit test framework provides those functions
1 Provides use case organization and execution
How to define a “ The test case ”?
How to flexibly control these “ The test case ” Implementation ?
2 Provide abundant assertion methods
When the execution result of the test case is inconsistent with the expected result , Determine that the test case failed . In automated testing , adopt “ Assertion ” To determine whether the test case execution is successful , The testing framework provides rich assertion methods , for example : Judge equal / Unequal inclusion / It doesn't contain 、True/False etc.
3 Provide rich logs
The reason for the failure needs to be clearly seen from the results . in addition , We also need to count the execution results of test cases , Such as total execution time 、 Number of failed test cases 、 Number of successful test cases, etc , These functions are also provided by the unit test framework .
Two know unittest
1 introduce unittest modular , Custom classes must inherit unittest Modular TestCase class
2 Test method created , Must be test start
3 Important concepts
1)TestCase
Test Case Is the smallest test unit , A specific return value used to check a specific set of inputs .unittest Provided TestCase The base class is inherited by the custom test class , It can be used to create new test cases
2)TestSuite
A test suite is a test case \ Test suite or a collection of both , Used to assemble a set of tests to run .
3)Test Runner
Test Runner Is a component , Used to coordinate test execution and provide results to users .Test Runner You can use a graphical interface 、 A text interface or a special value is returned to display the results of the test execution
4)Test Fixture
Test Fixture Represents the environmental preparation required to perform one or more tests , And associated cleanup actions .
3、 ... and assert methods

Four Organization of test cases
1 unittest Medium TestLoader Class provides the discover() Method can find test cases from multiple files
2 Find all test modules in the specified directory and its subdirectories , Only matching file names can be loaded , If you are not starting the top-level Directory , Then the top-level directory must be specified separately
discover(start_dir,pattern=’ test*.py’ ,top_level_dir=None)
3 start_dir: Module name or test case directory to be tested
4 pattern= 'test*.py’: Matching principle of test case file name
5 top_level_dir: Top level directory of the test module , If there is no top-level Directory , The default is None
5、 ... and Execution order of test cases
Multiple directories > Multiple test files > Multiple test classes > Multiple test methods
Execute the test case of multi-level directory
unittest The default in accordance with the ASCII Code sequence loading test cases ( The alphabetical and numerical order is 0-9,A-Z,a-z)
Execute multi-level subdirectories to lower one __init__.py file , This file is used to mark a directory as a standard python modular 
import unittest
test_dir = './test_case'
suite=unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite)
6、 ... and Skip tests and expected failures

7、 ... and Data driven applications
Installation mode
pip install parameterized
Usage mode
adopt @parameterized.expand() To decorate test cases ,expand Each tuple in is considered a test case , The data in the tuple is the change value of the test case

边栏推荐
- Data analysis report
- Redis-HyperLogLog-基数统计算法
- 单片机:PCF8591硬件接口
- 5G China unicom AP:B SMS ASCII 转码要求
- Modeling discussion series 143 data processing, analysis and decision system development
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-2 personal solutions
- Cache read / write -- write
- [test development] advanced part - Classification of various test technologies
- 重读经典:《End-to-End Object Detection with Transformers》
- ROS话题与节点
猜你喜欢

Sword finger offer II 022 Entry node of a link in a linked list

Introduction to MCU peripherals: temperature sensor DS18B20

1.4.2 Capital Market Theroy

Interpretation of mobile phone private charging protocol

【ZeloEngine】本地化流程/ImGui中文化

单片机:A/D(模数转换)的主要指标

单片机:红外遥控通信原理

史上最详细的Swin-Transformer 掩码机制(mask of window attentation)————shaoshuai

5g China Unicom ap:b SMS ASCII transcoding requirements

Dumi construit un blog documentaire
随机推荐
单片机:D/A 输出
单片机:RS485 通信与 Modbus 协议
R: Airline customer value analysis practice
Google Chrome browser reports an error: net:: err_ BLOCKED_ BY_ CLIENT
Use ASE encryption and decryption cache encapsulation in Vue project
Lenovo notebook computer uses the insert key. When the mouse becomes a small square, how to solve it
leetcode.1 --- 两数之和
单片机串口通信原理和控制程序
Discussion sur la modélisation de la série 143
Real time question answering of single chip microcomputer / embedded system
JS common array methods
Lambda termination operation find and match nonematch
单片机/嵌入式的实时性疑问解答
Difference between OKR and KPI
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-3 personal solutions
单片机:红外遥控通信原理
How can a sweeping robot avoid obstacles without "mental retardation"? Analysis of five mainstream obstacle avoidance techniques
Uni app enables pull-up loading and pull-down refresh (pull-down with animation)
Sword finger offer II 022 Entry node of a link in a linked list
[kubernetes series] pod chapter actual operation