测试基础 xmind2testcase 导出和 excel 文件

驭我 · 2021年07月22日 · 最后由 驭我 回复于 2021年07月22日 · 443 次阅读

背景: xmind2testcase 个人觉得是一个非常好用,因为个人习惯使用 xmind,就想着写用例的时候只有 xmind 来提高写用例的效率,在 testerhome 中发现这个方案,在此感谢大佬提供开源,感谢 testerhome。由于个人习惯倾向使用 excel,所以改了源码。由于技术有限,只能改成这样,大家要是有更好的方案希望留下宝贵意见。本人菜鸟第一帖,不喜勿喷!!!

1.在 zentao.py 文件中增加写入 excel 的方法,写入 excel 采用的是 openpyxl。实现代码如下:

def xmind_to_zento_excel_file(xmind_file):
    """
        写入excle文件xlsx
    """
    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[:-7] + ".xlsx"
    if os.path.exists(zentao_file):
        os.remove(zentao_file)
        logging.info('The zentao csv file already exists, return it directly: %s', zentao_file)

    workbook = openpyxl.workbook.Workbook()
    ws = workbook.active
    line = 1
    for data in zentao_testcase_rows:
        for col in range(1, len(data) + 1):
            ws.cell(row=line, column=col).value = data[col - 1]
        line += 1
    workbook.save(zentao_file)
    logging.info('Convert XMind file(%s) to a zentao csv file(%s) successfully!', xmind_file, zentao_file)
    return zentao_file

2.在 application.py 文件增加导入 excel 的视图函数,

@app.route('/<filename>/to/zentao_excle')
def download_zentao_excle_file(filename):
    full_path = join(app.config['UPLOAD_FOLDER'], filename)

    if not exists(full_path):
        abort(404)

    zentao_excel_file = xmind_to_zento_excel_file(full_path)
    filename = os.path.basename(zentao_excel_file) if zentao_excel_file else abort(404)

    return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)

3..在 application.py 文件中修改删除函数 delete_record 和 delete_records

def delete_record(filename, record_id):
    xmind_file = join(app.config['UPLOAD_FOLDER'], filename)
    testlink_file = join(app.config['UPLOAD_FOLDER'], filename[:-5] + 'xml')
    zentao_file = join(app.config['UPLOAD_FOLDER'], filename[:-5] + 'csv')
    zentao_file_exlce = join(app.config["UPLOAD_FOLDER"], filename[:-6] + "xlsx") #此处修改,增加删除excle文件

    for f in [xmind_file, testlink_file, zentao_file, zentao_file_exlce]:#此处修改,增加删除excle文件
        if exists(f):
            os.remove(f)

    c = g.db.cursor()
    sql = 'UPDATE records SET is_deleted=1 WHERE id = ?'
    c.execute(sql, (record_id,))
    g.db.commit()


def delete_records(keep=20):
    """Clean up files on server and mark the record as deleted"""
    sql = "SELECT * from records where is_deleted<>1 ORDER BY id desc LIMIT -1 offset {}".format(keep)
    assert isinstance(g.db, sqlite3.Connection)
    c = g.db.cursor()
    c.execute(sql)
    rows = c.fetchall()
    for row in rows:
        name = row[1]
        xmind_file = join(app.config['UPLOAD_FOLDER'], name)
        testlink_file = join(app.config['UPLOAD_FOLDER'], name[:-5] + 'xml')
        zentao_file = join(app.config['UPLOAD_FOLDER'], name[:-5] + 'csv')
        zentao_file_excle = join(app.config["UPLOAD_FOLDER", name[:-6] + 'xslx'])#此处修改,增加删除excle文件

        for f in [xmind_file, testlink_file, zentao_file, zentao_file_excle]:#此处修改,增加删除excle文件
            if exists(f):
                os.remove(f)

        sql = 'UPDATE records SET is_deleted=1 WHERE id = ?'
        c.execute(sql, (row[0],))
        g.db.commit()

4.在 index.html 和 preview.html 中增加导出 excle 文件,修改源码
indexl.html 文件中修改如下

<tbody>
                   {% for record in records %}
                       <tr>
                           <td title="{{ record[1] }}"> {{ record[0] }}</td>
                           <td>{{ record[2] }}</td>
                           <td><a href="{{ url_for('uploaded_file',filename=record[1]) }}">XMIND</a> |
                               <a href="{{ url_for("download_zentao_excle_file", filename=record[1]) }}">excel</a> |{#                                此处修改!!!#}
                               <a href="{{ url_for('download_zentao_file',filename=record[1]) }}">csv</a> |
                               <a href="{{ url_for('download_testlink_file',filename=record[1]) }}">XML</a> |
                               <a href="{{ url_for('preview_file',filename=record[1]) }}">PREVIEW</a> |
                               <a href="{{ url_for('delete_file',filename=record[1], record_id=record[4]) }}">DELETE</a>
                           </td>
                       </tr>
                   {% endfor %}

                   </tbody>

preview.html 修改如下

<div class="header">
    <h1>{{ name }} - Preview</h1>
    <h2>TestSuites: {{ suite_count }} / TestCases: {{ suite | length }}
        / <a href="{{ url_for("download_zentao_file",filename= name) }}">Get Zentao CSV</a>
        / <a href="{{ url_for("download_zentao_excle_file",filename= name) }}">Get Zentao XSLX</a> {#                                此处修改!!!#}
        / <a href="{{ url_for("download_testlink_file",filename= name) }}">Get TestLink XML</a>
        / <a href="{{ url_for("index") }}">Go Back</a></h2>
</div>
共收到 2 条回复 时间 点赞

这种小工具的改进,挺有用的,赞!

多谢,主要想在 testerhome,记录下来会后忘记了可以看看😂

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册