【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传

背景

前一篇博客记录的可以上传用例到testlink指定用例集的脚本,内部分享给了之后,同事希望能将testlink上原有的用例下载下来,用于下次修改上传,所有有了本文脚本。

具体实现

获取用例信息
def download_testcase():
    """
    获取(下载)testlink上面指定用例集的数据
    :return:
    """
    datas = []
    for data in tlc.getTestCasesForTestSuite(father_id, True, 'full'):
        actions = []
        expected_results = []
        name = data["name"]
        summary = data["summary"]
        preconditions = data["preconditions"]
        importance = data["importance"]
        execution_type = data["execution_type"]
        author = data["author_id"]
        # print(json.dumps(data, indent=4))
        for i in range(len(data["steps"])):
            actions.append(data["steps"][i]["actions"])
            expected_results.append(data["steps"][i]["expected_results"])
        datas.append((name, preconditions, '\n'.join(actions), '\n'.join(expected_results), format_execution_type(execution_type), format_auth(author), format_importance(importance), summary))
进行数据转换
def format_execution_type(source_data):
    """
    转换执行方式
    :param source_data:
    :return:
    """
    switcher = {
        '2': "自动化",
        '1': "手工"
    }
    return switcher.get(source_data, "Param not defind")

def format_importance(source_data):
    """
    转换优先级
    :param source_data:
    :return:
    """
    switcher = {
        '1': "低",
        '2': "中",
        '3': "高"
    }
    return switcher.get(source_data, "Param not defind")

def format_auth(source_data):
    """
    转换作者:可以通过testlink的user表查询到对应id->name对
    :param source_data:
    :return:
    """
    switcher = {
        '100': "tester_name",
    }
    return switcher.get(source_data, "Param not defind")
保存至Excel
def save_suits(file_path, datas):
    """
    保存用例
    :param file_path: 保存路径
    :param datas:
    :return:
    """
    book = xlrd.open_workbook(file_path, formatting_info=True)  # 读取Excel
    new_book = copy.copy(book)  # 复制读取的Excel
    sheet = new_book.get_sheet(0)  # 取第一个sheet页
    line_num = 1
    for i in range(0, len(datas)):
        name, preconditions, actions, expected_results,  execution_type, author, importance, summary = datas[i]
        sheet.write(line_num, 0, u'%s' % name)
        sheet.write(line_num, 1, u'%s' % preconditions)
        sheet.write(line_num, 2, u'%s' % actions)
        sheet.write(line_num, 3, u'%s' % expected_results)
        sheet.write(line_num, 4, u'%s' % execution_type)
        sheet.write(line_num, 5, u'%s' % author)
        sheet.write(line_num, 6, u'%s' % importance)
        sheet.write(line_num, 7, u'%s' % summary)
        line_num += 1
    report_path = os.path.abspath(os.path.join('download'))
    if not os.path.exists(report_path):
        os.makedirs(report_path)
    suits_name = get_suites(father_id)["name"]
    new_book.save(os.path.abspath(os.path.join(report_path, '用例集_{}@{}.xlsx'.format(suits_name, time.strftime('%Y.%m.%d@%H%M%S')))))  
    
def get_suites(suite_id):
    """
    获取用例集信息
    :return: 
    """
    try:
        suites = tlc.getTestSuiteByID(suite_id)
        return suites
    except testlink.testlinkerrors.TLResponseError as e:
        # traceback.print_exc()
        logger.warning(str(e).split('\n')[1])
        logger.warning(str(e).split('\n')[0])
        return

使用方法

环境依赖
环境依赖安装方法
Python3
xlrd库pip install xlrd
testlink库pip install TestLink-API-Python-client
xlutilspip install xlutils
具体方法
  • 将上述的代码保存到一个文件中,底部添加下列代码进行调用
if __name__ == "__main__":
    url = "http://localhost/lib/api/xmlrpc/v1/xmlrpc.php"
    key = "6c3fe0796142db21"  # 这个key是错误的key
    tlc = testlink.TestlinkAPIClient(url, key)
    father_id = "274539"   # 想要下载的用例集的ID,可通过在testlink界面选取用例集,然后点击右键获取
    download_testcase()
  • 目录结构参考,与上一篇文章的脚本放在了一起
D:\Project\UPLOAD_DATA2TESTLINK
│  download_testcase.py
│  logger_better.py
│  upload_excel_data.py
│
└─testCase
        down_load_template.xls
  • 在上一步的文件同一目录下创建一个testCase文件夹,创建一个xls文件,文件格式如下:

1172048-20180513141822623-981504031.png

转载于:https://www.cnblogs.com/Detector/p/9030650.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现自动执行 TestLink 测试用例,你可以考虑以下步骤: 1. 安装 TestLink:首先,你需要安装 TestLink 测试管理工具。你可以从 TestLink 官方网站下载并按照它们的安装指南进行安装。 2. 配置测试环境:确保你的测试环境中已经配置好了所需的测试工具和依赖项,例如自动化测试框架、测试驱动工具等。 3. 创建测试计划和测试用例:在 TestLink 中创建测试计划和相应的测试用例。确保你已经定义了所有必要的测试步骤和预期结果。 4. 编写自动化测试脚本:使用适合你的项目的自动化测试框架(如Selenium、Appium等),编写测试脚本来执行你的测试用例。这些脚本可以与 TestLink 进行集成。 5. 集成 TestLink 和自动化测试框架:使用 TestLink 提供的 API 或插件,将你的自动化测试框架与 TestLink 集成。这样,你就可以将自动化测试脚本与 TestLink 中的测试用例关联起来。 6. 执行自动化测试:通过运行自动化测试脚本,执行与 TestLink 关联的测试用例。在执行过程中,你可以将测试结果反馈给 TestLink。 7. 查看测试结果和生成测试报告:在 TestLink 中查看测试结果,并生成测试报告。这些报告可以帮助你跟踪测试进度和问题。 请注意,这只是一个高级概述。具体的实施步骤可能因你的项目和工具的不同而有所变化。你需要根据你的具体情况进行调整和进一步研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值