当前位置:网站首页>自动化测试——pytest框架介绍及示例
自动化测试——pytest框架介绍及示例
2022-06-26 09:34:00 【坦桑尼亚奥杜威峡谷能人】
pytest 介绍及特点:
pytest是一个非常成熟的全功能的python第三方测试框架
特点:
1. 简单灵活,容易上手
2. 支持参数化
3. 能够支持简单的单元测试和复杂的功能测试;也可以用来做selenium/appium等自动化测试,接口自动化测试(常见方式:pytest+request+allure)
4. 有很多第三方插件,还可以自定义扩展,如
pytest-selenium(集成selenium)、
pytest-html(html测试报告生成)、
pytest-rerunfailures(失败case重试)、
pytest-xdist(多CPU分发)、
pytest-ordering(控制测试函数执行顺序)等
5. 测试用例的skip(跳过测试⽅法)和xfail ( 执⾏预期失败操作 ) 处理
6. 可以很好的和Jenkins集成
7. 支持 allure 报告
安装:
pip3 insatll pytest
pytest --version
运行方式:
>> 命令行 << :
1. 指定执行的测试文件:(常用)
pytest 路径/py文件名
pytest datas/testlogin.py
注意:<默认搜索> 的是test开头的⽂件,Test开头的类,⼩写的test开头的⽅法
2. 带其他参数运行:
pytest 路径/py文件名 -v # 显示测试的详细参数信息
pytest 路径/py文件名 -s # 显示测试执行的输出信息
3. 指定执行的测试类:
pytest 测试文件::测试类
4. 指定执行的测试用例:
pytest 测试文件::测试类::测试方法
>> 脚本里执行 <<:
1. pytest.main()
pytest初始化和结束⽅法:
**函数级别**
setup()和 teardown()
1.初始化和结束⽅法必须在测试类的内部存在
2.setup运⾏于每个测试⽅法开始,teardown运⾏于每个测试⽅法结束,
有多少个测试⽅法会运⾏多少次初始化和结束⽅法
如果⽅法没有断⾔语句,那么默认断⾔成功的
**类级别**
setup_class() 和 teardown_class()
1.存在于测试类内部
2.只会在类开始运⾏setup_class ,类的结束运⾏ teardown_class
不关⼼测试类内部有多少个测试⽅法
测试函数运⾏顺序:由上⾄下去运⾏
pytest配置⽂件
**配置⽂件名字:**
pytest.ini tox.ini setup.cfg
通常放在测试根⽬录下⾯
配置⽂件⾸⾏必须式: [pytest]
命令⾏参数:addopts = 命令⾏参数
搜索测试⽬录:testpaths = 某个⽬录(通常是相对于根⽬录⽽⾔)
搜索测试⽂件:
python_files = ⽂件名
通常会使⽤通配符⽅式 "*":代表任意字符
搜索测试类:
python_classes = 类名
通常建议 通配符 "*"
搜索测试⽅法:
python_functions = ⽅法名
通常建议 通配符 "*"
配置⽂件好处:
可以约束 ⽂件名,类名,⽅法名
配置⽂件中不允许出现中⽂
运⾏过程:
1.命令进⼊项⽬根⽬录
2.输⼊ pytest ->查找当前⽬录下的配置⽂件
3.匹配配置⽂件中命令⾏参数
4.搜索某个⽬录 -> 某些⽂件 -> 某些测试类 -> 某些测试⽅法
如果出现 "no tests ran " ->查找配置⽂件中 搜索⽬录->搜索⽂件->搜索类->搜索⽅法
pytest 测试报告:
自动化测试——同一个项目下共用3种方法 ( pytest + allure 、unitest + HTMLTestRunner、unitest + unittestreport )实践记录
边栏推荐
- 2021-11-29 轨迹规划五次多项式
- Catalogue gradué de revues scientifiques et technologiques de haute qualité dans le domaine de l'informatique
- Notes on sports planning on November 22, 2021
- Wechat official account reported error 10003
- 【Open5GS】Open5GS安装配置
- Enter the page input box to automatically obtain the focus
- jz2440---使用uboot烧录程序
- LeetCode 498. Diagonal traversal
- Flink入门——单词统计
- I am in Zhongshan. Where can I open an account? Is online account opening safe?
猜你喜欢

2021年全国职业院校技能大赛(中职组)网络安全竞赛试题(1)详细解析教程

Champions League data set (Messi doesn't cry - leaving Barcelona may reach another peak)

install opencv-contrib-dev to use aruco code

2021-11-22 运动规划杂记
![[trajectory planning] testing of ruckig Library](/img/c7/51c0f6dc3bf7c7fa4528118a4c32fa.png)
[trajectory planning] testing of ruckig Library

Joint Noise-Tolerant Learning and Meta Camera Shift Adaptation for Unsupervised Person Re-ID

What you need to know to test -- URL, weak network, interface, automation
![[pulsar learning] pulsar Architecture Principle](/img/ec/5ab9aabc2beafd4238dc8055ba6fb2.png)
[pulsar learning] pulsar Architecture Principle

Learning to Generalize Unseen Domains via Memory-based Multi-Source Meta-Learning for Person Re-ID

Flutter's brain map notes are easy to find and search!
随机推荐
How to create an IE tab in edge browser
Learning to Generalize Unseen Domains via Memory-based Multi-Source Meta-Learning for Person Re-ID
Mysql database field query case sensitive setting
【AAAI 2021】Few-Shot One-Class Classification via Meta-Learning 【FSOCC via Meta-learning】
异常记录-23
jz2440---使用uboot烧录程序
Enter the page input box to automatically obtain the focus
Catalogue gradué de revues scientifiques et technologiques de haute qualité dans le domaine de l'informatique
thinkphp6.0的第三方扩展包,支持上传阿里云,七牛云
使用递归或while循环获取父/子层级结构的名称
做测试需要知道的内容——url、弱网、接口、自动化、
Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
Force buckle ----- remove the maximum and minimum values from the array
"One week's data collection" -- combinational logic circuit
2021-11-29 轨迹规划五次多项式
力扣------从数组中移除最大值和最小值
【CVPR 2019】Semantic Image Synthesis with Spatially-Adaptive Normalization(SPADE)
Kubernetes cluster deployment (v1.23.5)
mysql 数据库字段查询区分大小写设置
LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改