接口自动化-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. scrum项目冲刺_day04总结

    摘要:今日完成任务. 1.图像识别已优化 2.语音识别正在进行 3.搜索功能 正在进行 总任务: 一.appUI页面(已完成) 二.首页功能: 1.图像识别功能(已完成) 2.语音识别功能 3.垃圾搜 ...

  2. 一文让你彻底理解having和where的区别

    having子句与where都是设定条件筛选的语句,有相似之处也有区别. having与where的区别: having是在分组后对数据进行过滤 where是在分组前对数据进行过滤 having后面可 ...

  3. django如何加载外部文件

    django如何加载外部文件(环境:pycharm python2.7 django1.11) 有一份新的文件夹名为:py_aiplat_demo,内含有多个文件夹(SDK,demo,data). 1 ...

  4. django中admin一些方法

    1.概述:内容发布,负责添加,修改,删除内容及公告访问2.配置admin应用在settings文件中INSTALLED_APPS添加:'django.contrib.admin', 默认是已经添加好的 ...

  5. django 高级扩展-中间件-上传图片-分页-富文本-celery

    """ django 高级扩展 一.静态文件 1.css,js,json,图片,字体等 2.配置setting,在最底下设置静态文件目录,写入下面代码 #配置静态文件目录 ...

  6. 『GoLang』错误处理

    Go 没有像 Java 和 .NET 那样的 try/catch 异常机制:不能执行抛异常操作.但是有一套 defer-panic-and-recover 机制. Go 的设计者觉得 try/catc ...

  7. 『GoLang』控制结构

    条件语句 if 是用于测试某个条件(布尔型或逻辑型)的语句,如果该条件成立,则会执行if后由大括号括起来的代码块,否则就忽略该代码块继续执行后续的代码. if condition { // do so ...

  8. VS2013的switch case缩进问题

    原来的   更改设置   改完后    

  9. Expression Tree 遍历集合

    场景 从接口返回的数据是集合,却是 object 类型的.这个时候需要遍历这个集合.现提供两种方法. 方法一: 因为集合是可枚举的,所以可以尝试转为 IEnumerable 类型,然后遍历即可. st ...

  10. Superedge的新特性和未来之路

    作者 王冬,腾讯云高级研发工程师,专注于Kubernetes.容器等云原生领域,SuperEdge 核心开发人员,现负责腾讯云边缘容器TKE Edge私有化相关工作. 背景 2021年9月27号,,在 ...