# -*- 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 - 充值接口测试用例的更多相关文章

  1. 使用python进行接口自动化测试,批量执行测试用例

    工作中,使用python的requests库进行接口自动化测试是一个比较不错的选择,今天就以某网站的免费接口为例,展示以get请求进行批量执行测试用例.话不多说直接开讲 分析一下接口信息, 请求地址: ...

  2. python - 接口自动化测试 - TestLogin - 登录接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...

  3. python - 接口自动化测试 - TestRegister - 注册接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_register.py @ide: PyChar ...

  4. python web自动化测试框架搭建(功能&接口)——测试用例执行和结果收集

    由于unittest框架中结果收集在不同文件中,所以此处重写结果收集方法,加入执行时间,失败信息,失败截图等 TestRunner.py # coding=utf-8 import sys impor ...

  5. python web自动化测试框架搭建(功能&接口)——接口用例实现

    测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...

  6. python web自动化测试框架搭建(功能&接口)——接口公共方法

    接口公共方法有:数据引擎.http引擎.Excel引擎 1.数据引擎:获取用例.结果检查.结果统计 # -*- coding:utf-8 -*- from XlsEngine import XlsEn ...

  7. python web自动化测试框架搭建(功能&接口)——接口测试模块

    Python接口测试采用python读取excel的方法,通过requests库发送请求和接收响应.模块有: Data:用于存放excel用例的,用例格式: iutil: 接口公共方法,数据引擎.ht ...

  8. python web自动化测试框架搭建(功能&接口)——功能测试模块

    功能测试使用selenium,模块有: 1.futil: 公共方法,如元素高亮显示 # coding=utf-8 """高亮显示元素""" ...

  9. python web自动化测试框架搭建(功能&接口)——通用模块

    1.通用模块: config.conf: 公共配置文件,配置报告.日志.截图路径,以及邮件相关配置 [report] reportpath = E:\workspace\WebAutomation\s ...

随机推荐

  1. Nodejs入门边读边想边记(-)

    Node入门>>一本全面的Node.js教程网站地址:http://www.nodebeginner.org/index-zh-cn.html 本文记录我在阅读上面这个网站的过程中得到的一 ...

  2. 编程之美2015 资格赛 hihocoder 题目2: 回文字符序列

    思路:暴力搜,用BFS的方式,生成每一种可能,再对每一种可能进行判断是否回文,进行统计.严重超时!计算一个25个字符的,大概要20多秒! #include <iostream> #incl ...

  3. python资料汇总

    http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html

  4. PPII打不开 更改I.bat

    http://jingyan.baidu.com/article/3a2f7c2e7d277126afd6118d.html

  5. hdu-3371 Connect the Cities---kruskal

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目大意: 给n个城市,m条路,k组已知路,求最小费用联通所有城市: 解题思路: kruska ...

  6. iOS 集成支付宝过程中 我遇到的一些坑,请大家注意啦(ALI69错误,ALI64错误)

    支付宝很早一段时间就集成了,之前由于一直忙于开发就没有总结,今天整理桌面的时候看到,当时做支付时候的一些散落的笔记,就稍微整理一下,给大家分享一下. 第一:当时调用支付宝的时候,总是调不起来,进过断点 ...

  7. 浏览器 DNS缓存与DNS prefetch (DNS预解析)

    浏览器 DNS缓存 浏览器DNS缓存的时间跟DNS服务器返回的TTL值无关. 注:TTL(Time-To-Live),就是一条域名解析记录在DNS服务器中的存留时间. 浏览器在获取网站域名的实际IP地 ...

  8. SpringBoot学习记录(一)

    1. Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.1. @Configuration 和 @Bean Spring的Java配置方式是 ...

  9. detection in video and image

    video中的detection,背景更加复杂,目标更加不聚焦,同时由于图片分辨率低于图像,因此更加难做. image中的Detection,背景相对简单些,目标更加聚焦,同时图片分辨率高,因此更加容 ...

  10. Oracle 配置文件目录

    Oracle 配置文件目录 ① 在oracle安装目录下,找D:\oracle\product\10.2.0\client_1\NETWORK\ADMIN中的tnsnames.ora文件,找到之后,配 ...