当前位置:网站首页>pytest 基础认知
pytest 基础认知
2022-08-09 13:06:00 【沉觞流年】
pytest 基础认知
pytest 简介
pytest 是 python 的一种单元测试框架,与 python 自带的 unittest 测试框架类似,但是比 unittest 框架使用起来更简洁,效率更高。
根据pytest的官方网站介绍,它具有如下特点:
- 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考
- 能够支持简单的单元测试和复杂的功能测试
- 支持参数化
- 执行测试过程中可以将某些测试跳过(skip),或者对某些预期失败的case标记成失败
- 支持重复执行(rerun)失败的case
- 支持运行由nose, unittest编写的测试case
- 可生成html报告
- 方便的和持续集成工具jenkins集成
- 可支持执行部分用例
- 具有很多第三方插件,并且可以自定义扩展
pytest 安装
pip install pytest
使用该命令进行安装,安装的是最新版的pytest
安装html报告的插件:
pip install pytest-html
更多 pip相关命令,可查看以下两篇文章
pytest用例规则
pytest 测试用例编写规则
- 测试文件以 test_ 开头(以 _test 结尾也可以)
- 测试类以Test开头,并且不能带有 init 方法
- 测试函数以 test_ 开头
- 断言使用assert
pytest收集测试用例的规则
1、默认从当前目录中搜集测试用例,即在哪个目录下运行pytest命令,则从哪个目录当中搜索;
2、搜索规则:
- 符合命名规则 test_*.py 或者 *_test.py 的文件
- 以test_开头的函数名;
- 以Test开头的测试类(没有 __init__函数 )当中,以test_开头的函数
3、pytest运行规则:
- 查找当前目录及其子目录下以 test_*.py 或 *_test.py 文件,找到文件后,在文件中找到以 test_ 开头函数并执行。
- pytest 是以方法为单位发现用例的,写不写测试类不重要。
4、pytest执行顺序:
- 文件名称执行顺序按 ASCII码顺序执行。哪个文件先识别,就先执行哪个文件里的用例
- 文件内部按代码的先后顺序执行用例
pytest 运行的几种方式
1、编辑器 pycharm
test_demo1.py
def func(x):
return x
def test_answer():
assert func(5)==5
此时右键运行,可以发现,控制台并没有返回任何结果
所以,需要指定以pytest的方式运行该文件
可以看到,现在是默认以python文件的方式运行的
所以,选中该test_demo1文件后,点击左上角 – ,删除该文件的运行方式,然后点击 +
选择以 pytest方式运行该文件
如果选择以 pytest方式运行该文件后,没有默认导入相应的配置内容,就需要自己去添加
添加成功后,右上角会变成以pytest方式运行该文件
点击运行即可
此时断言是成功的,所以用例通过
2、命令执行
1. pytest
新建一个类,然后在类里面新建一个函数
test_demo1.py
def func(x):
return x
def test_answer():
assert func(5)==5
class TestDemo:
def test_answer2(self):
assert func(4) == 5
打开pycharm的终端,或者使用 cmd 命令进入终端也行,然后进入需要运行 pytest 用例的目录下,输入命令 pytest
用例的执行顺序,先执行了test_answer,然后再执行test_answer2,根据代码顺序由上而下执行
2. python -m pytest
在当前文件夹下新建一个demo目录,然后在demo目录下新建一个py文件
test_demo2.py
def func(x):
return x
def test_answer3():
assert func(2)==2
在终端输入命令:
python -m pytest
这种方式和输入命令 pytest效果是一样的
可以发现,用例执行了,rootdir 指的是需要发现测试用例的目录。只要指定的目录或其子目录下存在测试用例,就会被pytest机制发现并执行
边栏推荐
- How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
- The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)
- LeetCode 37. Solve Sudoku
- Q_04_06 把它放在一起:传送
- [MRCTF2020]套娃-1
- Realization of RTSP Protocol
- 陈强教授《机器学习及R应用》课程 第十六章作业
- render解析
- Professor Chen Qiang the machine learning and R application course chapter 18 assignments
- npm install失败
猜你喜欢
蓝桥历届真题-门牌制作
5G China unicom repeater network management protocol real-time requirements
搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
乐东消防救援大队应邀为干部开展消防安全培训
GIN Bind mode to get parameters and form validation
现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
Oracle Recovery Tools修复空闲坏块
Map mixed density function and quantile added line
面试攻略系列(三)-- 高级开发工程师面试问些啥?
随机推荐
ArcEngine(九)图形绘制
剑指 Offer 43. 1~n 整数中 1 出现的次数(递归、数学)
NC84 完全二叉树结点数
puzzle(016.5)逻辑电路
LeetCode 37.解数独
音频基础学习——声音的本质、术语与特性
Unity3d_API_Gyroscope 陀螺仪的接口
RobotFramework 之 Evaluate
FFmpeg相机花屏花图问题解决方法
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
01_iTween_第一天--小球抛物线
陈强教授《机器学习及R应用》课程 第十三章作业
问题系列-如何修改或更新localhost里的值
FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)
R language kaggle game data exploration and visualization
陈强教授《机器学习及R应用》课程 第十六章作业
Realization of RTSP Protocol
Map mixed density function and quantile added line
npm install失败
FFMPEG multimedia file processing (deletion and renaming of ffmpeg files)