• python


    # -*- 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
  • 相关阅读:
    Dijkstra-leetcode 743.网络延迟时间
    BFS-leetcode787 K站中转内最便宜的航班
    图论基础——单源最短路径问题
    DFS leetcode-547 朋友圈
    SpringBoot 使用注解向容器中注册Bean的方法总结
    SpringBoot对SpringMVC的支持
    数据源简介
    Spring MVC简介
    2020-2-10 Python 列表切片陷阱:引用、复制与深复制
    2020-2-2 语法糖
  • 原文地址:https://www.cnblogs.com/gotesting/p/10196138.html
Copyright © 2020-2023  润新知