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。

相关推荐
HIT_Weston14 小时前
31、【Ubuntu】【远程开发】内网穿透:反向隧道建立(三)
linux·运维·ubuntu
skywalk816315 小时前
在FreeBSD 14.3上部署轻量级Linux jail环境 仅仅占用10M内存
linux·运维·服务器·虚拟机·轻量化·freebsd·jail
敏姐的后花园16 小时前
模考倒计时网页版
java·服务器·前端
keep__go17 小时前
spark 单机安装
大数据·运维·分布式·spark
HIT_Weston19 小时前
27、【Ubuntu】【远程开发】内网穿透:CA 签名
linux·运维·ubuntu
猫小呆20 小时前
Weaviate服务器部署笔记
服务器·weaviate
M1582276905520 小时前
工业互联利器!EtherNet/IP 转 ModbusTCP 网关,让跨协议通信零门槛
服务器·网络·tcp/ip
阿巴~阿巴~20 小时前
基于UDP协议的英汉翻译服务系统:从网络通信到字典查询的完整机制
linux·服务器·网络·网络协议·udp协议·套接字绑定·英汉翻译服务系统
阿巴~阿巴~20 小时前
简易回声服务器实现与网络测试指南
linux·服务器·网络·udp协议·网络测试·udp套接字编程
凡间客1 天前
Ansible安装与入门
linux·运维·ansible