python - 接口自动化测试 - TestRecharge - 充值接口测试用例

# -*- coding:utf-8 -*- '''
@project: ApiAutoTest
@author: Jimmy
@file: test_recharge.py
@ide: PyCharm Community Edition
@time: 2018-12-27 10:07
@blog: https://www.cnblogs.com/gotesting/ '''
import unittest
import os
from ddt import ddt,data
from Common.http_request import HttpRequest
from Common.read_excel import ReadExcel
from Common.read_config import ReadConfig
from Common.contants import *
from Common.basic_data import DoRegex,Context
from Common.get_logger import GetLog
import json # 读取配置文件,获取当前URL前缀,用于灵活更换测试服务器地址
read_config = ReadConfig()
url_pre = read_config.get_config_str('api','url_pre') # 读取excel,获取login测试数据
data_dir = os.path.join(data_dir,'test_data.xlsx')
read_excel = ReadExcel(data_dir)
recharge_cases = read_excel.get_cases('recharge') get_log = GetLog() @ddt
class TestRecharge(unittest.TestCase): def setUp(self):
print('Test Start') def tearDown(self):
print('Test End') @data(*recharge_cases)
def test_recharge(self,case):
url = url_pre + case.url # 通过正则表达式 查找/替换,将excel读出来的${normal_user}及${normal_pwd}替换为配置文件中读取出的normal_user及normal_pwd的值
data = DoRegex.replace(case.data)
# 将替换后的字符串转换成字典
data = json.loads(data) # 判断Context中是否存在登录成功的cookies,若存在,则获取cookie,用于接口请求;若不存在,赋值为None
if hasattr(Context,'cookies'):
cookies = getattr(Context,'cookies')
else:
cookies = None # 记录当前测试case信息
get_log.log_info('''Test Case Info:
case_id : {0}
title : {1}
method : {2}
url : {3}
data : {4}
expected: {5}
'''.format(case.case_id,case.title,case.method,url,data,case.expected)) response = HttpRequest(method=case.method,url=url,data=data,cookies=cookies)
# 判断接口请求响应是否存在cookies,若存在,放入Context中
if response.get_cookies():
setattr(Context,'cookies',response.get_cookies())
actual = response.get_json()['msg'] # 记录当前测试case接口响应信息
get_log.log_info('''Test Case Request Response Result:
response : {0}
actual : {1}
'''.format(response.get_json(),actual)) try:
self.assertEquals(case.expected,actual)
read_excel.write_result('recharge',case.case_id,actual,'Pass')
get_log.log_info('Test Result is Passed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
except Exception as e:
read_excel.write_result('recharge',case.case_id,actual,'Fail')
get_log.log_info('Test Result is Failed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
get_log.log_error('Error msg :{0}'.format(e))
raise e
python - 接口自动化测试 - TestRecharge - 充值接口测试用例的更多相关文章
- 使用python进行接口自动化测试,批量执行测试用例
工作中,使用python的requests库进行接口自动化测试是一个比较不错的选择,今天就以某网站的免费接口为例,展示以get请求进行批量执行测试用例.话不多说直接开讲 分析一下接口信息, 请求地址: ...
- python - 接口自动化测试 - TestLogin - 登录接口测试用例
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...
- python - 接口自动化测试 - TestRegister - 注册接口测试用例
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_register.py @ide: PyChar ...
- python web自动化测试框架搭建(功能&接口)——测试用例执行和结果收集
由于unittest框架中结果收集在不同文件中,所以此处重写结果收集方法,加入执行时间,失败信息,失败截图等 TestRunner.py # coding=utf-8 import sys impor ...
- python web自动化测试框架搭建(功能&接口)——接口用例实现
测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...
- python web自动化测试框架搭建(功能&接口)——接口公共方法
接口公共方法有:数据引擎.http引擎.Excel引擎 1.数据引擎:获取用例.结果检查.结果统计 # -*- coding:utf-8 -*- from XlsEngine import XlsEn ...
- python web自动化测试框架搭建(功能&接口)——接口测试模块
Python接口测试采用python读取excel的方法,通过requests库发送请求和接收响应.模块有: Data:用于存放excel用例的,用例格式: iutil: 接口公共方法,数据引擎.ht ...
- python web自动化测试框架搭建(功能&接口)——功能测试模块
功能测试使用selenium,模块有: 1.futil: 公共方法,如元素高亮显示 # coding=utf-8 """高亮显示元素""" ...
- python web自动化测试框架搭建(功能&接口)——通用模块
1.通用模块: config.conf: 公共配置文件,配置报告.日志.截图路径,以及邮件相关配置 [report] reportpath = E:\workspace\WebAutomation\s ...
随机推荐
- php-7.1.11编译选项配置
./configure \ --prefix=/usr/local/php-7.1.11 \ --with-config-file-path=/usr/local/php7.1.11/etc \ -- ...
- shell实现mysql数据库备份
#!/bin/bash DB_USER="root" #数据库用户名 DB_PASS="12345678" #数据库密码 BACK_DIR="/bac ...
- ABAP Netweaver, Hybris Commerce和SAP 云平台的登录认证
ABAP Netweaver 在事务码SICF里选择一个服务,在明细页面对Procedure字段点击F1,查看Logon Procedure的帮助文档. 通过这个链接打开对应的帮助文档,可以看到下列七 ...
- [转载]AngularJS入门教程02:AngularJS模板
是时候给这些网页来点动态特性了——用AngularJS!我们这里为后面要加入的控制器添加了一个测试. 一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模 ...
- JoinQuant策略代码示例
总体回测前 ''' ================================================================================ 总体回测前 === ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 Apache 服务
想让我们的阿里云服务器成为一台 Web 服务器,我们需要安装一个 Web 服务器软件,比如 Apache ,或者 Nginx 等等.下面我们就一起来安装一个 Apache 服务. 我们可以使用 yum ...
- 操作系统(1)_操作系统结构_李善平ppt
cpu和内存之间通过地址总线.数据总线.控制总线连接.外部总线连接外部设备.下图有问题,内存和外设没有直接连接.同一组总线,CPU和内存连接的时候硬盘就不能和内存连接,否则有冲突,core和core之 ...
- MySql学习笔记04
复习 day01 数据库相关: create database db4 character set utf8; show databases; show create database db4; dr ...
- mysql 报错 Operand should contain 1 column(s)
报错 Operand should contain 1 column(s) 原因 select 后面加了 () select (x,x,x)
- 二十五、MySQL 索引
MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索 ...