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)的更多相关文章

  1. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  2. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  3. pytest文档51-内置fixture之cache使用

    前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...

  4. pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)

    前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...

  5. pytest文档43-元数据使用(pytest-metadata)

    前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...

  6. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  7. pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  8. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

  9. pytest文档55-plugins插件开发

    前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...

随机推荐

  1. Windows 10利用自带的 Hyper-v 安装Linux

    Linux由于其众多独特的优势(可参见Linux系统的优势),而被很多人所喜爱.而要使用Linux那首先要做的工作就是安装Linux系统了.这里给出在 win10 下利用虚拟机 Hyper-v 安装 ...

  2. 一步一步学习IdentityServer3 (15) 授权模式那些事

    总结一句话,其实很简单 在什么Clients 拿的认证权限Scope 就去 去开什么Scope限制的服务接口门 在写Clients的时候,会有Scope,看下面的代码 new Client { Cli ...

  3. 为django的python manage.py加自定义命令

    计划在开发软件的过程中, 每次可以自己加入测试数据,这样就可以每次作全新的测试了. 将这个初始化django modules数据命令,将在manage.py里是最合适的. 下面我们就来实现吧. 参考文 ...

  4. Asp.net Vnext 实现IView

    概述 Iview定义很简单,就是根据View上下文和TextWriter对象实现对View的呈现. 实现 实现IViewEngine public class TestViewEngine : IVi ...

  5. 【LOJ】#2537. 「PKUWC2018」Minimax

    题解 加法没写取模然后gg了QwQ,de了半天 思想还是比较自然的,线段树合并的维护方法我是真的很少写,然后没想到 很显然,我们有个很愉快的想法是,对于每个节点枚举它所有的叶子节点,对于一个叶子节点的 ...

  6. PyQt5调入数据库数据在表格中显示

    数据库为Postgresql import sys from form import Ui_Form from PyQt5.Qt import QWidget, QApplication,QTable ...

  7. log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).

    一.异常描述: log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLo ...

  8. Asp.Net Core2.0 WebAPI 使用Swagger生成漂亮的接口文档

    1.引用NuGet: Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerGen 或 <PackageReference I ...

  9. Python 爬虫个人记录(一)豆瓣电影250

    一.爬虫环境 Python3.6 scrapy1.4 火狐浏览器 qq浏览器 二.scrapy shell 测试并获取 xpath 1.进入scrapy shell 2 .获取html fetch(' ...

  10. C#中的特性 (Attribute) 入门 (二)

    C#中的特性 (Attribute) 入门 (二) 接下来我们要自己定义我们自己的特性,通过我们自己定义的特性来描述我们的代码. 自定义特性 所有的自定义特性都应该继承或者间接的继承自Attribut ...