python接口自动化11-pytest入门
2024-09-05 06:34:05
前言
pytest是一个非常成熟的全功能的Python测试框架,适合从简单的单元到复杂的功能测试,主要特点有以下几点:
- 简单灵活,容易上手;
- 支持参数化;
- 能够支持简单的单元测试;
- 标记测试功能与属性
- 复杂的功能测试,比如可以做selenium等自动化测试、接口自动化测试(pytest+requests);
- pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)等;
- Skip和xfail:处理不成功的测试用例;
- 可以很好的和jenkins集成;
- 通过xdist插件分发测试到多个CPU
一、简介
1、环境搭建推荐版本匹配:pip install pytest==3.6.3
- Python3.6.x + pytest 3.6.3
- Python3.7.x + pytest 4.0.2
2、查看版本:pytest --version
C:\Users\Administrator>pytest --version
This is pytest version 3、6、3, imported from d:\path_python\lib\site-packages\pytest、py
3、pytest 命名规则:
- 文件名以test_*、py 或 *_test、py
- 类已 Test* 开头
- 函数/方法以 test_* 开头
4、pytest 直接写用例,写完 cmd 运行,不需要导入其他模块。
G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items
test_demo1.py
我是用例:a
.
我是用例:b
.
============================================== 2 passed in 0.02 seconds ===============================================
二、pytest 命令行参数介绍
1、运行规则:pytest py文件路径
C:\Users\Administrator>pytest G:\python_study\study\pytest_demo\study\test_demo.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: C:\Users\Administrator, inifile:
collected 4 items test_demo.py .... [100%] ========================== 4 passed in 0.03 seconds ===========================
2、显示打印信息(不然不会看到打印内容):pytest -s xxx
G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items test_demo1.py
我是用例:a
.
我是用例:b
. ============================================== 2 passed in 0.02 seconds ===============================================
3、显示详细信息:pytest -v xxx
G:\python_study\study\pytest_demo\study>pytest test_demo1.py -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items test_demo1.py::Test_api::test_a PASSED [ 50%]
test_demo1.py::Test_api::test_b PASSED [100%] ========================== 2 passed in 0.02 seconds ===========================
4、简洁显示信息:pytest -q xxx
G:\python_study\study\pytest_demo\study>pytest test_demo1.py -q
.. [100%]
2 passed in 0.02 seconds
5、运行指定用例:pytest -k case_name (case_name可类可函数,模糊匹配关键字),如下匹配 demo
G:\python_study\study\pytest_demo\study>pytest -k demo -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 11 items / 5 deselected test_demo.py::test_ab PASSED [ 16%]
test_demo.py::test_aba PASSED [ 33%]
test_demo.py::Test_api::test_aa PASSED [ 50%]
test_demo.py::Test_api::test_b PASSED [ 66%]
test_demo1.py::Test_api::test_a PASSED [ 83%]
test_demo1.py::Test_api::test_b PASSED [100%] =================== 6 passed, 5 deselected in 0.06 seconds ====================
6、命令行参数不分顺序,还有其他命令行参数,不一一细说:
- 运行类用例且不运行类某个用例:pytest -v -k "Test_api1 and not test_a"
- 失败停止测试:pytest -x
- 指定个数失败后停止测试:pytest --maxfail=2
- 运行上一次失败用例(或没失败的):pytest --last-failed
- 等等
7、pycharm 设置 pytest 运行用例:
更多请查看 pytest -h 或者找度娘,我们一般用以上的参数够日常使用了。欢迎来QQ交流群:482713805
最新文章
- IIS安装
- 利用 a 标签自动解析 url
- PL-SQL 存储函数和存储过程
- 【读书笔记】iOS-UIWindow-密码框
- jsp页面List迭代
- iOS 9之SFSafariViewController
- Introduction to neural network —— 该“神经网络” 下拉“祭坛”
- swift3.0 hello swift(1)
- PHP开发笔记
- 洛谷 P3384 【模板】树链剖分
- freemarker报错之十二
- Error response from daemon: conflict: unable to remove repository reference 解决方案
- CSRF自动化检测
- js发送邮件 不会调用客户端。
- HTML5:表格相关标记及其属性
- webdriver自动化脚本
- bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演
- 2015小米暑期实习笔试题_风口的猪-中国牛市(dp)
- 此实现不是Windows平台FIPS验证的加密算法的一部分
- 注解Annotation补充介绍
热门文章
- 微信小程序框架部署:mpvue+typescript
- PWA学习笔记(二)
- mybatis报错: java.lang.IllegalArgumentException invalid comparison: java.util.Date and java.lang.String
- 华为2019年NE40E-X8,承诺命令
- FS-Cache 调研
- css字体效果
- Django中获取参数(路径,查询,请求头,请求体)
- 如何解决Sublime text3文件名称中文乱码问题
- make &;&; make install的区别
- 记一起Java大对象引起的FullGC事件及GC知识梳理