28、pytest实战:获取多用户鉴权

前提

测试过程中有用户体系,例如包括管理员、商家、用户角色,不同测试用例需要使用不同角色来操作,操作权限根据用户的鉴权来判断实现。

技能点

  • 建立全局变量文件,保存账号相关信息
  • 获取鉴权信息变为module级别fixture,同一module级别只请求一次鉴权

代码实现

shell 复制代码
# 目录
└─monitor_token
    │  __init__.py
    │
    ├─case
    │  │  __init__.py
    │  │
    │  └─monitor_token
    │          conftest.py
    │          test_get_token.py
    │          __init__.py
    │
    └─data
            userinfo.py

init.py全是空文件,无内容

python 复制代码
# content of conftest.py
import pytest
from monitor_token.data.userinfo import project_info

@pytest.fixture(scope='package')
def get_token():
    token_dict = {}
    get_token_url = 'http://192.168.1.40/api/cityos/sso/web/v2/user/login'
    headers = {
		'Content-Type':'application/json',
	}
    user_info = project_info['test']['user_info']
    for key, value in user_info.items():
        body = {
            'username': value[0],
            'password': value[1],
            'tenantId':'1'
        }
        # req = requests.post(url, body, headers=headers)
        token_dict[key] = 'Bearer-' + str(value[0]) + "-" + str(value[1])
    return  token_dict
python 复制代码
# content of test_get_token.py
import pytest

class TestGetToken:
    def test_get_manager_token(self, get_token):
        token = get_token['管理员']
        print('管理员token:'+token)
        assert 1
        
    def test_get_saler_token(self, get_token):
        token = get_token['商家']
        print('商家token:' + token)
        assert 1
    def test_get_cus_token(self, get_token):
        token = get_token['顾客']
        print("顾客token:" + token)
        assert 1
python 复制代码
# content of userinfo.py
import time
import sys

project_info = {
	'test':{
		'app_base_url':'http://192.168.1.50',
		'user_info':{
			'管理员':('manageuser','testpassword'),
            '商家':('saleruser','salerpassword'),
            '顾客':('cususer','cususerpassword'),
		},
		'database_info':{
			'url':'',
			'username':'',
			'password':'',
			'port':3306,
			'db_name':{
				'测试创新应用':'',
			}
		}
	},
	'prod':{
		'app_base_url':'http://192.168.1.40',
		'user_info':{
			'管理员':('testuser','testpassword'),
            '商家':('saleruser','salerpassword'),
            '顾客':('cususer','cususerpassword'),
		},
		'database_info':{
			'url':'',
			'username':'',
			'password':'',
			'port':3306,
			'db_name':{
				'生产创新应用':'',
			}
		}
	}
}

场景应用

用例文件夹下的conftest.py中实现获取token的fixture,测试用例中直接获取该fixture,并根据用户名称来使用。fixtrue设置作用域为包级,只能包内测试用例执行结束后,才会销毁,可以保证一次测试,只请求了一次token。

相关推荐
402 Payment Required5 分钟前
serv00 ssh登录保活脚本-邮件通知版
运维·chrome·ssh
小柏ぁ10 分钟前
calico/node is not ready: BIRD is not ready: BGP not established with xxx
运维·docker·kubernetes
Mintimate31 分钟前
云服务器 Linux 手动 DD 安装第三方 Linux 发行版:原理与实战
linux·运维·服务器
RussellFans42 分钟前
Linux 环境配置
linux·运维·服务器
网硕互联的小客服1 小时前
503 Service Unavailable:服务器暂时无法处理请求,可能是超载或维护中如何处理?
服务器·git·github
高冷的肌肉码喽1 小时前
Linux-进程间的通信
linux·运维·服务器
乖乖是干饭王2 小时前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu
jekc8682 小时前
禅道18.2集成LDAP
linux·运维·服务器
weixin_307779132 小时前
Linux下GCC和C++实现统计Clickhouse数据仓库指定表中各字段的空值、空字符串或零值比例
linux·运维·c++·数据仓库·clickhouse
Tender_光3 小时前
iptables实验
运维·服务器