当前位置:网站首页>【自动化测试】关于unittest你需要知道的事
【自动化测试】关于unittest你需要知道的事
2022-06-13 04:02:00 【YZL40514131】
一 单元测试框架提供了那些功能
1 提供用例组织和执行
如何定义一条“测试用例”?
如何灵活地控制这些“测试用例”的执行?
2 提供丰定的断言方法
当测试用例的执行结果与预期结果不一致时,判定测试用例失败。在自动化测试中,通过“断言”来判定测试用例执行成功与否,测试框架提供丰富的断言方法,例如:判断相等/不相等包含/不包含、True/False等
3 提供丰富的日志
需要从结果中清晰地看出失败的原因。另外,我们还需要统计测试用例的执行结果,如总执行时间、失败测试用例数、成功测试用例数等,这些功能也是由单元测试框架提供的。
二 认识unittest
1 引入unittest模块,自定义的类必须要继承unittest模块的TestCase类
2 创建的测试方法,必须以test开头
3 重要的概念
1)TestCase
Test Case是最小测试单元,用于检查特定输入集合的特定返回值。unittest提供的TestCase基类被自定义的测试类进行继承,它可以用来创建新的测试用例
2)TestSuite
测试套件事测试用例\测试套件或者两者的集合,用于组装一组要运行的测试.
3)Test Runner
Test Runner是一个组件,用于协调测试的执行并向用户提供结果。Test Runner可以用图形界面、文本界面或返回特殊值来展示执行测试的结果
4)Test Fixture
Test Fixture代表执行一个或多个测试所需的环境准备,以及关联的清理动作。
三 断言方法
四 测试用例的组织
1 unittest中的TestLoader类提供的discover()方法可以从多个文件中查找测试用例
2 找到指定目录及其子目录下的所有测试模块,只有匹配的文件名才能被加载,如果启动的不是顶层目录,那么顶层目录必须单独指定
discover(start_dir,pattern=’ test*.py’ ,top_level_dir=None)
3 start_dir:待测试的模块名或测试用例目录
4 pattern= 'test*.py’:测试用例文件名的匹配原则
5 top_level_dir:测试模块的顶层目录,如果没有顶层目录,则默认为None
五 测试用例的执行顺序
多个目录>多个测试文件>多个测试类>多个测试方法
执行多级目录的测试用例
unittest默认按照ASCII码的顺序加载测试用例(字母与数字顺序为0-9,A-Z,a-z)
执行多级子目录下放一个__init__.py文件,该文件的作用是将一个目录标记一个标准python模块
import unittest
test_dir = './test_case'
suite=unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite)
六 跳过测试和预期失败
七 数据驱动应用
安装方式
pip install parameterized
使用方式
通过@parameterized.expand()去装饰测试用例,expand中的每个元组都被认为是一条测试用例,元组中的数据就为测试用例变化的值
边栏推荐
- 单片机:NEC 协议红外遥控器
- Goframe day 5
- 高等数学(第七版)同济大学 习题1-3 个人解答
- Lambda终结操作查找与匹配anyMatch
- 单片机:I2C通信协议讲解
- Redis-HyperLogLog-基数统计算法
- UE4 learning notes - functions of terrain tool
- Discussion sur la modélisation de la série 143
- [Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster
- 建模杂谈系列143 数据处理、分析与决策系统开发的梳理
猜你喜欢
Use ASE encryption and decryption cache encapsulation in Vue project
Single chip microcomputer: basic concepts of a/d and d/a
GoFrame第五天
【ZeloEngine】本地化流程/ImGui中文化
Solution to failure to download files by wechat scanning QR code
The most detailed swing transformer mask of window attachment in history -- Shaoshuai
MCU: NEC protocol infrared remote controller
Lightweight digital mall system based on PHP
Intervention analysis + pseudo regression
SCM: introduction to Modbus communication protocol
随机推荐
Summary of meeting between president Ren and scientists and experts in system engineering
Lambda termination operation find and match nonematch
Zoom and move the H5 part of the mobile end
Unity Shader 学习 004-Shader 调试 平台差异性 第三方调试工具
UE4 learning notes - functions of terrain tool
Single chip microcomputer: a/d differential input signal
单片机串口通信原理和控制程序
Principle and control program of single chip microcomputer serial port communication
Meaning of different values of margin and padding
Introduction to MCU peripherals: temperature sensor DS18B20
[MySQL] index and transaction
Koa file upload and download
LVS四層負載均衡集群(3)集群功能分類 - HPC
Answer private message @ Tiantian Wx //2022-6-12 C language 51 single chip microcomputer led analog traffic light
Lambda终结操作count
单片机:A/D(模数转换)的主要指标
Difference between OKR and KPI
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-3 personal solutions
[test development] use case
[Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster