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修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作。
 
 1 # 定义的夹具函数,使用装饰器pytest.fixture
2 @pytest.fixture
3 def my_fruit():
4 print("login:用户执行登录操作")
5
6 # 使用夹具函数的测试用例
7 def test_my_fruit_in_basket(my_fruit):
8 print("hello world")
9
10 if __name__ == '__main__':
11 pytest.main(['test_login.py::test_my_fruit_in_basket', '-s'])
12
13 #执行结果:
14 collected 1 item
15 test_login.py login:
16 用户执行登录操作
17 hello world
18 .
19 ============================== 1 passed in 0.02s ==========================

fixture作用

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

调用fixture有三种方式

  1. Fixture名字作为测试用例的参数
  可以直接使用fixture名称作为输入参数(是个典型的高阶函数),在这种情况下,fixture函数返回的fixture实例将被注入,最终在测试用例执行前执行这个装饰过的函数。如下列代码,①将返回值传递给测试用例,②通过函数入参方式,可以传入多个fixture函数
 1 import pytest
2
3 @pytest.fixture
4 def first_entry():
5 return "a"
6 @pytest.fixture
7 def order(first_entry):
8 return [first_entry]
9 def test_string(order):
10 order.append("b")
11 assert order == ["a", "b"], "断言执行失败"
12
13 if __name__ == '__main__':
14 pytest.main(['test_login.py::test_string', '-s'])
  1. 使用@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
  1. 使用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 #teardown:返回收件人
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)实际为函数调用。可参考高阶函数与装饰器,并无此用法。
 
 
 
 
 
 
 
 
 
解决方式使用闭包,如下图代码: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")
 

pytest进阶之fixture函数的更多相关文章

  1. pytest进阶之fixture

    前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...

  2. Pytest - 进阶功能fixture

    1. 概述 Pytest的fixture功能灵活好用,支持参数设置,便于进行多用例测试,简单便捷,颇有pythonic.如果要深入学习pytest,必学fixture. fixture函数的作用: 完 ...

  3. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  4. pytest框架之fixture前置和后置

    一.conftest.py 定义公共的fixture,多个测试类中都可以调用 pytest提供了conftest.py文件,可以将fixture定义在此文件中 运行测试用例时,不需要去导入这个文件,会 ...

  5. 【pytest官方文档】解读- 如何自定义mark标记,并将测试用例的数据传递给fixture函数

    在之前的分享中,我们知道可以使用yield或者return关键字把fixture函数里的值传递给test函数. 这种方法很实用,比如我在fixture函数里向数据库里插入必要的测试数据,那我就可以把相 ...

  6. Pytest【定制fixture】

    在pytest中的fixture是在测试函数运行前后,由pytest执行的外壳函数,fixture中的代码可以定制,满足多变的测试需求:包括定义传入测试中的数据集.配置测试前系统的初始化状态.为批量测 ...

  7. 【pytest系列】- fixture测试夹具详解

    如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html fixture的优势 ​ pyt ...

  8. Pytest进阶使用

    fixture 特点: 命令灵活:对于setup,teardown可以省略 数据共享:在conftest.py配置里写方法可以实现数据共享,不需要import导入,可以跨文件共享 scope的层次及神 ...

  9. JavaScript进阶系列04,函数参数个数不确定情况下的解决方案

    本篇主要体验函数参数个数不确定情况下的一个解决方案.先来看一段使用函数作为参数进行计算的实例. var calculate = function(x, y, fn) { return fn(x, y) ...

随机推荐

  1. PWA & Service Workers 版本更新 bug

    PWA & Service Workers 版本更新 bug PWA & Service Worker https://developer.mozilla.org/zh-CN/docs ...

  2. JavaScript Inheritance All in One

    JavaScript Inheritance All in One constructor inheritance prototype chain inheritance "use stri ...

  3. how to check website offline status in js

    how to check website offline status in js https://developer.mozilla.org/en-US/docs/Web/API/Navigator ...

  4. 大送福利!市场香饽饽VAST到底什么来头,为何被高价估值

    近日,NGK星空计划新币VAST成为了香饽饽,还未正式上线前,市场讨论的热度就居高不下.如今NGK推出1万VAST免费送新人福利更是将这波热度推向了高潮. 具体福利规则:在美国加州时间2021年2月8 ...

  5. NGK的发行量是多少?NGK销毁机制是怎么样的?

    代币销毁(Coin Burning),是指将代币从流通中永久性去除.换句话说,被销毁的代币相当于被永久性冻结,再也无法流入市场.那为什么要进行代币销毁呢? 销毁加密货币,可以使剩余加密货币的价值升高, ...

  6. 「NGK每日快讯」11.19日NGK第16期官方快讯!

  7. Vue学习笔记-vue-element-admin 按装报错再按装

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  8. 搭建Elasticsearch可视化界面 Kibana

    前言 每一个版本的es都有一个对应的Kibana版本,建议和es相同版本,官网地址: 点击进入下载地址 步骤 1.解压 tar -zxvf kibana-7.2.0-linux-x86_64 2.修改 ...

  9. Django批量插入数据和分页器

    目录 一.ajax结合sweetalert实现删除按钮动态效果 二.bulk_create批量插入数据 1. 一条一条插入 2. 批量插入 三.自定义分页器 一.ajax结合sweetalert实现删 ...

  10. LeetCode-层数最深叶子结点的和

    层数最深叶子结点的和 LeetCode-1302 这里可以采用上一题中求解二叉树的深度的方法. 因为需要记录最深结点的值的和,所以这里可以边求和,如果遇到不符合最深结点时再将和sum=0. /** * ...