pytest文档24-fixture的作用范围(scope)
fixture作用范围
fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function
fixture(scope="function", params=None, autouse=False, ids=None, name=None):
"""使用装饰器标记fixture的功能
** 作者:上海-悠悠 QQ交流群:588402570**
可以使用此装饰器(带或不带参数)来定义fixture功能。 fixture功能的名称可以在以后使用
引用它会在运行测试之前调用它:test模块或类可以使用pytest.mark.usefixtures(fixturename标记。
测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入。
:arg scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session".
:arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它
:arg autouse: 如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture
:arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
:arg name: fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名
“fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')“”。
- function 每一个函数或方法都会调用
- class 每一个类调用一次,一个类可以有多个方法
- module,每一个.py文件调用一次,该文件内又有多个function和class
- session 是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module
scope="function"
@pytest.fixture()如果不写参数,默认就是scope="function",它的作用范围是每个测试用例来之前运行一次,销毁代码在测试用例运行之后运行。
import pytest
@pytest.fixture()
def first():
print("\n获取用户名")
a = "yoyo"
return a
@pytest.fixture(scope="function")
def sencond():
print("\n获取密码")
b = "123456"
return b
def test_1(first):
'''用例传fixture'''
print("测试账号:%s" %first)
assert first == "yoyo"
def test_2(sencond):
'''用例传fixture'''
print("测试密码:%s" %sencond)
assert sencond == "123456"
if __name__ == "__main__":
pytest.main(["-s", "test_fixture7.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 2 items
test_fixture7.py
获取用户名
测试账号:yoyo
.
获取密码
测试密码:123456
.
========================== 2 passed in 0.01 seconds ===========================
用例放到类里面也一样
import pytest
@pytest.fixture()
def first():
print("\n获取用户名")
a = "yoyo"
return a
@pytest.fixture(scope="function")
def sencond():
print("\n获取密码")
b = "123456"
return b
class TestCase():
def test_1(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo"
def test_2(self, sencond):
'''用例传fixture'''
print("测试密码:%s" % sencond)
assert sencond == "123456"
if __name__ == "__main__":
pytest.main(["-s", "test_fixture7.py"])
scope="class"
fixture为class级别的时候,如果一个class里面有多个用例,都调用了此fixture,那么此fixture只在该class里所有用例开始前执行一次
import pytest
@pytest.fixture(scope="class")
def first():
print("\n获取用户名,scope为class级别只运行一次")
a = "yoyo"
return a
class TestCase():
def test_1(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo"
def test_2(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo"
if __name__ == "__main__":
pytest.main(["-s", "test_fixture9.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 2 items
test_fixture9.py
获取用户名,scope为class级别只运行一次
测试账号:yoyo
.测试账号:yoyo
.
========================== 2 passed in 0.13 seconds ===========================
scope="module"
fixture为module级别时,在当前.py脚本里面所有用例开始前只执行一次
import pytest
@pytest.fixture(scope="module")
def first():
print("\n获取用户名,scope为module级别当前.py模块只运行一次")
a = "yoyo"
return a
def test_1(first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo"
class TestCase():
def test_2(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo"
if __name__ == "__main__":
pytest.main(["-s", "test_fixture10.py"])
运行结果
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 2 items
test_fixture10.py
获取用户名,scope为module级别当前.py模块只运行一次
测试账号:yoyo
.测试账号:yoyo
.
========================== 2 passed in 0.14 seconds ===========================
scope="session"
fixture为session级别是可以跨.py模块调用的,也就是当我们有多个.py文件的用例时候,如果多个用例只需调用一次fixture,那就可以设置为scope="session",并且写到conftest.py文件里
conftest.py文件名称是固定的,pytest会自动识别该文件。放到工程的根目录下,就可以全局调用了,如果放到某个package包下,那就只在该package内有效
conftest.py
import pytest
@pytest.fixture(scope="session")
def first():
print("\n获取用户名,scope为session级别多个.py模块只运行一次")
a = "yoyo"
return a
test_fixture11.py和test_fixture12.py用例脚本
# test_fixture11.py
import pytest
def test_1(first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo"
if __name__ == "__main__":
pytest.main(["-s", "test_fixture11.py"])
# test_fixture12.py
import pytest
def test_2(first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo"
if __name__ == "__main__":
pytest.main(["-s", "test_fixture12.py"])
如果想同时运行test_fixture11.py和test_fixture12.py,在cmd执行
pytest -s test_fixture11.py test_fixture12.py
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 2 items
test_fixture11.py . [ 50%]
test_fixture12.py . [100%]
========================== 2 passed in 0.03 seconds ===========================
D:\YOYO\fixt>pytest -s test_fixture11.py test_fixture12.py
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 2 items
test_fixture11.py
获取用户名,scope为session级别多个.py模块只运行一次
测试账号:yoyo
.
test_fixture12.py 测试账号:yoyo
.
========================== 2 passed in 0.03 seconds ===========================
---------------------------------pytest结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以关注下我的个人公众号:yoyoketang

pytest文档24-fixture的作用范围(scope)的更多相关文章
- pytest文档7-pytest-html生成html报告
前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- pytest文档51-内置fixture之cache使用
前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...
- pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)
前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...
- pytest文档43-元数据使用(pytest-metadata)
前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...
- pytest文档19-doctest测试框架
前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...
- pytest文档1-环境准备与入门
前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...
- pytest文档56-插件打包上传到 pypi 库
前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...
- pytest文档55-plugins插件开发
前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...
随机推荐
- xss攻击原理与解决方法
概述 XSS攻击是Web攻击中最常见的攻击方法之一,它是通过对网页注入可执行代码且成功地被浏览器 执行,达到攻击的目的,形成了一次有效XSS攻击,一旦攻击成功,它可以获取用户的联系人列 表,然后向联系 ...
- C#基础之数据类型
c#有15个预定义类型,其中13个是值类型,两个是引用类型(string 和 object) 1.整型 2.浮点类型 float数据类型用于较小的浮点数,因为它要求的精度较低. double数据类型比 ...
- Kaldi 安装
以后要重点搞caldi了,虽然集群上有,但还是本地安装一下吧. 参考 Kaldi 学习手记(一):Kaldi 的编译安装 在 ubuntu 下安装 kaldi 基本步骤 两个文章基本差不多 1 ...
- 设置Loadrunner负载机临时文件目录
设置Loadrunner负载机临时文件目录 最近在跑稳定性测试 3 X 24小时的时候,发现负载机产生的日志还运行记录等等竟然有100多G! C盘空间不足,但是D盘还有700多G空间呢,怎么让临时文件 ...
- NET生成缩略图
1.添加一个html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...
- 收集Nginx的json格式日志(五)
一.配置nginx [root@linux-node1 ~]# vim /etc/nginx/nginx.conf #修改日志格式为json格式,并创建一个nginxweb的网站目录 log_form ...
- Mac下思维导图Xmind使用入门
1.软件下载 中文官网地址: http://www.xmindchina.net   安装过程比较傻瓜化,这里就不截图了. 2.用Xmind设计软件模块: 1>.新建一个思维导图,如下图,选 ...
- 部署 LAMP
部署 LAMP https://help.aliyun.com/document_detail/50774.html?spm=a2c4g.11186623.6.773.Em8xVc 文档提供方:上海驻 ...
- Eclipse daemon not running. starting it now on port ***的
daemon not running. starting it now on port ***的 1) 运行 cmd,进入命令行2) 输入 netstat -ano ,找出占用端口***(port * ...
- JAVA单向链表实现
JAVA单向链表实现 单向链表 链表和数组一样是一种最常用的线性数据结构,两者各有优缺点.数组我们知道是在内存上的一块连续的空间构成,所以其元素访问可以通过下标进行,随机访问速度很快,但数组也有其缺点 ...