Exemplo n.º 1
0
class DoExcel:
    '''根据配置文件,对excel中数据进行全部读取、单个读取和写入'''
    flag = ReadConfig().get_str('TestSet','RunCase') # 从配置文件中读取执行用例的配置

    def __init__(self,sheetname,filename=common_path.excel_path):
        self.filename = filename
        self.sheetname = sheetname

    def read_all_data(self):
        '''根据配置文件中定义所有跑的用例,输出用例数据'''
        wb = load_workbook(self.filename)
        sheet = wb[self.sheetname]
        file_data = []
        for i in range(2, sheet.max_row+1):
            row_data = {}
            row_data['caseId'] = sheet.cell(i,1).value
            row_data['description'] = sheet.cell(i,2).value
            row_data['method'] = sheet.cell(i,3).value
            row_data['params'] = sheet.cell(i,4).value
            row_data['expectedResult'] = sheet.cell(i,5).value
            row_data['sql'] = sheet.cell(i,6).value
            if self.flag.upper() == 'ALL':  # 如果配置文件是执行全部用例,则读取的每行数据都添加到最终输出数据中
                file_data.append(row_data)
            else:
                flag = eval(self.flag)  # 用例的配置文件格式为字典 嵌套列表
                if self.sheetname in flag.keys():   # 表单名即为模块名,判断是否有设置此模块的用例
                    if row_data['caseId'] in flag[self.sheetname]: # 判断当前读取出来的用例是否在配置文件中
                        file_data.append(row_data)
        wb.close()
        return file_data

    def read_one_data(self,row,column):
        '''读取某个单元格的值并返回'''
        wb = load_workbook(self.filename)
        sheet = wb[self.sheetname]
        res = sheet.cell(row,column).value
        wb.close()
        return res

    def update_excel(self,row,column,value):
        '''写入某个单元格的值'''
        wb = load_workbook(self.filename)
        sheet = wb[self.sheetname]
        try:
            sheet.cell(row,column).value = value
            wb.save(self.filename)
            wb.close()
        except Exception as e:
            print('写入数据错误,错误为{}'.format(e))
Exemplo n.º 2
0
    def __init__(self, configfile=common_path.config_path, logfile=common_path.log_path):
        '''configefile:配置文件地址
           logFile:写入日志文件的地址,默认和当前文件在同一地址下,文件名为logger.log'''
        cf = ReadConfig(configfile)
        self.logger_name = cf.get_str('Log','LogName')
        self.log_level = cf.get_str('Log','LogLevel')
        self.handler_type = cf.get_str('Log','HandlerType')
        self.handler_level = cf.get_str('Log','HandlerLevel')
        self.format = cf.get_str('Log','Format')

        self.logger= logging.getLogger(self.logger_name)  # 创建一个新的日志接收器
        self.logger.setLevel(self.log_level)             # 给日志接收器设置接收等级
        if self.handler_type == 'StreamHandler':         # 根据配置文件中输出渠道的设置,创建不同的输出渠道
            self.handler=logging.StreamHandler()
        else:
            self.handler=logging.FileHandler(logfile,'a',encoding='utf-8')
        self.handler.setLevel(self.handler_level)           # 给输出渠道设置输出等级
        self.handler_format = logging.Formatter(self.format)  # 根据配置生成输出渠道输出模板
        self.handler.setFormatter(self.handler_format)         # 将模板与输出渠道挂接
Exemplo n.º 3
0
 def __init__(self,
              host='127.0.0.1',
              port=3306,
              user='',
              pwd='',
              database='',
              configfile=configfile,
              session=None):
     '''可以使用数据库链接信息或配置文件直接初始化数据库连接对象'''
     if configfile is not None:  # 如果存在配置文件,则按照配置文件去读取连接信息
         cf = ReadConfig(configfile)
         self.host = cf.get_str(session, 'Host')  # 此处将配置文件中option写死为Host
         self.port = cf.get_str(session, 'Port')
         self.user = cf.get_str(session, 'UserName')
         self.pwd = cf.get_str(session, 'Pwd')
         self.database = cf.get_str(session, 'Database')
     else:
         self.host = host
         self.port = port
         self.user = user
         self.pwd = pwd
         self.database = database
Exemplo n.º 4
0
# @Author: Tansy_Xiaoming
# @Email : [email protected]
# @File  : bidloan_case.py
import unittest
from ddt import ddt, data, unpack
import json
from class_0313_api_practice_4.common.do_excel import DoExcel
from class_0313_api_practice_4.common.my_log import MyLog
from class_0313_api_practice_4.common.http_request import HttpRequest
from class_0313_api_practice_4.common.check_database import CheckDB
from class_0313_api_practice_4.common.read_config import ReadConfig
from class_0313_api_practice_4.common.common_data import CommonData

wb = DoExcel('BidLoan')
test_data = wb.read_all_data()
url = ReadConfig().get_str('TestData', 'URL') + '/member/bidLoan'
login_url = ReadConfig().get_str('TestData', 'URL') + '/member/login'


@ddt
class BidLoanCase(unittest.TestCase):
    '''定义投资的用例实现'''
    def setUp(self):
        self.my_logger = MyLog()
        self.db = CheckDB(session='DBConnect_QA')
        self.my_logger.info_log('---------投资用例开始执行----------')

    def tearDown(self):
        self.my_logger.info_log('---------投资用例结束执行-----------')

    @data(*test_data)
Exemplo n.º 5
0
# -*- coding: utf-8 -*-
# @Time  : 2019/3/25 11:10
# @Author: Tansy_Xiaoming
# @Email : [email protected]
# @File  : run_case.py
import unittest
import HTMLTestRunnerNew
from class_0313_api_practice_4.common import common_path
from class_0313_api_practice_4.common.read_config import ReadConfig
from class_0313_api_practice_4.test_case import register_case
from class_0313_api_practice_4.test_case import login_case
from class_0313_api_practice_4.test_case import recharge_case
from class_0313_api_practice_4.test_case import bidloan_case

run_flag = ReadConfig().get_str('TestSet', 'RunCase')  # 读取运行用例配置
suite = unittest.TestSuite()
loader = unittest.TestLoader()
if run_flag == 'ALL':
    suite.addTests(loader.loadTestsFromModule(register_case))
    suite.addTests(loader.loadTestsFromModule(login_case))
    suite.addTests(loader.loadTestsFromModule(recharge_case))
    suite.addTests(loader.loadTestsFromModule(bidloan_case))
else:
    run_flag = eval(run_flag)
    for item in run_flag:  # 根据用例配置,加载不同的用例实现类
        if item == 'Register':
            run_case = getattr(register_case, 'RegisterCase')  # 将字符串转换为class类型
        elif item == 'Login':
            run_case = getattr(login_case, 'LoginCase')
        elif item == 'Recharge':
            run_case = getattr(recharge_case, 'RechargeCase')
Exemplo n.º 6
0
# @Author: Tansy_Xiaoming
# @Email : [email protected]
# @File  : recharge_case.py
import unittest
from ddt import ddt,data,unpack
import json
from class_0313_api_practice_4.common.do_excel import DoExcel
from class_0313_api_practice_4.common.my_log import MyLog
from class_0313_api_practice_4.common.http_request import HttpRequest
from class_0313_api_practice_4.common.check_database import CheckDB
from class_0313_api_practice_4.common.read_config import ReadConfig
from class_0313_api_practice_4.common.common_data import CommonData

wb = DoExcel('Recharge')
test_data = wb.read_all_data()
url = ReadConfig().get_str('TestData','URL') + '/member/recharge'
login_url = ReadConfig().get_str('TestData','URL') + '/member/login'


@ddt
class RechargeCase(unittest.TestCase):
    '''实现充值的用例逻辑'''
    def setUp(self):
        self.my_logger = MyLog()
        self.db = CheckDB(session='DBConnect_QA')
        self.my_logger.info_log('---------充值用例开始执行----------')

    def tearDown(self):
        self.my_logger.info_log('-----------充值用例执行结束---------')

    @data(*test_data)
Exemplo n.º 7
0
# @Time  : 2019/3/23 16:33
# @Author: Tansy_Xiaoming
# @Email : [email protected]
# @File  : register_case.py
import unittest
from ddt import ddt, data, unpack
import json
from class_0313_api_practice_4.common.do_excel import DoExcel
from class_0313_api_practice_4.common.my_log import MyLog
from class_0313_api_practice_4.common.http_request import HttpRequest
from class_0313_api_practice_4.common.check_database import CheckDB
from class_0313_api_practice_4.common.read_config import ReadConfig

wb = DoExcel('Register')  # 实例化一个excel对象
test_data = wb.read_all_data()  # 读取注册的需要运行的全部用例
url = ReadConfig().get_str(
    'TestData', 'URL') + '/member/register'  # 根绝配置文件定义的URL去调对应地址的注册接口


@ddt
class RegisterCase(unittest.TestCase):
    '''定义注册用例的逻辑'''
    def setUp(self):
        print('---------注册用例开始执行了----------')
        self.my_logger = MyLog()
        self.db = CheckDB(session='DBConnect_QA')

    def tearDown(self):
        print('----------注册用例执行结束了----------')

    @data(*test_data)
    @unpack