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。

相关推荐
Karoku0662 小时前
【CI/CD】CI/CD环境搭建流程和持续集成环境配置
运维·ci/cd·docker·容器·kubernetes·prometheus
勤奋的凯尔森同学5 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
丁卯4045 小时前
Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用
服务器·后端·golang
chengooooooo6 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
人间打气筒(Ada)7 小时前
MySQL主从架构
服务器·数据库·mysql
落笔画忧愁e8 小时前
FastGPT快速将消息发送至飞书
服务器·数据库·飞书
小冷爱学习!8 小时前
华为动态路由-OSPF-完全末梢区域
服务器·网络·华为
技术小齐9 小时前
网络运维学习笔记 016网工初级(HCIA-Datacom与CCNA-EI)PPP点对点协议和PPPoE以太网上的点对点协议(此处只讲华为)
运维·网络·学习
ITPUB-微风9 小时前
Service Mesh在爱奇艺的落地实践:架构、运维与扩展
运维·架构·service_mesh
落幕9 小时前
C语言-进程
linux·运维·服务器