pytest进阶教程之fixture函数详解


Posted in Python onMarch 29, 2021

fixture函数存在意义

与python自带的unitest测试框架中的setup、teardown类似,pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作。但是相对来说又比setup、teardown好用。

firture相对于setup和teardown的优势

  1. 命名方式灵活,不局限于setup和teardown这几个命名
  2. conftest.py 配置里可以实现数据共享,不需要import就能自动找到一些配置
  3. scope="module" 可以实现多个.py跨文件共享前置, 每一个.py文件调用一次
  4. scope="session" 以实现多个.py跨文件使用一个session来完成多个用例

fixture函数定义

通过将fixture声明为参数名,测试用例函数可以请求fixture。fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作。

# 定义的夹具函数,使用装饰器pytest.fixture
@pytest.fixture
def my_fruit():
 print("login:用户执行登录操作")
 
# 使用夹具函数的测试用例
def test_my_fruit_in_basket(my_fruit):
 print("hello world")
 
if __name__ == '__main__':
 pytest.main(['test_login.py::test_my_fruit_in_basket', '-s'])
 
#执行结果:
collected 1 item
test_login.py login:
用户执行登录操作
hello world
.
============================== 1 passed in 0.02s ==========================

fixture作用

  • 做测试前后的初始化设置,如测试数据准备,链接数据库,打开浏览器等这些操作都可以使用fixture来实现。
  • 测试用例的前置条件可以使用fixture实现 。
  • 支持经典的xunit fixture ,像unittest使用的setup和teardown。
  • fixture可以实现unittest不能实现的功能,比如unittest中的测试用例和测试用例之间是无法传递参数和数据的,但是fixture却可以解决这个问题。

调用fixture有三种方式

Fixture名字作为测试用例的参数

可以直接使用fixture名称作为输入参数(是个典型的高阶函数),在这种情况下,fixture函数返回的fixture实例将被注入,最终在测试用例执行前执行这个装饰过的函数。如下列代码,①将返回值传递给测试用例,②通过函数入参方式,可以传入多个fixture函数

import pytest
 
@pytest.fixture
def first_entry():
 return "a"
@pytest.fixture
def order(first_entry):
 return [first_entry]
def test_string(order):
 order.append("b")
 assert order == ["a", "b"], "断言执行失败"
 
if __name__ == '__main__':
 pytest.main(['test_login.py::test_string', '-s'])

使用@pytest.mark.usefixtures('fixture')装饰器

每个函数或者类前使用@pytest.mark.usefixtures('fixture')装饰器进行装饰。

import pytest
@pytest.fixture
def my_fruit():
 print("login:用户执行登录操作")
 
# 被夹具函数装饰的测试用例
@pytest.mark.usefixtures("my_fruit")
def test_my_fruit_in_basket():
 print("hello world")
 
 
if __name__ == '__main__':
 pytest.main(['test_login.py', '-s', '-q'])
  
# 执行结果
login:用户执行登录操作 
hello world 
. 
1 passed in 0.01s

使用autouse参数

指定fixture的参数autouse=True这样模块内的每个测试用例会自动调用fixture。

import pytest
@pytest.fixture(autouse=True)
def my_fruit():
 print("login:用户执行登录操作")
 
# 被夹具函数装饰的测试用例
def test_my_fruit_in_basket():
 print("hello world")
 
 
if __name__ == '__main__':
 pytest.main(['test_login.py', '-s', '-q'])

备注: 如果fixture有返回值,那么usefixture以及autouse就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。 因此最常用的是通过参数传递的方法。

指定Fixture函数的作用范围

Fixture中的scope的参数,控制Fixture函数的作用范围

scope = ‘function' 测试函数维度,默认范围,则在测试结束时销毁fixture。

scope = ‘class' 测试类维度,在class中最后一次测试的拆卸过程中,夹具被破坏。

scope = ‘module' 测试文件维度,在模块中最后一次测试的拆卸过程中,夹具被破坏。

scope = ‘session' 测试会话维度,夹具在测试会话结束时被销毁。

fixture函数的返回值:return 和 yield 和 addfinalizer终结函数

return:

通过下面的代码,我们已经发现可以通过测试用例函数传入参数的形式,直接使用fixture函数的返回值,这个相对来说比较简单。

import pytest
 
@pytest.fixture
def first_entry():
 return "a"
@pytest.fixture
def order(first_entry):
 return [first_entry]
def test_string(order):
 order.append("b")
 assert order == ["a", "b"], "断言执行失败"
  
if __name__ == '__main__':
 pytest.main(['test_login.py::test_string', '-s'])

yield:

yeild也是一种函数的返回值类型,是函数上下文管理器,使用yield被调fixture函数执行遇到yield会停止执行,接着执行调用的函数,调用的函数执行完后会继续执行fixture函数yield关键后面的代码。因此利用fixture函数,我们可以说pytest集合了setup、teardown,既做了初始化,又做了后置的清理工作。

import pytest
from emaillib import Email, MailAdminClient
 
@pytest.fixture
def mail_admin():
 return MailAdminClient()
 
# 配置发送者的fixture函数
@pytest.fixture
def sending_user(mail_admin):
 user = mail_admin.create_user() #setup:创建发件人
 yield user      # 返回发件人
 admin_client.delete_user(user) #teardown:删除发件人
 
# 配置收件人的fixture函数
@pytest.fixture
def receiving_user(mail_admin):
 user = mail_admin.create_user() #setup:创建收件人
 yield user      #返回收件人
 admin_client.delete_user(user) #teardown:删除收件人
 
def test_email_received(sending_user, receiving_user, email):
 email = Email(subject="Hey!", body="How's it going?")
 sending_user.send_email(email, receiving_user)
 assert email in receiving_user.inbox

项目中的实际使用

翻译下面代码,在调用Entry_into_index前,启动APP,遇到yield关键字,中止fixture函数调用,执行调用函数Entry_into_index内容,在Entry_into_index函数调用后,执行yield函数后的driver.close_app(),关闭APP。

@pytest.fixture(scope='session')
def startApp_fixture(start_app):
 driver = start_app
 res = lp(driver).get_agree_info()
 try:
  assert res == "同意"
 except Exception as e:
  log.error("启动APP失败")
  log.exception(e)
  raise e
 else:
  lp(driver).click_agree()
  lp(driver).click_next_step()
  lp(driver).click_alert()
  lp(driver).click_pass()
  # 创建首页
  index_page = indexPage(driver)
  yield index_page, driver
  # 后置条件
  time.sleep(3)
  driver.close_app()
   
# 调用fixture函数
@pytest.fixture(scope='session')
def Entry_into_index(startApp_fixture)
 index_page = startApp_fixture()[0]
 driver = startApp_fixture()[1]

fixture函数需要传递参数

工厂作为固定装置:可以使用闭包,通过外部去调用函数里面函数。

工厂固定装置原因:

上面已经说过,调用fixture函数A可以通过用fixture名称作为调用函数B参数,在这种情况下,fixture函数返回的fixture实例将被注入,最终在测试用例B执行前执行这个装饰过的函数def B(A):pass。但是有个问题在给测试用例添加装饰函数时,传入的参数是fixture函数的函数名,如果需要给fixture函数添加参数时,是不可以用下面形式,代码会直接报错。原因是测试用例传入参数为fixture函数名,如果fixture函数名添加(参数)后,表现形式为add(params)实际为函数调用。可参考高阶函数与装饰器,并无此用法。

pytest进阶教程之fixture函数详解

解决方式使用闭包,如下图代码:make_customer_record函数返回的是内部函数_make_customer_record(夹具不直接返回数据,而是返回一个生成数据的函数),注意此处未加(),非函数调用,因此在测试用例中customer_1 = make_customer_record("Lisa")此处可拆解为两部分,customer_1 = make_customer_record的结果为_make_customer_record对象 ,加上("Lisa") 实际是对调_make_customer_record函数进行调用:函数名+(参数),以达到可以传参的目的。

@pytest.fixture
def make_customer_record():
 def _make_customer_record(name):
  return {"name": name, "orders": []}
 
 return _make_customer_record #注意此处不加(),非函数调用
 
def test_customer_records(make_customer_record):
 customer_1 = make_customer_record("Lisa")

 

Python 相关文章推荐
Python模块学习 re 正则表达式
May 19 Python
python获取指定网页上所有超链接的方法
Apr 04 Python
Python中join和split用法实例
Apr 14 Python
Python实现两个list对应元素相减操作示例
Jun 09 Python
python+opencv识别图片中的圆形
Mar 25 Python
对python 各种删除文件失败的处理方式分享
Apr 24 Python
python 实现语音聊天机器人的示例代码
Dec 02 Python
django框架用户权限中的session缓存到redis中的方法
Aug 06 Python
浅谈Python2之汉字编码为unicode的问题(即类似\xc3\xa4)
Aug 12 Python
TensorFlow索引与切片的实现方法
Nov 20 Python
python3操作注册表的方法(Url protocol)
Feb 05 Python
python中os.remove()用法及注意事项
Jan 31 Python
python中pandas.read_csv()函数的深入讲解
Mar 29 #Python
python编写函数注意事项总结
Mar 29 #Python
python基于tkinter制作无损音乐下载工具
Python requests库参数提交的注意事项总结
Python爬虫爬取全球疫情数据并存储到mysql数据库的步骤
Python爬虫数据的分类及json数据使用小结
Mar 29 #Python
python re模块和正则表达式
Mar 24 #Python
You might like
随机头像PHP版
2006/10/09 PHP
深入解读php中关于抽象(abstract)类和抽象方法的问题分析
2014/01/03 PHP
php判断并删除空目录及空子目录的方法
2015/02/11 PHP
php微信公众号开发之翻页查询
2018/10/20 PHP
javascript 对表格的行和列都能加亮显示
2008/12/26 Javascript
javascript 多种搜索引擎集成的页面实现代码
2010/01/02 Javascript
基于jQuery的模仿新浪微博时间的组件
2011/10/04 Javascript
js使下拉列表框可编辑不止是选择
2013/12/12 Javascript
js获取滚动距离的方法
2015/05/30 Javascript
JavaScript判断表单中多选框checkbox选中个数的方法
2015/08/17 Javascript
jQuery滚动加载图片实现原理
2015/12/14 Javascript
jQuery移动端日期(datedropper)和时间(timedropper)选择器附源码下载
2016/04/19 Javascript
无缝滚动的简单实现代码(推荐)
2016/06/07 Javascript
nodejs接入阿里大鱼短信验证码的方法
2017/07/10 NodeJs
webpack vue 项目打包生成的文件,资源文件报404问题的修复方法(总结篇)
2018/01/09 Javascript
vuex state及mapState的基础用法详解
2018/04/19 Javascript
vue+SSM实现验证码功能
2018/12/07 Javascript
ES6 Map结构的应用实例分析
2019/06/26 Javascript
[58:57]2018DOTA2亚洲邀请赛3月29日小组赛B组 Effect VS VGJ.T
2018/03/30 DOTA
Linux下使用python调用top命令获得CPU利用率
2015/03/10 Python
Django 生成登陆验证码代码分享
2017/12/12 Python
Python之pandas读写文件乱码的解决方法
2018/04/20 Python
Python实现通过继承覆盖方法示例
2018/07/02 Python
深入浅析Python获取对象信息的函数type()、isinstance()、dir()
2018/09/17 Python
Pycharm新建模板默认添加个人信息的实例
2019/07/15 Python
使用Python给头像加上圣诞帽或圣诞老人小图标附源码
2019/12/25 Python
Kipling凯浦林美国官网:世界著名时尚休闲包袋品牌
2016/08/24 全球购物
有机童装:Toby Tiger
2018/05/23 全球购物
俄罗斯韩国化妆品网上商店:Cosmasi.ru
2019/10/31 全球购物
写一个在SQL Server创建表的SQL语句
2012/03/10 面试题
会议接待欢迎词
2014/01/12 职场文书
试用期员工考核制度
2014/01/22 职场文书
员工安全承诺书
2014/05/22 职场文书
超市客服工作职责
2014/06/11 职场文书
2019已经过半,你知道年中工作总结该怎么写吗?
2019/07/03 职场文书
Python基础学习之奇异的GUI对话框
2021/05/27 Python