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。

相关推荐
yeflx14 小时前
Ubuntu常用指令
linux·运维·ubuntu
秦渝兴14 小时前
Ubuntu 电脑进不去桌面?从 TTY 到图形界面的完整排障指南
linux·运维·ubuntu
金融RPA机器人丨实在智能14 小时前
物流行业选自动化方案,如何评估与现有系统的集成难度?深度解析2026集成避坑指南
大数据·运维·人工智能·自动化
Mortalbreeze14 小时前
进程间通信 ---- 基于管道来实现
linux·服务器
Bert.Cai15 小时前
Linux sort命令详解
linux·运维·服务器
开开心心就好15 小时前
免费无广告的批量卸载与系统清理工具
linux·服务器·网络·智能手机·rabbitmq·excel·memcached
倔强的石头10615 小时前
SenseNova-U1 实战体验:从网页版生成,到 Mac 踩坑,再到 CUDA 服务器跑通本地部署
运维·服务器·macos
l167751685415 小时前
天翼云服务器失联排查完整报告_事件报告
运维·服务器·云原生·云计算
wanhengidc15 小时前
高防服务器中的数据安全
运维·服务器·网络
艾莉丝努力练剑15 小时前
【Linux网络】Linux 网络编程:HTTP(五)HTTP收尾,从Cookie会话保持、抓包问题到 HTTPS 初识
linux·运维·服务器·网络·c++