配置文件的基础知识

在进行测试的时候,可以在代码里设置用例是执行全部还是按照设置去执行

excel_data.py
from openpyxl import load_workbook
class demo_excel:
    def __init__(self,file_name,sheet_name):
        self.file_name=file_name
        self.sheet_name=sheet_name

    def get_data(self,button="all"):
        '''button:控制是否执行所有的用例 默认为all
        为all就执行所有的用例,如果不等于all的就进入分支判断
        button的值只能输入all和列表这两种类型的参数'''
        open_wb=load_workbook(self.file_name)
        sheet=open_wb[self.sheet_name]
        data_list=[]
        for i in range(2,sheet.max_row+1):
               data_dict={}
               data_dict['ID']=sheet.cell(i,1).value
               data_dict['TITLE']=sheet.cell(i,2).value
               data_dict['METHOD']=sheet.cell(i,3).value
               data_dict['URL']=sheet.cell(i,4).value
               data_dict['DATA']=sheet.cell(i,5).value
               data_dict['header']=sheet.cell(i,6).value
               data_dict['expeced_errcode']=sheet.cell(i,7).value
               data_dict['expeced_errmsg'] = sheet.cell(i,8).value
               data_list.append(data_dict)#存储所有的数据
        #根据button值去判断
        if button=="all":
            final_data=data_list
        else:#[1,2,3,4]
            final_data=[]
            for item in data_list:
                if item['ID'] in button:
                    final_data.append(item)
        return final_data
if __name__ == '__main__':
  t=demo_excel("C:\\Users\\123456\\Desktop\\demo_excel.xlsx",'测试登陆').get_data()
  print(t)
test_http.py
import unittest
from  auto_study.API_AUTO.Tools.http_request import HttpRequest
from ddt import  ddt,data
from learn_ddt.excel_data import demo_excel
# test_data=demo_excel("C:\\Users\\123456\\Desktop\\demo_excel.xlsx",'测试登陆').get_data("all")
test_data=demo_excel("C:\\Users\\123456\\Desktop\\demo_excel.xlsx",'测试登陆').get_data([1,3])

@ddt
class TestHttp(unittest.TestCase):#登陆接口的用例
    def setUp(self):
        pass
    @data(*test_data)
    def test_api(self,item):#接口用例
            res = HttpRequest().http_request(item['URL'], item['METHOD'],eval(item['header']),item['DATA'])
            try:
                # self.assertEqual(item['expeced_errcode'],json.loads(res.text)['errcode'])或者是下面的形式
                self.assertEqual(item['expeced_errcode'], res.json()['errcode'])
            except AssertionError as e:
                print("出错啦,断言错误是{}".format(e))
                raise
            print(res.status_code)
            print(res.text)
    def tearDown(self):
        print("用例执行结束")
test_suit.py
import unittest
from auto_study.api_auto_test import HTMLTestRunner
from learn_ddt.test_http import TestHttp #具体到类名
from learn_ddt.excel_data import demo_excel
#ddt加载用例必须通过loader
t=demo_excel("C:\\Users\\123456\\Desktop\\demo_excel.xlsx",'测试登陆').get_data()
suite = unittest.TestSuite()
loader=unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestHttp))

with open("test_yapi.html","wb") as file:
    runner= HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2, description="测试接口网址yapi的登陆", title="yapi")
    runner.run(suite)

以上代码的缺点是:不通俗易懂

优化:放在配置文件里

配置文件
python里面用到的配置文件一边是properties、 config、 ini 、log4j
首先接触的是:configparser 可以去读取配置信息
configparser模块里面的ConfigParse

配置文件的3个区域
section option value
1)[]表示一个section;
2)=或:用来分隔key和value,两侧的空格会被忽略;
例如:[Button] 这个是section
button=all 这个分别是option value,相当于字典里的key和value

case.config
[Button]
button=all

[auto_test]
type_1=api_test
type_2=ui_test
name=["自动化"]

do_config.py
import  configparser

cf=configparser.ConfigParser()
cf.read('case.config',encoding='utf-8')
#方式一:
res_1=cf.get('Button','button')
print(res_1)
# #方式二:
res_2=cf['Button']['button']
print(res_2)

print(cf.sections())
print(cf.items('auto_test'))
#数据类型(结论:都是字符串,转换数据类型用eval())
print(type(cf.get('auto_test','type_1')))
print(type(cf.get('auto_test','name')))

###应用到项目中,将配置文件放置在工具类的下面

case.config
[Button]
button=[1,3]
read_config.py
import configparser
class ReadConfig:
    def read_config(self,file_name,section,option):
        cf=configparser.ConfigParser()
        cf.read(file_name,encoding='utf-8')
        # print(cf.get(section,option))
        return cf.get(section,option)
if __name__ == '__main__':
    res=ReadConfig().read_config('case.config','Button','button')
    print(res)
excel_data.py
from openpyxl import load_workbook
from auto_study.API_AUTO.Tools.read_config import ReadConfig
class demo_excel:
    def __init__(self,file_name,sheet_name):
        self.file_name=file_name
        self.sheet_name=sheet_name

    def get_data(self,button="all"):
        '''button:控制是否执行所有的用例 默认为all
        为all就执行所有的用例,如果不等于all的就进入分支判断
        button的值只能输入all和列表这两种类型的参数'''
        #从配置文件读取button的值
        button=ReadConfig().read_config('case.config','Button','button')
        open_wb=load_workbook(self.file_name)
        sheet=open_wb[self.sheet_name]
        data_list=[]
        for i in range(2,sheet.max_row+1):
               data_dict={}
               data_dict['ID']=sheet.cell(i,1).value
               data_dict['TITLE']=sheet.cell(i,2).value
               data_dict['METHOD']=sheet.cell(i,3).value
               data_dict['URL']=sheet.cell(i,4).value
               data_dict['DATA']=sheet.cell(i,5).value
               data_dict['header']=sheet.cell(i,6).value
               data_dict['expeced_errcode']=sheet.cell(i,7).value
               data_dict['expeced_errmsg'] = sheet.cell(i,8).value
               data_list.append(data_dict)#存储所有的数据
        #根据button值去判断
        if button=="all":
            final_data=data_list
        else:#[1,2,3,4]
            final_data=[]
            for item in data_list:
                if item['ID'] in eval(button):
                    final_data.append(item)
        return final_data
if __name__ == '__main__':
  t=demo_excel("C:\\Users\\123456\\Desktop\\demo_excel.xlsx",'测试登陆').get_data()
  print(t)

test_suit.py
import unittest
from auto_study.api_auto_test import HTMLTestRunner
from learn_ddt.test_http import TestHttp #具体到类名
from learn_ddt.excel_data import demo_excel
#ddt加载用例必须通过loader
t=demo_excel("C:\\Users\\123456\\Desktop\\demo_excel.xlsx",'测试登陆').get_data()
suite = unittest.TestSuite()
loader=unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestHttp))

with open("test_yapi.html","wb") as file:
    runner= HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2, description="测试接口网址yapi的登陆", title="yapi")
    runner.run(suite)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值