当前位置:网站首页>【自动化测试框架】关于unittest你需要知道的事
【自动化测试框架】关于unittest你需要知道的事
2022-07-06 16:26:00 【测试小娜】
一、UnitTest单元测试框架提供了那些功能
1.提供用例组织和执行
如何定义一条“测试用例”?
如何灵活地控制这些“测试用例”的执行?
2.提供丰定的断言方法
当测试用例的执行结果与预期结果不一致时,判定测试用例失败。在自动化测试中,通过“断言”来判定测试用例执行成功与否,测试框架提供丰富的断言方法,例如:判断相等/不相等包含/不包含、True/False等
3.提供丰富的日志
需要从结果中清晰地看出失败的原因。另外,我们还需要统计测试用例的执行结果,如总执行时间、失败测试用例数、成功测试用例数等,这些功能也是由单元测试框架提供的。
二、认识unittest
1.引入unittest模块,自定义的类必须要继承unittest模块的TestCase类
2.创建的测试方法,必须以test开头
3.重要的概念
3.1 TestCase
Test Case是最小测试单元,用于检查特定输入集合的特定返回值。unittest提供的TestCase基类被自定义的测试类进行继承,它可以用来创建新的测试用例
3.2 TestSuite
测试套件事测试用例\测试套件或者两者的集合,用于组装一组要运行的测试
3.3 Test Runner
Test Runner是一个组件,用于协调测试的执行并向用户提供结果。Test Runner可以用图形界面、文本界面或返回特殊值来展示执行测试的结果
3.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中的每个元组都被认为是一条测试用例,元组中的数据就为测试用例变化的值
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走
这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
边栏推荐
- The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
- The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
- 若依请求url中带有jsessionid的解决办法
- Realize colorful lines and shape your heart
- The programmer refused the offer because of low salary, HR became angry and netizens exploded
- Implementation steps of mysql start log in docker
- Without CD, I'll teach you a trick to restore the factory settings of win10 system
- How to implement Lua entry of API gateway
- Master binary tree in one article
- app通用功能測試用例
猜你喜欢
【无人机】多无人协同任务分配程序平台含Matlab代码
Implementation steps of mysql start log in docker
吴恩达2022机器学习课程评测来了!
Today's sleep quality record 78 points
Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN
js對JSON數組的增删改查
Daily question brushing record (XV)
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
The method of reinstalling win10 system is as simple as that
leetcode:236. 二叉树的最近公共祖先
随机推荐
STM32通过串口进入和唤醒停止模式
The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
Gold three silver four, don't change jobs
Experiment 4: installing packages from Gui
Competition between public and private chains in data privacy and throughput
Unity 颜色板|调色板|无级变色功能
Knowledge * review
B 站弹幕 protobuf 协议还原分析
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
How does crmeb mall system help marketing?
(1) Chang'an chain learning notes - start Chang'an chain
mysql-cdc 的jar包 ,在flink运行模式下,是不是要放在不同的地方呢?
Talking about the current malpractice and future development
电脑重装系统u盘文件被隐藏要怎么找出来
What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
How can Oracle CDC deserialize with jsondebeziumdeserializationschema
Summary of three methods for MySQL to view table structure
Close unregistering application XXX with Eureka with status down after Eureka client starts
Design a red envelope grabbing system
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃