文章目录
一、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默认返回None
yield token,username
print('完成退出登录')
def test_search(login):
token,username=login
print(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,username
print('完成退出登录')
@pytest.fixture()
def connectDB():
print('连接数据库')
yield
print('断开数据库')
测试用例
import pytest
def test_search(login):
token,username=login
print(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,username
print('完成退出登录')
测试方法
import pytest
def test_search(login):
token,username=login
print(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.param
def 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)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123132.html