Frame--配置文件

1. 配置文件
1.1 标准配置文件
格式有:.config、.ini、.conf、.properties、.xml
1.2 python配置文件
1. 配置文件中包含一个或多个 section, 每个 section 有自己的 option;
2. section 用 [sect_name] 表示,每个option是一个键值对,使用分隔符 = 或 : 隔开;
3. 在option 分隔符两端的空格会被忽略掉
4. 配置文件使用 #注释
## case.conf
[SECTION]
button = off
case_id = [1,3,4,5,7]
1.3 配置文件读取
configparser库:处理python配置文件的专用库
import configparser


class ReadConfig:
    # file_path:文件位置,section:配置文件section,option:key键
    def read_config(self, file_path, section, option):
        cf = configparser.ConfigParser()
        cf.read(file_path, encoding='utf-8')
        value = cf.get(section, option)
        return value


if __name__ == '__main__':
    button = ReadConfig().read_config('case.conf', 'SECTION', 'button')
    case_no = ReadConfig().read_config('case.conf', 'SECTION', 'case_id')
    print(button,case_no)
> off [1,3,4,5,7]
2. 代码
# do_excel.py

import openpyxl

class DoExcel:
    def read_data(self, button, case_id):   # 读取excel文件中的测试数据并根据配置文件进行过滤
        wb = openpyxl.load_workbook('test_data.xlsx')
        sheet = wb['model1']                # model1 excel工作簿中工作表名
        header = []
        for column in range(1, 6):
            header.append(sheet.cell(1, column).value)
        # ['case_id', 'title', 'param_a', 'param_b', 'ExpectedResult', 'ActualResult', 'TestResult']

        test_data = []
        for row_id  in range(2, 10):
            row_data = {}
            for column_id in range(1, 6):
                row_data[header[column_id-1]] = sheet.cell(row_id, column_id).value
            test_data.append(row_data)

        # 根据配置文件过滤测试数据

        if button == 'on':       # 如果button为on时,执行配置文件中配置的用例,否则执行左右用例
            final_data = []      # final_data:最终的的测试数据
            for item in test_data:
                if item['case_id'] in case_id:
                    final_data.append(item)
        else:
            final_data = test_data
        return final_data

    
    # 测试数据回写函数
    def write_back(self, row_id, ActualResult, TestResult):
        wb = openpyxl.load_workbook('test_data.xlsx')
        sheet = wb['model1']
        sheet.cell(row_id, 6).value = ActualResult
        sheet.cell(row_id, 7).value = TestResult
        wb.save('test_data.xlsx')

# excute_case.py
import unittest
from zy.test_case import TestSub
from zy.do_excel import DoExcel
import HTMLTestRunnerNew
from zy.conf import ReadConfig

button = ReadConfig().read_config('case.conf', 'SECTION', 'button')
case_no = eval(ReadConfig().read_config('case.conf', 'SECTION', 'case_id'))  # 配置文件读取的数据默认为字符串

test_data = DoExcel().read_data(button, case_no)

suite = unittest.TestSuite()
for item in test_data:
    suite.addTest(TestSub(item['param_a'], item['param_b'], item['ExpectedResult'], item['title'], item['case_id'], 'test_sub'))

with open('TestResult.html', 'wb+') as file:
    runner = HTMLTestRunnerNew.HTMLTestRunner(file, title='减法测试', description='第二轮整体回归测试减法方法的正确性', tester='华杰-zy')
    runner.run(suite)
# test_case.py
import unittest
from zy.code import Sub
from zy.do_excel import DoExcel


class TestSub(unittest.TestCase):
    def setUp(self):
        self.t = Sub()
        self.wb = DoExcel()

    def __init__(self, a, b, expected, title, case_id, methodName):
        super(TestSub, self).__init__(methodName)
        self.a = a
        self.b = b
        self.expected = expected
        self.title = title
        self.case_id = case_id

    def tearDown(self):
        pass

    def test_sub(self):
        print('用例标题:{}'.format(self.title))
        res = self.t.sub(self.a, self.b)
        try:
            self.assertEqual(res, self.expected)
            test_result = 'PASS'
        except AssertionError as e:
            test_result = 'FAIL'
            raise e
        finally:
            self.wb.write_back(self.case_id+1, res, test_result)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值