当前位置:网站首页>【自动化测试】关于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中的每个元组都被认为是一条测试用例,元组中的数据就为测试用例变化的值

边栏推荐
- [note]vs2015 compilation of masm32 using MASM32 Library
- [test development] use case
- 1.4.2 Capital Market Theroy
- Configuration and practice of shardingsphere JDBC sub database separation of read and write
- EGO Planner代码解析----CMakeLists.txt和package.xml
- Modeling discussion series 143 data processing, analysis and decision system development
- Call C function in Lua
- Talking about the wavelength of laser radar
- [笔记]vs2015 编写汇编masm32之使用MASM32库
- 单片机串口通信原理和控制程序
猜你喜欢

Interpretation of mobile phone private charging protocol

Advanced Mathematics (Seventh Edition) Tongji University exercises 1-3 personal solutions

R: Airline customer value analysis practice

【LeetCode】860. Change with lemonade (2 brushes for wrong questions)

ET框架-22 创建ServerInfo实体及事件

单片机:A/D 差分输入信号

Unity Shader 学习 004-Shader 调试 平台差异性 第三方调试工具
![[test development] installation of test management tool Zen path](/img/8b/363e393bdb2d3400a2e8361af8677d.png)
[test development] installation of test management tool Zen path

Single chip microcomputer: infrared remote control communication principle

Unity shader learning 004 shader debugging platform difference third-party debugging tools
随机推荐
EGO Planner代码解析----CMakeLists.txt和package.xml
Real time requirements for 5g China Unicom repeater network management protocol
R: Employee turnover forecast practice
Unity Shader 学习 004-Shader 调试 平台差异性 第三方调试工具
leetcode. 1 --- sum of two numbers
基于PHP的轻量数码商城系统
【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
Value of line height
高等数学(第七版)同济大学 习题1-3 个人解答
ET框架-22 创建ServerInfo实体及事件
[MySQL] index and transaction
Forgotten fleeting years
Use ASE encryption and decryption cache encapsulation in Vue project
干预分析 + 伪回归
谈谈激光雷达的波长
Understand the pseudo static configuration to solve the 404 problem of refreshing the page of the deployment project
Common array methods in JS (map, filter, foreach, reduce)
Lambda终结操作查找与匹配findAny
Student management system
1-72 convert string to decimal integer