接口自动化测试框架编写之路(1):unitest读取txt文件批量执行用例

方法一:获取所有用例py对应的绝对路径

def get_case_path():
    """
    function:获取所有需要执行的python文件的绝对路径
    return: cases_paths
    """
    cases_paths = []
    txt_path = base_dir_path + '\\' + 'cases_list'
    with open(txt_path, 'r') as f:
        while True:
            line = f.readline()
            if not line:
                break
            if not line.startswith('#'):
                cases_paths.append(base_dir_path + '\\' + line.replace('\n', ''))
    return cases_paths

方法二:生产测试用例集合,将所有测试用例实例加入其中

def test_suit():
    """
      function:将每个测试用例实例添加到all_suit测试用例集合中
      return: all_suit
    """
    all_suit = unittest.TestSuite()
    for i in get_case_path():
        file_name = i.split('\\')[-1]
        file_path = i.split(file_name)[0]
        suit = unittest.defaultTestLoader.discover(file_path, file_name)
        all_suit.addTests(suit)
    return all_suit

最后:调用mian函数执行测试用例集

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(test_suit())

以下为框架目录:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值