当前位置:网站首页>【自动化测试框架】关于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群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
边栏推荐
- MySQL connected vscode successfully, but this error is reported
- Today's sleep quality record 78 points
- Experiment 4: installing packages from Gui
- 今日睡眠质量记录78分
- Ajout, suppression et modification d'un tableau json par JS
- MVC and MVVM
- flinksql select id ,count(*) from a group by id .
- koa2对Json数组增删改查
- 【212】php发送post请求有哪三种方法
- Without CD, I'll teach you a trick to restore the factory settings of win10 system
猜你喜欢

Do you still have to rely on Simba to shout for a new business that is Kwai?

Today's sleep quality record 78 points

Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN

js對JSON數組的增删改查

Server SMP, NUMA, MPP system learning notes.

Talking about the current malpractice and future development

MySQL connected vscode successfully, but this error is reported

Entropy information entropy cross entropy

基于SSM框架实现的房屋租赁管理系统

leetcode:236. The nearest common ancestor of binary tree
随机推荐
谁说新消费品牌大溃败?背后有人赢麻了
With the help of this treasure artifact, I became the whole stack
Summary of three methods for MySQL to view table structure
Koa2 addition, deletion, modification and query of JSON array
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
Close unregistering application XXX with Eureka with status down after Eureka client starts
The best sister won the big factory offer of 8 test posts at one go, which made me very proud
求帮助xampp做sqlilab是一片黑
The method of reinstalling win10 system is as simple as that
Automatic test tool katalon (WEB) test operation instructions
App general function test cases
(1) Chang'an chain learning notes - start Chang'an chain
Wind chime card issuing network source code latest version - commercially available
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务...
Server SMP, NUMA, MPP system learning notes.
Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
js导入excel&导出excel
JS import excel & Export Excel
ArrayExpress数据库里的细胞只有两个txt是不是只能根据Line到ENA下载测序跑矩阵?
How to answer the dualistic opposition of Zhihu