fixture可以当做参数传入

定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开。fixture是有返回值得,没有返回值默认为None。用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。

ex:

import pytest

@pytest.fixture()
def test1():
a = 'leo'
return a def test2(test1):
assert test1 == 'leo' if __name__ == '__main__':
pytest.main('-q test_fixture.py') 输出:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 item test_fixture.py . [100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0

使用多个fixture

如果用例需要用到多个fixture的返回数据,fixture也可以返回一个元祖,list或字典,然后从里面取出对应数据。

ex:

import pytest

@pytest.fixture()
def test1():
a = 'leo'
b = '123456'
print('传出a,b')
return (a, b) def test2(test1):
u = test1[0]
p = test1[1]
assert u == 'leo'
assert p == '123456'
print('元祖形式正确') if __name__ == '__main__':
pytest.main('-q test_fixture.py') 输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 item test_fixture.py 传出a,b
.元祖形式正确
[100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0

当然也可以分成多个fixture,然后在用例中传多个fixture参数

import pytest

@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a @pytest.fixture()
def test2():
b = '123456'
print('传出b')
return b def test3(test1, test2):
u = test1
p = test2
assert u == 'leo'
assert p == '123456'
print('传入多个fixture参数正确') if __name__ == '__main__':
pytest.main('-q test_fixture.py') 输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 item test_fixture.py
传出a
传出b
.传入多个fixture参数正确

fixture互相调用

import pytest

@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a def test2(test1):
assert test1 == 'leo'
print('fixture传参成功') if __name__ == '__main__':
pytest.main('-q test_fixture.py') 输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 item test_fixture.py
传出a
.fixture传参成功
[100%] ========================== 1 passed in 0.03 seconds ===========================
Process finished with exit code 0

介绍完了fixture的使用方式,现在介绍一下fixture的作用范围(scope)

fixture的作用范围

fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function

-function:每一个函数或方法都会调用

-class:每一个类调用一次,一个类中可以有多个方法

-module:每一个.py文件调用一次,该文件内又有多个function和class

-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

fixture源码详解

fixture(scope='function',params=None,autouse=False,ids=None,name=None):

scope:有四个级别参数"function"(默认),"class","module","session"

params:一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它。

autouse:如果True,则为所有测试激活fixture func可以看到它。如果为False则显示需要参考来激活fixture

ids:每个字符串id的列表,每个字符串对应于params这样他们就是测试ID的一部分。如果没有提供ID它们将从params自动生成

name:fixture的名称。这默认为装饰函数的名称。如果fixture在定义它的统一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽,解决这个问题的一种方法时将装饰函数命令"fixture_<fixturename>"然后使用"@pytest.fixture(name='<fixturename>')"。

具体阐述一下scope四个参数的范围

scope="function"

@pytest.fixture()如果不写参数,参数就是scope="function",它的作用范围是每个测试用例来之前运行一次,销毁代码在测试用例之后运行。

import pytest

@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a @pytest.fixture(scope='function')
def test2():
b = '男'
print('\n传出b')
return b def test3(test1):
name = 'leo'
print('找到name')
assert test1 == name def test4(test2):
sex = '男'
print('找到sex')
assert test2 == sex if __name__ == '__main__':
pytest.main('-q test_fixture.py') 输出结果: platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items test_fixture.py
传出a
.找到name 传出b
.找到sex
[100%] ========================== 2 passed in 0.04 seconds ===========================

放在类中实现结果也是一样的

import pytest

@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a @pytest.fixture(scope='function')
def test2():
b = '男'
print('\n传出b')
return b class TestCase:
def test3(self, test1):
name = 'leo'
print('找到name')
assert test1 == name def test4(self, test2):
sex = '男'
print('找到sex')
assert test2 == sex if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py']) 输出结果: platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items test_fixture.py
传出a
.找到name 传出b
.找到sex
[100%] ========================== 2 passed in 0.03 seconds ===========================
Process finished with exit code 0

scope="class"

fixture为class级别的时候,如果一个class里面有多个用例,都调用了次fixture,那么此fixture只在此class里所有用例开始前执行一次。

import pytest

@pytest.fixture(scope='class')
def test1():
b = '男'
print('传出了%s, 且只在class里所有用例开始前执行一次!!!' % b)
return b class TestCase:
def test3(self, test1):
name = '男'
print('找到name')
assert test1 == name def test4(self, test1):
sex = '男'
print('找到sex')
assert test1 == sex if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py']) 输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items test_fixture.py 传出了男, 且只在class里所有用例开始前执行一次!!!
.找到name
.找到sex
[100%] ========================== 2 passed in 0.05 seconds ===========================
Process finished with exit code 0

 scope="module"

fixture为module时,在当前.py脚本里面所有用例开始前只执行一次。

import pytest
##test_fixture.py @pytest.fixture(scope='module')
def test1():
b = '男'
print('传出了%s, 且在当前py文件下执行一次!!!' % b)
return b def test3(test1):
name = '男'
print('找到name')
assert test1 == name class TestCase: def test4(self, test1):
sex = '男'
print('找到sex')
assert test1 == sex if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py']) 输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items test_fixture.py 传出了男, 且在当前py文件下执行一次!!!
.找到sex
.找到name
[100%] ========================== 2 passed in 0.03 seconds ===========================
Process finished with exit code 0

scope="session"

fixture为session级别是可以跨.py模块调用的,也就是当我们有多个.py文件的用例的时候,如果多个用例只需调用一次fixture,那就可以设置为scope="session",并且写到conftest.py文件里。

conftest.py文件名称时固定的,pytest会自动识别该文件。放到项目的根目录下就可以全局调用了,如果放到某个package下,那就在改package内有效。

文件目录为

import pytest
# conftest.py @pytest.fixture(scope='session')
def test1():
sex = '男'
print('获取到%s' % sex)
return sex
import pytest
# test_fixture.py def test3(test1):
name = '男'
print('找到name')
assert test1 == name if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])
import pytest
# test_fixture1.py class TestCase: def test4(self, test1):
sex = '男'
print('找到sex')
assert test1 == sex if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py'])

如果需要同时执行两个py文件,可以在cmd中在文件py文件所在目录下执行命令:pytest -s test_fixture.py test_fixture1.py

执行结果为:

================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:
collected 2 items test_fixture.py 获取到男
找到name
.
test_fixture1.py 找到sex
. ============================================== 2 passed in 0.05 seconds ===============================================

调用fixture的三种方法

1.函数或类里面方法直接传fixture的函数参数名称

import pytest
# test_fixture1.py @pytest.fixture()
def test1():
print('\n开始执行function') def test_a(test1):
print('---用例a执行---') class TestCase: def test_b(self, test1):
print('---用例b执行') 输出结果:
test_fixture1.py
开始执行function
.---用例a执行--- 开始执行function
.---用例b执行
[100%] ========================== 2 passed in 0.05 seconds ===========================
Process finished with exit code 0

2.使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

import pytest
# test_fixture1.py @pytest.fixture()
def test1():
print('\n开始执行function') @pytest.mark.usefixtures('test1')
def test_a():
print('---用例a执行---') @pytest.mark.usefixtures('test1')
class TestCase: def test_b(self):
print('---用例b执行---') def test_c(self):
print('---用例c执行---') if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py']) 输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 3 items test_fixture1.py
开始执行function
.---用例a执行--- 开始执行function
.---用例b执行--- 开始执行function
.---用例c执行---
[100%] ========================== 3 passed in 0.06 seconds ===========================
Process finished with exit code 0

叠加usefixtures

如果一个方法或者一个class用例想要同时调用多个fixture,可以使用@pytest.mark.usefixture()进行叠加。注意叠加顺序,先执行的放底层,后执行的放上层。

import pytest
# test_fixture1.py @pytest.fixture()
def test1():
print('\n开始执行function1') @pytest.fixture()
def test2():
print('\n开始执行function2') @pytest.mark.usefixtures('test1')
@pytest.mark.usefixtures('test2')
def test_a():
print('---用例a执行---') @pytest.mark.usefixtures('test2')
@pytest.mark.usefixtures('test1')
class TestCase: def test_b(self):
print('---用例b执行---') def test_c(self):
print('---用例c执行---') if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py']) 输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 3 items test_fixture1.py
开始执行function2 开始执行function1
.---用例a执行--- 开始执行function1 开始执行function2
.---用例b执行--- 开始执行function1 开始执行function2
.---用例c执行---
[100%] ========================== 3 passed in 0.03 seconds ===========================
Process finished with exit code 0

usefixtures与传fixture区别

 如果fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。

当fixture需要用到return出来的参数时,只能讲参数名称直接当参数传入,不需要用到return出来的参数时,两种方式都可以。

fixture自动使用autouse=True

当用例很多的时候,每次都传这个参数,会很麻烦。fixture里面有个参数autouse,默认是False没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了

autouse设置为True,自动调用fixture功能

import pytest
# test_fixture1.py @pytest.fixture(scope='module', autouse=True)
def test1():
print('\n开始执行module') @pytest.fixture(scope='class', autouse=True)
def test2():
print('\n开始执行class') @pytest.fixture(scope='function', autouse=True)
def test3():
print('\n开始执行function') def test_a():
print('---用例a执行---') def test_d():
print('---用例d执行---') class TestCase: def test_b(self):
print('---用例b执行---') def test_c(self):
print('---用例c执行---') if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py']) 输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 4 items test_fixture1.py
开始执行module 开始执行class 开始执行function
.---用例a执行--- 开始执行class 开始执行function
.---用例d执行--- 开始执行class 开始执行function
.---用例b执行--- 开始执行function
.---用例c执行---
[100%]

pytest框架fixture的使用的更多相关文章

  1. 【Pytest04】全网最全最新的Pytest框架fixture应用篇(2)

    一.Fixture参数之params参数可实现参数化:(可以为list和tuple,或者字典列表,字典元祖等) 实例如下: import pytest def read_yaml(): '] @pyt ...

  2. 【Pytest03】全网最全最新的Pytest框架fixture应用篇(1)

    fixtrue修饰器标记的方法通常用于在其他函数.模块.类或者整个工程调用时会优先执行,通常会被用于完成预置处理和重复操作.例如:登录,执行SQL等操作. 完整方法如下:fixture(scope=' ...

  3. pytest框架: fixture之conftest.py

    原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown ...

  4. pytest框架之fixture详细使用

    本人之前写了一套基于unnitest框架的UI自动化框架,但是发现了pytest框架之后觉得unnitest太low,现在重头开始学pytest框架,一边学习一边记录,和大家分享,话不多说,那就先从p ...

  5. 『德不孤』Pytest框架 — 11、Pytest中Fixture装饰器(一)

    目录 1.Fixture装饰器的用途 2.Fixture参数说明 3.Fixture装饰器简单应用 4.yield执行后置函数 1.Fixture装饰器的用途 做测试前后的初始化设置,如测试数据准备, ...

  6. 『德不孤』Pytest框架 — 12、Pytest中Fixture装饰器(二)

    目录 5.addfinalizer关键字 6.带返回值的Fixture 7.Fixture实现参数化 (1)params参数的使用 (2)进阶使用 8.@pytest.mark.usefixtures ...

  7. Appium 并发多进程基于 Pytest框架

    前言: 之前通过重写unittest的初始化方法加入设备参数进行并发,实现了基于unittest的appium多设备并发,但是考虑到unittest的框架实在过于简陋,也不方便后期的Jenkins的持 ...

  8. pytest 框架自动化Selenium 之yield 使用

    环境 python 3.7 由于3.0-3.5以下部分pytest可能有部分兼容问题安装建议2.7-2.9,3.5-最新 pip install pytest专属 pytest框架包 pip inst ...

  9. pytest框架使用教程

    Pytest框架 一.简介 pytest:基于unittest之上的单元测试框架 有什么特点? 自动发现测试模块和测试方法 断言更加方便,assert + 表达式,例如 assert 1 == 1 灵 ...

随机推荐

  1. beego搭建api服务

    beego介绍 beego是一个Golang实现的开源Go应用开发框架,他可以用来快速开发 API.Web 及后端服务等各种应用,是一个 RESTful的框架,主要设计灵感来源于tornado.sin ...

  2. 将Flink计算完毕后的数据Sink到Nebula

    Flink是目前流计算的隐形王者,在国际国内有有庞大的拥趸. Nebula是国产图数据库的后起之秀,在DBEngines中排名也逐年上升. 将两者进行结合,可以产生很多应用场景:比如实时计算服务链路调 ...

  3. Jenkins自动化CI&CD流水线

    1 环境说明 主机名称 IP cpu核数/内存/硬盘 安装软件 用途 controlnode 172.16.1.120 2/2/60 git 代码仓库 slavenode1 172.16.1.121 ...

  4. ps 合并两张图片

    1.ps 打开第一张图片,2.打开另一张图片为图层.3.选中图层,创建蒙版: 4.点击蒙版+按Alt键,打开蒙版:5.打开蒙版后选中渐变工具,途中黑色为不显示区域,(注意渐变模式要改为正常):6.调整 ...

  5. Tomcat 中文乱码,设置UTF-8

    1.修改tomcat的conf目录下 server.xml文件加上 URIEncoding="UTF-8" <Connector port="8080" ...

  6. Raspberry Pi:树莓派安装Kali2021新版本

    准备材料 树莓派4B kali系统镜像 SDFormatter (格式化工具) Win32DiskImager (镜像拷录工具) 镜像下载 kali下载地址:https://www.offensive ...

  7. c盘爆满清理解决方案----Dism

    前言:在以前windons操作系统,很容易导致c盘爆满,原因了就那么几个,升级,app默认安装,打补丁等等.还有种蛇皮现象,就是无缘无故c盘就标红了,进去看看,又个c盘的系统文件,不敢删了,怕系统就崩 ...

  8. C语言常用函数笔记

    strcmp 比较字符串: sscanf 读取格式化的字符串中的数据: memset 初始化内存的"万能函数",通常为新申请的内存进行初始化工作.对一段内存空间全部设置为某个字符, ...

  9. Ionic命令笔记

    Ionic命令:ionic serve 开启服务调试ionic cordova prepare android 生成android原生项目 ionic cordova run browser 打包成混 ...

  10. linux驱动之LED驱动

    通过之前的学习,了解到linux驱动编写的流程是:先通过注册函数注册我们编写的入口函数,然后在入口函数中获取设备号->注册字符设备->自动创建设备节点->获取设备树信息,最后通过销毁 ...