接口自动化-pytest中的fixture - scope               

介绍

fixture文章中介绍的比较少,同学们可以去搜索下fixture的详解或者去看看源码

在这之前博主都是用的unittest单元测试框架去写的接口自动化,感觉也挺好用,但是得知pytest的fixture以及allure后,则出现了真香警告!!

先说fixture源码中包含了几大核心,我摘出了源码中的一部分

def fixture(
fixture_function: Optional[_FixtureFunction] = None,
*,
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
params: Optional[Iterable[object]] = None,
autouse: bool = False,
ids: Optional[
Union[
Iterable[Union[None, str, float, int, bool]],
Callable[[Any], Optional[object]],
]
] = None,
name: Optional[str] = None,
)

1、scope  2、params   3、autouse    4、ids

本文章对第一条 scope进行详细解释,因过于详细,非精简版内容,适合小白进行观看

scope:是控制fixture的作用范围

scope中包含了4个功能

1、function

每一个函数都会调用,使用方式:在fixture函数上面增加@pytest.fixture()  括号中不用加function,不传的话默认为function

2、class

每一个类调用一次,每个类下的方法是funtion概念  使用方式:在fixture函数当面增加@pytest.fixture('class')

3、module

每一个py文件调用一次,里面的方法和类,是class和function概念 使用方式:在fixture函数当面增加@pytest.fixture('module')

4、session

多个py文件调用一次,每个py文件都是module的概念 使用方式:在fixture函数当面增加@pytest.fixture('session')

详解

函数调用fixture函数的时候,是前置的

一、function

下列代码中,可以看到,我们设置了个fixture-function函数,然后pytest中每个函数都可以调用fixture-function

import pytest
@pytest.fixture()
def getsql_project(): #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
project_id = 335
project_id2 = 332
print('验证是否前置的执行')
return project_id,project_id2 def test_set_project1(getsql_project): #我们要用fixture函数的时候,直接在括号中调用就好了
print('第一个id是',getsql_project[0]) def test_set_project2(getsql_project):
print('第二个id是', getsql_project[1]) if __name__ == '__main__':
pytest.main(["-s","test_fixture.py"])

返回结果如下

============================= test session starts =============================
collecting ... collected 2 items test_fixture_scope.py::test_set_project1 验证是否前置的执行
PASSED [ 50%]第一个id是 335 test_fixture_scope.py::test_set_project2 验证是否前置的执行
PASSED [100%]第二个id是 332 ============================== 2 passed in 0.08s ============================== Process finished with exit code 0

可以看到,每个方法都调用了一次fixture函数

二、class

一个类下只会触发一次

import pytest
@pytest.fixture(scope='class')
def getsql_project(): #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
project_id = 335
project_id2 = 332
print('验证是否前置的执行')
return project_id,project_id2 class Test_fixture: def test_set_project1(self,getsql_project): #我们要用fixture函数的时候,直接在括号中调用就好了
print('第一个id是',getsql_project[0]) def test_set_project2(self,getsql_project):
print('第二个id是', getsql_project[1]) if __name__ == '__main__':
pytest.main(["-s","test_fixture.py"])

返回结果

============================= test session starts =============================
collecting ... collected 2 items test_fixture_scope.py::Test_fixture::test_set_project1
test_fixture_scope.py::Test_fixture::test_set_project2 ============================== 2 passed in 0.09s ============================== Process finished with exit code 0
验证是否前置的执行
PASSED [ 50%]第一个id是 335
PASSED [100%]第二个id是 332

可以看到,验证是否前置的执行,只被执行了一次,意味着我们2个方法都调用了fixture函数,实际只被执行了一次

三、module

一个py文件下只会执行一次

我们首先看下,我们有多个类,每个类都调用下fixture函数

import pytest
@pytest.fixture(scope='module')
def getsql_project(): #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
project_id = 335
project_id2 = 332
print('验证是否前置的执行')
return project_id,project_id2 class Test_fixture:
def test_set_project1(self,getsql_project): #我们要用fixture函数的时候,直接在括号中调用就好了
print('第一个类id1',getsql_project[0])
def test_set_project2(self,getsql_project):
print('第一个类id2', getsql_project[1])
if __name__ == '__main__':
pytest.main(["-s","test_fixture.py"]) class Test_fixture2:
def test_set_project3(self,getsql_project): #我们要用fixture函数的时候,直接在括号中调用就好了
print('第二个类id1',getsql_project[0])
def test_set_project4(self,getsql_project):
print('第二个类id2', getsql_project[1])
if __name__ == '__main__':
pytest.main(["-s","test_fixture.py"])

返回结果(可以看出,验证是否前置执行  被执行了2次,因为我们定义的class,每个类被执行一次所以执行了2次)

============================= test session starts =============================
collecting ... collected 4 items test_fixture_scope.py::Test_fixture::test_set_project1
test_fixture_scope.py::Test_fixture::test_set_project2
test_fixture_scope.py::Test_fixture2::test_set_project3 验证是否前置的执行
PASSED [ 25%]第一个类id1 335
PASSED [ 50%]第一个类id2 332 test_fixture_scope.py::Test_fixture2::test_set_project4 ============================== 4 passed in 0.09s ============================== Process finished with exit code 0
验证是否前置的执行
PASSED [ 75%]第二个类id1 335
PASSED [100%]第二个类id2 332

重点来了

此时我们将class换成module

@pytest.fixture(scope='module')

返回结果(在整个py文件中,不论多个类调用,只被运行了一次)

============================= test session starts =============================
collecting ... collected 4 items test_fixture_scope.py::Test_fixture::test_set_project1
test_fixture_scope.py::Test_fixture::test_set_project2
test_fixture_scope.py::Test_fixture2::test_set_project3 验证是否前置的执行
PASSED [ 25%]第一个类id1 335
PASSED [ 50%]第一个类id2 332 test_fixture_scope.py::Test_fixture2::test_set_project4 ============================== 4 passed in 0.08s ============================== Process finished with exit code 0
PASSED [ 75%]第二个类id1 335
PASSED [100%]第二个类id2 332

四、session

一般我们这种fixture都卸载目录下的conftest.py文件下,如果有2个py文件都调用了conftest.py下的fixture函数,如果fixture是session形式,多个py可以用这一个函数返回的数据,但是不会重复调用。

新建conftest.py 文件,里面去放入我们的fixture函数

import pytest
@pytest.fixture(scope='module')
def getsql_project(): #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
project_id = 335
project_id2 = 332
print('验证是否前置的执行')
return project_id,project_id2

然后多个py都同时调用getsql_project  实际只被调用一次

test_fixture_scope.py::Test_fixture::test_set_project1
test_fixture_scope.py::Test_fixture::test_set_project2
test_fixture_scope.py::Test_fixture2::test_set_project3 验证是否前置的执行
PASSED [ 25%]第一个类id1 335
PASSED [ 50%]第一个类id2 332 test_fixture_scope.py::Test_fixture2::test_set_project4 ============================== 4 passed in 0.08s ============================== Process finished with exit code 0
PASSED [ 75%]第二个类id1 335
PASSED [100%]第二个类id2 332

接口自动化 - pytest-fixture -scope作用范围的更多相关文章

  1. pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能

    conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("&qu ...

  2. requests接口自动化-pytest框架

    pytest框架规则 测试文件以test_开头或者以_test结尾 测试类以Test开头,并且不能带有init方法 测试函数以test_开头 断言使用assert pytest框架运行用例 运行单个文 ...

  3. pytest:通过scope控制fixture的作用范围

    一.fixture里面有个参数scope,通过scope可以控制fixture的作用范围,根据作用范围大小划分:session>module>class>function,具体作用范 ...

  4. 基于Python+Requests+Pytest+YAML+Allure实现接口自动化

    本项目实现接口自动化的技术选型:Python+Requests+Pytest+YAML+Allure ,主要是针对之前开发的一个接口项目来进行学习,通过 Python+Requests 来发送和处理H ...

  5. Pytest学习(六) - conftest.py结合接口自动化的举例使用

    一.conftest.py作用 可以理解成存放fixture的配置文件 二.conftest.py配置fixture注意事项 pytest会默认读取conftest.py里面的所有fixture co ...

  6. python+pytest接口自动化(13)-token关联登录

    在PC端登录公司的后台管理系统或在手机上登录某个APP时,经常会发现登录成功后,返回参数中会包含token,它的值为一段较长的字符串,而后续去请求的请求头中都需要带上这个token作为参数,否则就提示 ...

  7. pytest fixture中scope试验,包含function、module、class、session、package

    上图是试验的目录结构 conftest.py:存放pytest fixture的文件 import uuid import pytest @pytest.fixture(scope="mod ...

  8. python接口自动化12-pytest前后置与fixture

    前言 我们都知道在自动化测试中都会用到前后置,pytest 相比 unittest 无论是前后置还是插件等都灵活了许多,还能自己用 fixture 来定义.(甩 unttest 半条街?) 首先了解一 ...

  9. 如何用tep完成增删改查接口自动化

    tep的设计理念是让人人都可以用Python写自动化,本文就来介绍如何用tep完成增删改查接口自动化. 环境变量 编辑fixtures/fixture_admin.py: "qa" ...

随机推荐

  1. Vue3的其他属性和API函数

    customRef() 自定义Ref函数实现Ref()的相关功能 1 <script> 2 import { ref customRef} from 'vue' 3 4 function ...

  2. 自己实现一个Controller——终极型

    经过前两篇的学习与实操,也大致掌握了一个k8s资源的Controller写法了,如有不熟,可回顾 自己实现一个Controller--标准型 自己实现一个Controller--精简型 但是目前也只能 ...

  3. Java之SpringBoot自定义配置与整合Druid

    Java之SpringBoot自定义配置与整合Druid SpringBoot配置文件 优先级 前面SpringBoot基础有提到,关于SpringBoot配置文件可以是properties或者是ya ...

  4. 低代码+RPA+AI,能否让ERP焕发下一春?

    从2004年开始,国内ERP项目的实施便在各大企业热火朝天地展开,2014年,国内大中型企业已经基本完成了ERP系统的普及.ERP已经在大中型企业中成为不可或缺的关键信息系统.企业核心业务的流转与管控 ...

  5. Promise源码实现与测试

    const PENDING = 'pending', FULFILLED = 'fulfilled', REJECTED = 'rejected' class MyPromise { construc ...

  6. 『Python』matplotlib划分画布的主要函数

    1. subplot() 绘制网格区域中几何形状相同的子区布局 函数签名有两种: subplot(numRows, numCols, plotNum) subplot(CRN) 都是整数,意思是将画布 ...

  7. Centos7下thinkphp5.0环境配置

    首先把yum源修改为阿里的yum源,如果没有安装wget,先安装一个.(如果有请蹦过) wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors ...

  8. 【原创】linux mint 17.3 kvm 安装windows7虚拟机

    一.安装windows7虚拟机 linux mint 17.3是一个不错的桌面发行版本,我下载了 linux mint 17.3 for xfce 桌面版本,运行速度没得说,而且安装设置都挺简单,非常 ...

  9. Python3入门系列之-----return返回值,我终于懂了

    前言 初学者学习return的用法有点蒙,不知道它的作用是什么?返回的是什么?在什么时候要用?小伙伴也可能会遇到和我同样的困扰,给大家举个例子,马上就明白了. 同一段代码,函数中带return和没有r ...

  10. HTML的一些技巧

    清除form表单 this.$refs.formName.resetFields() 验证表单 this.$refs.createForm.validate((valid) => {}) 当im ...