零基础小白(五)下篇-Pytest框架接口实战
2021-01-17 本文已影响0人
巴鶴
零基础小白 接口自动化测试集锦: https://www.jianshu.com/nb/49125734
第5步: 重构测试数据
步骤1: 使用Yaml维护数据,新增testlogin.yml文件
#登录测试用例
---
#测试用例名称
"case_name": "登录成功用例"
#url地址
"url": "/member/api/mi/login"
#data
"data":
uId: "202012xxxxxx1945"
nickName: "1164xxxxxx0"
session: "NOjvQ0vscRTUobHQ"
#headers
"headers":
App-Channel: "200xxxxxx05"
Host: "xxxxxx.com"
App-GameId: "8xxxxxx05"
App-DeviceNum: "95915b3b-befd-35ed-afb6-xxxxxx7092d0b82"
#期望结果
"expect": '"isFrozen": false, "type": "1"'
---
#测试用例名称
"case_name": "登录失败用例"
#url地址
"url": "/member/api/mi/login"
#data
"data":
uId: "2021121xxxxxx241945"
nickName: "1164208866"
session: "NOjvQ0vscRTUobHQ"
#headers
"headers":
App-Channel: "200xxxxxx5"
Host: "sdk-apxxxxxx.com"
App-GameId: "80005"
App-DeviceNum: "9xxxxxx5b3b-befd-35ed-afb6-23d7092d0b82"
#期望结果
"expect": '"isFrozen": false, "type": "1"'
步骤2: 重构Conf.py代码,新增读取testlogin.yml测试数据
# -*- coding: utf-8 -*-
# @Time : 2021/1/11 16:16
# @File : Conf.py.py
# @Author : Yvon_₯㎕ζ๓
import os,json
from utils.YamlUtil import YamlReader
#获取项目基本目录
current = os.path.abspath(__file__)
# print(current)
#获取当前项目的绝对路径
BASE_DIR = os.path.dirname(os.path.dirname(current))
# print(BASE_DIR)
#定义config目录路径
_config_path = BASE_DIR + os.sep + "config"
# print(_config_path)
#定义conf.yml文件路径
_confyml_file = _config_path + os.sep + "conf.yml"
# print(_confyml_file)
# 定义logs文件路径
_log_path = BASE_DIR + os.sep + "logs"
# print(_log_path)
#定义data目录路径
_data_path = BASE_DIR + os.sep + "data"
# print(_data_path)
#定义testlogin.yml文件路径
_testlogin_config_file = _data_path + os.sep + "testlogin.yml"
# print(_testlogin_config_file)
#***************************************定义方法**************************************
def get_config_path():
"""
获取config文件夹目录
:return:
"""
return _config_path
def get_confyml_file():
'''
获取conf.yml文件路径目录
:return:
'''
return _confyml_file
def get_log_path():
"""
获取log文件路径
:return:
"""
return _log_path
def get_data_path():
"""
获取data文件夹目录
:return:
"""
return _data_path
def get_testlogin_config_file():
"""
获取登录配置文件
:return:
"""
return _testlogin_config_file
#读取配置文件,创建类
class ConfigYaml:
def __init__(self):
# 初始化读取yaml配置文件
self.config = YamlReader(get_confyml_file()).data()
# 初始化读取testlogin yaml配置文件
self.testlogin_config = YamlReader(get_testlogin_config_file()).data_all()
# 定义方法获取重要信息
def get_conf_url(self):
'''
获取confyml配置文件中url地址
:return:
'''
return self.config["BASE"]["test"]["url"]
def get_conf_log(self):
"""
获取日志级别
:return:
"""
return self.config["BASE"]["log_level"]
def get_conf_log_extension(self):
"""
获取文件扩展名
:return:
"""
return self.config["BASE"]["log_extension"]
def get_testlogin_conf_info(self):
"""
返回testlogin yaml文档所有内容
:return:
"""
return self.testlogin_config
if __name__ == "__main__":
conf_read = ConfigYaml()
# print(conf_read.get_conf_log_extension())
r = conf_read.get_testlogin_conf_info()
print(json.dumps(r, sort_keys=True, ensure_ascii=False, indent=4, separators=(', ', ': '))) # Json格式打印
# print(conf_read.get_testlogin_conf_info())



第6步: Pytest登录接口实战
登录接口代码参考
# -*- coding: utf-8 -*-
# @Time : 2021/1/13 15:20
# @File : 004 tets_login.py
# @Author : Yvon_₯㎕ζ๓
from config.Conf import ConfigYaml
import pytest,json
from utils.RequestsUtil import Request
#1、获取测试用例内容list
#获取testlogin.yml文件路径
data_list = ConfigYaml().get_testlogin_conf_info()
# print(data_list)
'''参数化执行测试用例'''
@pytest.mark.parametrize("login",data_list) # login 是定义的变量
def test_login_pytest(login):
# 初始化url,data
url = ConfigYaml().get_conf_url() + login["url"]
# print("url %s"%url)
data = login["data"]
headers = login["headers"]
# 初始化requests请求
request = Request()
res = request.post(url,headers=headers,json = data)
print(json.dumps(res, sort_keys=True, ensure_ascii=False, indent=4, separators=(', ', ': '))) # Json格式打印
if __name__ == "__main__":
pytest.main(["-s","004 tets_login.py"])
运行登录接口脚本
