当前位置:网站首页>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机制发现并执行
边栏推荐
- Q_04_04 Q#类型模型
- Q_04_06 把它放在一起:传送
- Q_06_01 Q#编程语言
- NC53 删除链表的倒数第n个节点
- kustomize entry example and basic syntax instructions
- RobotFramework 之 资源文件
- Time series analysis course lab report
- 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)
- [FPGA Tutorial Case 48] Image Case 8 - Realization of Converting RGB Image to HSV Image Based on FPGA, Assisted Verification by MATLAB
- IDEA Gradle 常遇问题(一)
猜你喜欢
5G Unicom Network Management Design Ideas
RobotFramework 之 条件判断
PO、DO、TO、VO、DO、DTO、DAO、POJO都是什么?
利用信号灯和共享内存实现进程间同步通信
WSA toolkit installed app store tip doesn't work how to solve?
Periodic sharing of Alibaba Da Tao system model governance
5G China unicom repeater network management protocol real-time requirements
puzzle(016.5)逻辑电路
程序员的七夕怎么过?不会是写代码吧
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
随机推荐
蓝桥杯线上模拟赛——Flex 经典骰子布局
技嘉显卡 RGBFusion 不能调光解决方法
Unity3d_API_Gyroscope 陀螺仪的接口
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
[FPGA Tutorial Case 48] Image Case 8 - Realization of Converting RGB Image to HSV Image Based on FPGA, Assisted Verification by MATLAB
ARM board adds routing function
微服务+微信小程序实现社区服务
telnet+ftp to control and upgrade the device
面试攻略系列(四)-- 你不知道的大厂面试
An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
NC40 链表相加(二)
Microsoft 10/11 命令行打开系统设置页(WUAP,!WIN32)
GIN初探,环境安装
NC193 二叉树的前序遍历
Unity3d_API_GPS_LocationService
Q_06_04 语句和其他构造
搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
FFmpeg相机花屏花图问题解决方法
FFmpeg多媒体文件处理(FFMPEG日志系统)
WSA toolkit installed app store tip doesn't work how to solve?