当前位置:网站首页>自动化测试——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 )实践记录
边栏推荐
- Summary of common commands of vim
- Leetcode connected to rainwater series 42 (one dimension) 407 (2D)
- Kubernetes cluster deployment (v1.23.5)
- "One week's data collection" -- combinational logic circuit
- Comparison of similar PMS in QPM
- Abnormal record-23
- Flink入门——单词统计
- Super data processing operator helps you finish data processing quickly
- 我在中山,到哪里开户比较好?在线开户安全么?
- logback
猜你喜欢

install realsense2: The following packages have unmet dependencies: libgtk-3-dev

Mysql database field query case sensitive setting

There is a strong demand for enterprise level data integration services. How to find a breakthrough for optimization?

Badge collection 6:api\_ Use of level

Badge series 5: use of codecov

A Style-Based Generator Architecture for Generative Adversarial Networks

Construction practice of bank intelligent analysis and decision-making platform

GAN Inversion: A Survey

install realsense2: The following packages have unmet dependencies: libgtk-3-dev

c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
随机推荐
Catalogue gradué de revues scientifiques et technologiques de haute qualité dans le domaine de l'informatique
How does flutter transfer parameters to the next page when switching pages?
LeetCode 958. Completeness checking of binary tree
Redis notes (13) - scan and keys search for specific prefix key fields (command format, usage examples, locating large keys)
install ompl. sh
Super data processing operator helps you finish data processing quickly
工企专利匹配数据(数十万数据量)1998-2014年
Solve Django's if Version (1, 3, 3): raise improverlyconfigured ('mysqlclient 1.3.3 or new is required
How to view the data mini map quickly and conveniently after importing data in origin
【CVPR 2019】Semantic Image Synthesis with Spatially-Adaptive Normalization(SPADE)
力扣------从数组中移除最大值和最小值
教你用shell脚本检测服务器程序是否在运行
【CVPR 2021】 Lifelong Person Re-Identification via Adaptive Knowledge Accumulation
【CVPR 2021】DatasetGAN: Efficient Labeled Data Factory with Minimal Human Effort
爬虫相关文章收藏:pyppeteer 、Burpsuite
Learning to Generalize Unseen Domains via Memory-based Multi-Source Meta-Learning for Person Re-ID
SQL modification of table structure
The third-party extension package of thinkphp6.0 supports uploading to Alibaba cloud and qiniu cloud
Differences between VI and vim and common commands
My creation anniversary