【xmind测试用例转换为excel工具】xmind2testcase工具代码优化

基础信息

xmind2testcase工具安装

1、安装python环境(建议3.6以上版本)
2、在命令行执行pip install xmind2testcase
3、在命令行执行xmind2testcase webtool(执行成功即代表安装完成)
PS:请尽量使用XMind8 Update9版本编写测试用例,且思维导图符合格式要求

xmind2testcase工具执行

1、在命令行执行xmind2testcase webtool
2、.命令行会显示本地生成的网页网址,直接复制到浏览器进入即可!,效果见下图
在这里插入图片描述
在这里插入图片描述

xmind格式要求

在这里插入图片描述

禅道用例模板格式要求

在这里插入图片描述

代码优化

适配禅道模板代码优化

1、找到源码位置(我的路径是D:\python\Lib\site-packages\xmind2testcase)
2、找到文件 parser.py(修改以下方法去除用例标题中的空格)

def gen_testcase_title(topics):
    """Link all topic's title as testcase title"""
    titles = [topic['title'] for topic in topics]
    titles = filter_empty_or_ignore_element(titles)
 
    # when separator is not blank, will add space around separator, e.g. '/' will be changed to ' / '
    separator = config['sep']
    if separator != ' ':
        # 修改前
        # separator = ' {} '.format(separator)
        # 修改后
        separator = f'{separator}'
    return separator.join(titles)

3、找到文件 zentao.py文件

  1. 修改优先级部分
def gen_case_priority(priority):
	    # 修改前
	    # mapping = {1: '高', 2: '中', 3: '低'}
	    # 修改后
	    mapping = {1: '1', 2: '2', 3: '3', 4: '4'}
	    if priority in mapping.keys():
	        return mapping[priority]
	    else:`
	        # 修改前
	        return '中'
	        # 修改后
	        return '2'
  1. 修改.修改用例类型部分

def gen_case_type(case_type):
    # 修改前
    # mapping = {1: '手动', 2: '自动'}
    # 修改后
    mapping = {1: '功能测试', 2: '其他测试'}
    if case_type in mapping.keys():
        return mapping[case_type]
    else:
        # 修改前
        # return '手动'
        # 修改后
        return '功能测试'

  1. 修改修改适应阶段部分

def gen_a_testcase_row(testcase_dict):
    case_module = gen_case_module(testcase_dict['suite'])
    case_title = testcase_dict['name']
    case_precontion = testcase_dict['preconditions']
    case_step, case_expected_result = gen_case_step_and_expected_result(testcase_dict['steps'])
    case_keyword = ''
    case_priority = gen_case_priority(testcase_dict['importance'])
    case_type = gen_case_type(testcase_dict['execution_type'])
    # 修改前
    # case_apply_phase = '迭代测试'
    # 修改后
    case_apply_phase = '系统测试阶段'
    row = [case_module, case_title, case_precontion, case_step, case_expected_result, case_keyword, case_priority,
           case_type, case_apply_phase]
    return row

4.修复导出文件有空行问题,修改一下方法xmind_to_zentao_csv_file ,写入方法增加newline=‘’

# 修改前
# with open(zentao_file, 'w', encoding='utf8') as f:
# 修改后
with open(zentao_file, 'w', encoding='utf8', newline='') as f:
  1. 修复用例步骤、预期结果序号后多一个空格问题

def gen_case_step_and_expected_result(steps):
    case_step = ''
    case_expected_result = ''
    # 修改后,把+ '. ' + 后的空格去掉  + '.' +
    for step_dict in steps:
        case_step += str(step_dict['step_number']) + '.' + step_dict['actions'].replace('\n', '').strip() + '\n'
        case_expected_result += str(step_dict['step_number']) + '.' + \
                                step_dict['expectedresults'].replace('\n', '').strip() + '\n' \

  1. 修复每导出一个测试用例步骤和预期结果会多一个换行符问题
def gen_case_step_and_expected_result(steps):
    case_step = ''
    case_expected_result = ''
    # 修改后,把+ '. ' + 后的空格去掉  + '.' +
    for step_dict in steps:
        case_step += str(step_dict['step_number']) + '.' + step_dict['actions'].replace('\n', '').strip() + '\n'
        case_expected_result += str(step_dict['step_number']) + '.' + \
                                step_dict['expectedresults'].replace('\n', '').strip() + '\n' \
            if step_dict.get('expectedresults', '') else ''
    # 添加,去除每个单元格里最后一个换行符
    case_step = case_step.rstrip('\n')
    case_expected_result = case_expected_result.rstrip('\n')
    return case_step, case_expected_result
    #去除最后一个换行符

7、填写默认关键词

def gen_a_testcase_row(testcase_dict):
    case_module = gen_case_module(testcase_dict['suite'])
    case_title = testcase_dict['name']
    case_precontion = testcase_dict['preconditions']
    case_step, case_expected_result = gen_case_step_and_expected_result(testcase_dict['steps'])
    # 此处可填写默认关键词
    case_keyword = ''

8、适配禅道用例模板文件修改

def xmind_to_zentao_csv_file(xmind_file):
    """Convert XMind file to a zentao csv file"""
    xmind_file = get_absolute_path(xmind_file)
    logging.info('Start converting XMind file(%s) to zentao file...', xmind_file)
    testcases = get_xmind_testcase_list(xmind_file)
    fileheader = ["所属产品","所属模块","相关研发需求", "用例标题", "前置条件","关键词", "优先级", "用例类型", "适用阶段","用例状态", "步骤", "预期"]
    zentao_testcase_rows = [fileheader]
    for testcase in testcases:
        row = gen_a_testcase_row(testcase)
        zentao_testcase_rows.append(row)
    zentao_file = xmind_file[:-6] + '.csv'
    if os.path.exists(zentao_file):
        os.remove(zentao_file)
        # logging.info('The zentao csv file already exists, return it directly: %s', zentao_file)
        # return zentao_file
    #with open(zentao_file, 'w', encoding='utf8') as f:
    with open(zentao_file, 'w', encoding='utf8', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(zentao_testcase_rows)
        logging.info('Convert XMind file(%s) to a zentao csv file(%s) successfully!', xmind_file, zentao_file)
    return zentao_file


def gen_a_testcase_row(testcase_dict):
    case_product = testcase_dict['product']
    case_module = gen_case_module(testcase_dict['suite'])
    case_RD_requirements=''
    case_title = testcase_dict['name']
    case_precontion = testcase_dict['preconditions']
    case_step, case_expected_result = gen_case_step_and_expected_result(testcase_dict['steps'])
    case_keyword = ''
    case_priority = gen_case_priority(testcase_dict['importance'])
    case_type = gen_case_type(testcase_dict['execution_type'])
    case_apply_phase = '功能测试阶段'
    case_statue='正常'
    row = [case_product,case_module,case_RD_requirements,case_title, case_precontion,case_keyword, case_priority, case_type, case_apply_phase, case_statue, case_step, case_expected_result]
    return row

xmind2testcase加装生成excel文件功能

1、找到xmind2testcase中的zentao.py文件,增加xmind_to_zentao_xlsx_file_by_pandas函数
(我的路径是D:\python\Lib\site-packages\xmind2testcase\zentao.py)

import pandas as pd    #使用pandas包来写入excel的,需要引入
def xmind_to_zentao_xlsx_file_by_pandas(xmind_file): #xmind导出禅道用例模板的xlsx格式
    """Convert XMind file to a zentao xlsx file"""
    xmind_file = get_absolute_path(xmind_file)
    logging.info('Start converting XMind file(%s) to zentao file...', xmind_file)
    testcases = get_xmind_testcase_list(xmind_file)
    fileheader = ["所属产品","所属模块","相关研发需求", "用例标题", "前置条件","关键词", "优先级", "用例类型", "适用阶段","用例状态", "步骤", "预期"]
    zentao_testcase_rows = []
    for testcase in testcases:
        row = gen_a_testcase_row(testcase)
        zentao_testcase_rows.append(row)
    zentao_file = xmind_file[:-6] + '.xlsx'
    df = pd.DataFrame(data=zentao_testcase_rows, columns=fileheader) #构造数据
    df.to_excel(zentao_file, index=False)  #写入文件,设置不需要索引
    logging.info('Convert XMind file(%s) to a zentao csv file(%s) successfully!', xmind_file, zentao_file)
    return zentao_file

2、前往webtool安装目录下(我的位置为D:\python\Lib\site-packages\webtool)的application.py中,添加路由和引用

from xmind2testcase.zentao import xmind_to_zentao_xlsx_file_by_pandas
#导入zentao.py的xmind_to_zentao_xlsx_file_by_pandas函数

@app.route('/<filename>/to/zentao_xlsx')
def download_zentao_xlsx_file(filename):
    full_path = join(app.config['UPLOAD_FOLDER'], filename)
    if not exists(full_path):                                                             
        abort(404)
    zentao_xlsx_file = xmind_to_zentao_xlsx_file_by_pandas(full_path)
    filename = os.path.basename(zentao_xlsx_file) if zentao_xlsx_file else abort(404)
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)

3、前往前往templates安装目录下(我的位置为D:\python\Lib\site-packages\webtool\templates)的index.html中添加xlsx的访问路径

<a href="{{ url_for('download_zentao_xlsx_file',filename=record[1]) }}">XLSX</a> |

在这里插入图片描述
4、在同级目录的preview.html中添加xlsx的访问路径

/ <a href="{{ url_for('download_zentao_xlsx_file',filename= name) }}">Get Zentao xlsx</a>

在这里插入图片描述

使用说明

1、使用xmind工具写测试用例
2、打开xmind2testcase工具
3、在工具内导入xmind测试用例文件
4、使用工具导出excel文件
在这里插入图片描述
在这里插入图片描述

5、将导出的excel文件导入禅道

参考以下博文
https://blog.csdn.net/qq_45336444/article/details/121232164
https://blog.csdn.net/weixin_45046421/article/details/124962569

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值