fixture作用范围
ixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function
fixture(scope="function", params=None, autouse=False, ids=None, name=None):
"""使用装饰器标记fixture的功能
** 作者:上海-悠悠 QQ交流群:**
可以使用此装饰器(带或不带参数)来定义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 = ""
return b def test_1(first):
'''用例传fixture'''
print("测试账号:%s" %first)
assert first == "yoyo" def test_2(sencond):
'''用例传fixture'''
print("测试密码:%s" %sencond)
assert sencond == "" if __name__ == "__main__":
pytest.main(["-s", "test_fixture7.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture7.py
获取用户名
测试账号:yoyo
.
获取密码
测试密码:
. ========================== 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 = ""
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 == "" 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., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture9.py
获取用户名,scope为class级别只运行一次
测试账号:yoyo
.测试账号:yoyo
. ========================== 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., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture10.py
获取用户名,scope为module级别当前.py模块只运行一次
测试账号:yoyo
.测试账号:yoyo
. ========================== 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., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture11.py . [ %]
test_fixture12.py . [%] ========================== passed in 0.03 seconds =========================== D:\YOYO\fixt>pytest -s test_fixture11.py test_fixture12.py
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture11.py
获取用户名,scope为session级别多个.py模块只运行一次
测试账号:yoyo
.
test_fixture12.py 测试账号:yoyo
. ========================== passed in 0.03 seconds ===========================
fixture作用范围的更多相关文章
- pytest进阶之fixture
前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...
- pytest文档24-fixture的作用范围(scope)
fixture作用范围 fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function fixture( ...
- Pytest【定制fixture】
在pytest中的fixture是在测试函数运行前后,由pytest执行的外壳函数,fixture中的代码可以定制,满足多变的测试需求:包括定义传入测试中的数据集.配置测试前系统的初始化状态.为批量测 ...
- pytest框架: fixture之conftest.py
原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown ...
- 【PYTEST】第四章Fixture
知识点: 利用fixture共享数据 conftest.py共享fixture 使用多个fixture fixture作用范围 usefixture 重命名 1. 利用fixture共享数据 test ...
- Pytest测试框架(三):pytest fixture 用法
xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...
- 『德不孤』Pytest框架 — 12、Pytest中Fixture装饰器(二)
目录 5.addfinalizer关键字 6.带返回值的Fixture 7.Fixture实现参数化 (1)params参数的使用 (2)进阶使用 8.@pytest.mark.usefixtures ...
- pytest--fixture
前戏 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...
- pytest学习纪要123-针对经常用到的内容详实记录
pytest123 本文主要参考:https://www.cnblogs.com/yoyoketang/tag/pytest 如有侵权,请站内联系我 目录 pytest123 1.setup和tear ...
随机推荐
- 清北学堂dp图论营游记day1
讲课人: 老师对dp的理解是类似于分治思想,由小状态推出大状态.不同的是分治算法没有重叠子问题. dp把子问题越划越小,从而推出了基础状态.然后是dp方程,要满足简洁性,并且充分描述能够影响最后结果的 ...
- JavaScript入门学习之一——初级语法
JavaScript是前端编辑的一种编程语言(不同于html,html是一种标记语言),所以和其他的编程语言一样,我们将会从下面几点学习 基础语法 数据类型 函数 面向对象 JavaScript的组成 ...
- springmvc其他类获取request记得web.xml
<listener> <listener-class>org.springframework.web.context.request.RequestContextListene ...
- constant read 和 current read
来自网络,并且在本机实验完成: onsistent read :我的理解,就是通过scn来读取. 读取的过程中要保证 scn是一致的.举个例子,一个SELECT 语句在SCN=100的时刻开始读取一 ...
- HDU - 6386 Age of Moyu (双端队列+bfs)
题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...
- 红帽Linux故障定位技术详解与实例(4)
红帽Linux故障定位技术详解与实例(4) 在线故障定位就是在故障发生时, 故障所处的操作系统环境仍然可以访问,故障处理人员可通过console, ssh等方式登录到操作系统上,在shell上执行 ...
- 面向对象之封装 及@property装饰器使用
目录 封装 1.封装的定义 2.封装的目的: 3.封装的三种方式 4.封装的优点 5.访问限制(封装) @property 装饰器 属性property底层实现 封装 1.封装的定义 将复杂的丑陋的, ...
- Python&R&Matlab:批量生成变量
在编写程序时,有时我们需要命名相当多的变量,比如x0.x1.x2.....xn,用手一个个打出来是相当麻烦的.那么这时我们就需要批量生成变量了. 解决这个问题的关键在于,'xn'是自动构造出来的字符串 ...
- Python 练习实例3
Python 练习实例3 题目:一个整数,它加上100后是一个完https://www.xuanhe.net/全平方数,再加上168又是一个完全平方数,请问该数是多少? 程序分析: 假设该数为 x. ...
- 仅1年GitHub Star数翻倍,Flink 做了什么?
Apache Flink 是公认的新一代开源大数据计算引擎,其流水线运行系统既可以执行批处理程序也可以执行流处理程序.目前,Flink 已成为 Apache 基金会和 GitHub 社区最为活跃的项目 ...