pytest + yaml 框架 -15.fixtures 功能使用
创始人
2024-04-24 22:34:51
0

前言

前面讲到全局只登录一次,全部的yaml 用例都会公用一个请求会话。
那有些接口不需要登录怎么办呢?比如登录和注册的接口,是不需要带上登录 token 的。
我除了默认用到一个 requests_session 全局的内置 fixture,还预留了2个

  • requests_module: 每个yaml文件中用一次
  • requests_function: 每个用例中用一次

pip 安装插件

pip install pytest-yaml-yoyo

requests_module 和 requests_function 内置 fixture 功能在 v1.1.1 版本实现

全局设置请求头部

公共请求头部带上token参考这篇https://www.cnblogs.com/yoyoketang/p/16924506.html

我在pytest + yaml 框架框架中封装了一个内置 fixture 叫 requests_session, 它的作用范围是 scope=“session” ,也就是全部 session 用例会话中仅实例化一次。
现在我只需在 conftest 中写一个登录的 fixture 功能,获取 token 后添加到 requests_session 头部

import pytest
import uuid
"""
全局仅登录一次,获取token,
在请求头部添加Authentication Bearer 认证
内置fixture requests_session
"""def login():"""登录方法"""# 调用登录方法,返回tokenreturn str(uuid.uuid4())  # noqa@pytest.fixture(scope="session", autouse=True)
def login_first(requests_session):"""全局仅一次登录, 更新session请求头部"""# 调用登录方法,获得tokentoken = login()headers = {"Authentication": f"Bearer {token}"}requests_session.headers.update(headers)

接着我写2个yaml文件(注意,yaml文件中也不需要重复去添加请求头部了
test_get_demo.yml

config:name: getteststeps:
-name: getrequest:method: GETurl: http://httpbin.org/getvalidate:- eq: [status_code, 200]

test_post_demo.yml

config:name: post示例variables:username: testpassword: "123456"teststeps:
-name: postrequest:method: POSTurl: http://httpbin.org/postjson:username: ${username}password: ${password}validate:- eq: [status_code, 200]

在命令行中输入pytest运行, 于是可以看到,在2个用例中都自动带上了请求头部参数。
有小伙伴问到:请求头部需加其他固定的参数怎么办?比如versionId 和configId, 方法都是一样

    headers = {"Authentication": f"Bearer {token}","versionId": "v1.0","configId": "10086"}requests_session.headers.update(headers)

当我们设置了全局 requests_session 请求会话后, 默认所有的请求都会带上添加的头部参数

headers = {"Authentication": f"Bearer {token}","versionId": "v1.0","configId": "10086"}

requests_module 和 requests_function

那有些接口不需要登录怎么办呢?比如登录和注册的接口,是不需要带上登录token的。

除了默认用到一个requests_session 全局的内置fixture,还预留了2个

  • requests_module: 每个yaml文件中用一个请求会话(会保持cookies)
  • requests_function: 每个用例中用一次,每个用例独立运行,不保持cookies

接下来看下如何在用例中使用test_register.yml

config:name: post示例fixtures: requests_module注册1:request:method: POSTurl: http://httpbin.org/postjson:username: test123password: "123456"validate:- eq: [status_code, 200]注册2:request:method: POSTurl: http://httpbin.org/postjson:username: test444password: "123456"validate:- eq: [status_code, 200]

在config 中传入 fixtures参数,requests_module 是每个yaml文件中用一个请求会话(会保持cookies)
requests_function 作用是每个用例中用一次,每个用例独立运行,不保持cookies。

自定义 fixtures

pytest 的核心功能是学会灵活使用fixtures, 那么我们的这个插件也是可以支持在用例中调用fixtures功能的。

在conftest.py 文件中写你需要实现的fixture 功能, 设置使用范围为scope="function" 函数级别

import pytest@pytest.fixture(scope="function")
def demo_fixture():print("用例前置操作->do something .....")yieldprint("用例后置操作,do something .....")

在 yaml 文件中引用 fixture

config:name: post示例fixtures: demo_fixture注册1:request:method: POSTurl: http://httpbin.org/postjson:username: test123password: "123456"validate:- eq: [status_code, 200]注册2:request:method: POSTurl: http://httpbin.org/postjson:username: test444password: "123456"validate:- eq: [status_code, 200]

于是运行结果可以看到,每个用例前后都会执行

collected 2 items                                                                                          test_f2.yml 用例前置操作->do something .....
.用例后置操作,do something .....
用例前置操作->do something .....
用例后置操作,do something .....

如果想整个yaml 文件中仅运行一次,那么conftest.py 文件中写你需要实现的 fixture 功能, 设置使用范围为scope="module" 模块级别

import pytest@pytest.fixture(scope="module")
def demo_fixture():print("用例前置操作->do something .....")yieldprint("用例后置操作,do something .....")

于是看到运行的时候,仅在yaml 文件的全部用例中只执行一次

collected 2 items                                                                                          test_f2.yml 用例前置操作->do something .....
..用例后置操作,do something .....

多个fixtures的使用

当 yaml 中的用例需要用到多个fixtures时, 支持2种格式

格式一: 逗号隔开

config:fixtures: fixture_name1,  fixture_name2

格式二: 用 list

config:fixtures: [fixture_name1,  fixture_name2]

requests_module 和 requests_function 内置 fixture 功能在 v1.1.1 版本实现, 版本太低的请及时更新版本。

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...