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
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')

@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

        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']
        try:
            self.assertEquals(case.expected,actual)
            read_excel.write_result('recharge',case.case_id,actual,'Pass')
        except Exception as e:
            read_excel.write_result('recharge',case.case_id,actual,'Fail')
            raise e

猜你喜欢

转载自www.cnblogs.com/gotesting/p/10194527.html
今日推荐