前面讲到全局只登录一次,全部的yaml 用例都会公用一个请求会话。
那有些接口不需要登录怎么办呢?比如登录和注册的接口,是不需要带上登录 token 的。
我除了默认用到一个 requests_session 全局的内置 fixture,还预留了2个
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"}
那有些接口不需要登录怎么办呢?比如登录和注册的接口,是不需要带上登录token的。
除了默认用到一个requests_session 全局的内置fixture,还预留了2个
接下来看下如何在用例中使用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。
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 .....
当 yaml 中的用例需要用到多个fixtures时, 支持2种格式
格式一: 逗号隔开
config:fixtures: fixture_name1, fixture_name2
格式二: 用 list
config:fixtures: [fixture_name1, fixture_name2]
requests_module 和 requests_function 内置 fixture 功能在 v1.1.1 版本实现, 版本太低的请及时更新版本。
上一篇:一文搞懂百万富翁问题