pytest测试框架fixture的应用
文章目录
- 一、fixture特点和优势
- 场景:
- 步骤:
- 二、fixture在自动化测试中的应用:作用域
- 三、fixture在自动化测试中的应用:yield
- 场景
- 解决
- 步骤
- 格式
- 四、fixture在自动化测试中的应用:数据共享
- 场景
- 解决
- 前提
- 执行
- conftest.py文件
- 测试用例
- 测试结果
- 五、fixture在自动化测试中的应用:自动应用
- 场景
- 解决
- 步骤
- conftest.py
- 测试方法
- 六、fixture在自动化测试中的应用:参数化
- 场景
- 解决
- 步骤
- 测试用例
- 测试结果
- 七、fixture用法总结
一、fixture特点和优势
1、命令灵活:对于setup、teardown,可以不起这两个名字
2、数据共享:在conftest.py配置里写的方法可以实现数据共享,不需要导入conftest.py文件,可以跨文件共享
3、scope的层次以及神奇的yield组合相当于setup和teardown
4、实现参数化</font>
场景:
测试用例执行时,有的用例需要登陆才能执行,有些用例不需要登陆。
setup和teardown 无法满足。fixture 可以。默认scope(范围)function
步骤:
1.导入 pytest
2.在登陆的函数上面加@pytest.fxture()
3.在要使用的测试方法中传入(登陆函数名称),就先登录
4.不传入的就不登陆直接执行测试方法。
import pytest#定义了登录的fixture
@pytest.fixture()
def login():print('完成登录操作')def test_search():print('搜索')def test_cart(login):print('购物车')def test_order(login):print('下单')
二、fixture在自动化测试中的应用:作用域
session:每次会话只需要运行一次,会话内所有方法、类、模块都共享这个方法
module:每个.py文件调用一次
class:每个测试类只运行一次
function:每个方法都会调用</font>
定义了登录的fixture,尽量避免以test_开头
import pytest#fixture的作用域#定义了登录的fixture,尽量避免以test_开头
#@pytest.fixture(scope='module')
@pytest.fixture(scope='function')
#@pytest.fixture(scope='class')
def login():print('完成登录操作')def test_search(login):print('搜索')def test_cart(login):print('购物车')def test_order(login):print('下单')class TestDemo:def test_case1(self,login):print('case1')def test_case2(self,login):print('case2')
三、fixture在自动化测试中的应用:yield
场景
测试之间数据准备,测试结束后销毁清除数据
解决
通过在fixture函数中加入yield关键字,yield是调用第一次返回结果,第二次执行她下面的语句返回。
步骤
在@pytest.fixture(scope=module)
在登录的方法中加入yield,之后加销毁清除的步骤
yield后面如果不加任何数据,默认返回None</font>
格式
@pytest.fixture()
def fixture_name():#setup操作yield 返回值teardown 操作
@pytest.fixture(scope='class')
def login():print('完成登录操作')token='xxxxxxxxxxxxxxx'username='kobe'#yield默认返回Noneyield token,usernameprint('完成退出登录')def test_search(login):token,username=loginprint(f'token:{token};username:{username}')print('搜索')def test_cart(login):print('购物车')def test_order(login):print('下单')
四、fixture在自动化测试中的应用:数据共享
场景
公共的模块要在不同文件中,要在大家都访问到地方
解决
使用conftest.py这个文件进行数据共享,并且他可以放在不同位置起着不同的范围共享作用
前提
conftest.py文件名是不能换的
放在项目下是全局的数据共享的地方
执行
系统执行到参数login时先从本模块中查找是否有这个名字的变量
如果没有,之后再conftest.py中找是否有
conftest.py文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/2/16 16:56
# @Author : 杜兰特
# @File : conftest.py#conftest.py 名字是固定的,不能改变
import pytest@pytest.fixture(scope='function',autouse=True)
def login():print('完成登录操作')token='qrredfdsasddsaw'username='kobe'yield token,usernameprint('完成退出登录')@pytest.fixture()
def connectDB():print('连接数据库')yieldprint('断开数据库')
测试用例
import pytestdef test_search(login):token,username=loginprint(f'token:{token};username:{username}')print('搜索')def test_cart(login):print('购物车')def test_order(login):print('下单')def test_share(connectDB):print('分享')class TestDemo:def test_case1(self,login):print('case1')def test_case2(self,login):print('case2')
测试结果
五、fixture在自动化测试中的应用:自动应用
场景
不想原测试方法有任何改动,或者全部都自动实现自动应用
解决
在使用fixture中参数autouse=True实现
步骤
在方法上面加上@pytest.fixture(autouse=True)
conftest.py
import pytest@pytest.fixture(scope='function',autouse=True)
def login():print('完成登录操作')token='qrredfdsasddsaw'username='kobe'yield token,usernameprint('完成退出登录')
测试方法
import pytestdef test_search(login):token,username=loginprint(f'token:{token};username:{username}')print('搜索')def test_cart():print('购物车')def test_order():print('下单')class TestDemo:def test_case1(self):print('case1')def test_case2(self):print('case2')
六、fixture在自动化测试中的应用:参数化
场景
测试离不开数据,为了数据灵活,一般数据都是通过参数传递的
解决
fixture通过固定参数request传递
步骤
在fixture中增加@pytest.fixture(params=[1,2,3,4,5,6])
在方法中参数传入request,方法体里面使用request.param接收参数
测试用例
import pytest@pytest.fixture(params=["curry","kobe","kd"])
def login(request):print(f'用户名:{request.param}')yield request.paramdef test_demo1(login):print(f'demo1 case:{login}')
测试结果
七、fixture用法总结
1、共享数据
2、参数化:params,request.param
3、自动执行(autouse=True),不用传入函数作为参数
4、实现setup、teardown,更加灵活
5、yield用法
6、作用域(session、module、class、function)
pytest测试框架fixture的应用相关推荐
- pytest测试框架--fixture的基本使用
1.fixture的含义 fixture的目的是提供一个测试的基线,在此基线基础上,可以更可靠的进行重复测试. 2.fixture的优势 Pytest的fixture相对于传统的xUnit的setup ...
- Pytest测试框架(二):pytest 的setup/teardown方法
系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...
- Python编程必不可少的pytest测试框架
进行编程测试重要的是为了更高效的完成功能的实现. pytest是基于unittest实现的第三方测试框架,比 unittest 更加的简洁.高效,并且可以完美兼容 unittest 的测试代码,无需对 ...
- Pytest 测试框架——数据驱动
引言 前面已经和大家介绍过 Unittest 测试框架的数据驱动框架 DDT,以及其实现原理.今天和大家分享的是 Pytest 测试框架的数据驱动,Pytest 测试框架的数据驱动是由 pytest ...
- Pytest测试框架(五):pytest + allure生成测试报告
系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...
- pytest系列——fixture函数使用(pytest测试框架测试固件)
前言 setup和teardown能实现在测试用例执行之前或之后做一些操作,但是这种是整个测试脚本全局生效的: 如果我们想实现某些用例执行之前进行登录,某些用例执行之前不需要进行登录,这种场景我们再使 ...
- pytest测试框架_聊聊 Python 的单元测试框架(三):最火的 pytest
本文首发于 HelloGitHub 公众号,并发表于 Prodesire 博客. 一.介绍 本篇文章是<聊聊 Python 的单元测试框架>的第三篇,前两篇分别介绍了标准库 unittes ...
- pytest测试框架4-插件与hook函数
一.简介 pytest的自带功能很强大,通过添加插件可以扩展功能,pytest的代码结构适合定制和扩展插件, 可以借助hook函数来实现. 把fixture函数或者hook函数添加到conftest文 ...
- Pytest测试框架的基本使用和allure测试报告
一.测试用例的识别与运行 目录识别 通过pytest.ini配置文件配置 如果未指定任何参数,则收集从testpaths(如已配置)或当前目录开始.另外,命令行参数可以在目录.文件名或节点ID的任何组 ...
最新文章
- django外调用url_Django学习(url配置及参数获取)
- 计算机应用看法,对计算机应用教学方法改革的看法
- 虚拟化四路服务器,专为虚拟化设计 戴尔R905四路服务器评测
- HDU1862 EXCEL排序
- 1996黄金一代NBA选秀
- I Hate It(线段树)
- 1400协议是什么和28181区别_1400张拆解案例,够你PPT拆解学习好几遍了!
- TPLinker 联合抽取 实体链接方式+源码分析
- TurboIM专业集成即时通讯获新宠
- c#使用Transactions类完成多个数据库的事务操作(分布式事务处理)
- 【面向校招】Golang面试题总结
- centos安装aria2c_CentOS下安装aria2教程
- 小米开源:站在巨人肩膀上的创新
- [HNOI2003]激光炸弹(二维前缀和+大坑点)
- expert个人版 sqlite_SQLite Expert Personal
- 在有已认证的公众号情况下,复用资质快速注册小程序,免除再次认证费用
- 一文解读如何评估项目的价值和可行性?
- 【优化求解】基于猫群算法CSO求解最优目标matlab源码
- VC实现对Excel表格的操作
- 记录红米K20pro至尊版刷机安装httpcanary抓包全过程
热门文章
- 大数据分析与数据分析的根本区别在哪里
- 成都中考生专门学计算机哪个学校好,「计算机网络技术专业」2021年成都哪所计算机网络技术专业学校好_学校推荐...
- 嵌入式linux开发-(一)如何编写linuxC代码并运行(ubantu20.04)
- nbu备份恢复catalog
- STC 单片机应使用何种编译器/汇编器
- html 手机 touch,手机的html上,touchstart、touchmove、touchend同时存在时,touchmove和touchend失效...
- 安装mysql5.7.msi_windows下安装mysql-installer-community-5.7.16.msi记录
- ArcGIS_空间插值分析
- 直方图匹配原理 MALAB实现
- 2021年 山东大学 算法导论考卷 回忆版