python提效小工具-統計xmind用例數量

2022-10-15 15:00:19

問題:做測試的朋友們經常會用到xmind這個工具來梳理測試點或寫測試用例,但是xmind8沒有自帶的統計測試用例,其他版本的xmind有些自帶節點數量統計功能,但也也不會累計最終的數量,導致統計測試工作量比較困難。

解決方法:利用python開發小工具,實現同一份xmind檔案中一個或多個sheet頁的用例數量統計功能。

一、原始碼

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
__author__ = 'zhongxintao'
import tkinter as tk
from tkinter import filedialog, messagebox
from xmindparser import xmind_to_dict
import xmind


class ParseXmind:
    def __init__(self, root):
        self.count = 0
        self.case_fail = 0
        self.case_success = 0
        self.case_block = 0
        self.case_priority = 0

        # total彙總用
        self.total_cases = 0
        self.total_success = 0
        self.total_fail = 0
        self.total_block = 0
        self.toal_case_priority = 0

        # 設定彈框標題、初始位置、預設大小
        root.title(u'xmind檔案解析及用例統計工具')
        width = 760
        height = 600
        xscreen = root.winfo_screenwidth()
        yscreen = root.winfo_screenheight()
        xmiddle = (xscreen - width) / 2
        ymiddle = (yscreen - height) / 2
        root.geometry('%dx%d+%d+%d' % (width, height, xmiddle, ymiddle))

        # 設定2個Frame
        self.frm1 = tk.Frame(root)
        self.frm2 = tk.Frame(root)

        # 設定彈框佈局
        self.frm1.grid(row=1, padx='20', pady='20')
        self.frm2.grid(row=2, padx='30', pady='30')

        self.but_upload = tk.Button(self.frm1, text=u'上傳xmind檔案', command=self.upload_files, bg='#dfdfdf')
        self.but_upload.grid(row=0, column=0, pady='10')
        self.text = tk.Text(self.frm1, width=55, height=10, bg='#f0f0f0')
        self.text.grid(row=1, column=0)
        self.but2 = tk.Button(self.frm2, text=u"開始統計", command=self.new_lines, bg='#dfdfdf')
        self.but2.grid(row=0, columnspan=6, pady='10')
        self.label_file = tk.Label(self.frm2, text=u"檔名", relief='groove', borderwidth='2', width=25,
                                   bg='#FFD0A2')
        self.label_file.grid(row=1, column=0)
        self.label_case = tk.Label(self.frm2, text=u"用例數", relief='groove', borderwidth='2', width=10,
                                   bg='#FFD0A2').grid(row=1, column=1)
        self.label_pass = tk.Label(self.frm2, text=u"成功", relief='groove', borderwidth='2', width=10,
                                   bg='#FFD0A2').grid(row=1, column=2)
        self.label_fail = tk.Label(self.frm2, text=u"失敗", relief='groove', borderwidth='2', width=10,
                                   bg='#FFD0A2').grid(row=1, column=3)
        self.label_block = tk.Label(self.frm2, text=u"阻塞", relief='groove', borderwidth='2', width=10,
                                    bg='#FFD0A2').grid(row=1, column=4)
        self.label_case_priority = tk.Label(self.frm2, text="p0case", relief='groove', borderwidth='2',
                                            width=10, bg='#FFD0A2').grid(row=1, column=5)

    def count_case(self, li):
        """統計xmind中的用例數"""
        for i in range(len(li)):
            if li[i].__contains__('topics'):
                # 帶topics標籤表示有子標題,遞迴執行方法
                self.count_case(li[i]['topics'])
                # 不帶topics表示無子標題,此級別即是用例
            else:
                # 有標記成功或失敗時會有makers標籤
                if li[i].__contains__('makers'):
                    for mark in li[i]['makers']:
                        # 成功
                        if mark == "symbol-right":
                            self.case_success += 1
                        # 失敗
                        elif mark == "symbol-wrong":
                            self.case_fail += 1
                        # 阻塞
                        elif mark == "symbol-attention":
                            self.case_block += 1
                        # 優先順序
                        elif mark == "priority-1":
                            self.case_priority += 1
                # 用例總數
                self.count += 1

    def new_line(self, filename, row_number):
        """用例統計表新增一行"""
        # sheets是一個list,可包含多sheet頁
        sheets = xmind_to_dict(filename)  # 呼叫此方法,將xmind轉成字典
        for sheet in sheets:
            print(sheet)
            # 字典的值sheet['topic']['topics']是一個list
            my_list = sheet['topic']['topics']
            print(my_list)
            self.count_case(my_list)

        # 插入一行統計資料
        lastname = filename.split('/')
        self.label_file = tk.Label(self.frm2, text=lastname[-1], relief='groove', borderwidth='2', width=25)
        self.label_file.grid(row=row_number, column=0)

        self.label_case = tk.Label(self.frm2, text=self.count, relief='groove', borderwidth='2', width=10)
        self.label_case.grid(row=row_number, column=1)
        self.label_pass = tk.Label(self.frm2, text=self.case_success, relief='groove', borderwidth='2',
                                   width=10)
        self.label_pass.grid(row=row_number, column=2)
        self.label_fail = tk.Label(self.frm2, text=self.case_fail, relief='groove', borderwidth='2', width=10)
        self.label_fail.grid(row=row_number, column=3)
        self.label_block = tk.Label(self.frm2, text=self.case_block, relief='groove', borderwidth='2', width=10)
        self.label_block.grid(row=row_number, column=4)
        self.label_case_priority = tk.Label(self.frm2, text=self.case_priority, relief='groove', borderwidth='2',
                                            width=10)
        self.label_case_priority.grid(row=row_number, column=5)
        self.total_cases += self.count
        self.total_success += self.case_success
        self.total_fail += self.case_fail
        self.total_block += self.case_block
        self.toal_case_priority += self.case_priority

    def new_lines(self):
        """用例統計表新增多行"""
        # 從text中獲取所有行
        lines = self.text.get(1.0, tk.END)
        row_number = 2
        # 分隔成每行
        for line in lines.splitlines():
            if line == '':
                break
            print(line)
            self.new_line(line, row_number)
            row_number += 1

        # total彙總行
        self.label_file = tk.Label(self.frm2, text='total', relief='groove', borderwidth='2', width=25)
        self.label_file.grid(row=row_number, column=0)
        self.label_case = tk.Label(self.frm2, text=self.total_cases, relief='groove', borderwidth='2', width=10)
        self.label_case.grid(row=row_number, column=1)

        self.label_pass = tk.Label(self.frm2, text=self.total_success, relief='groove', borderwidth='2',
                                   width=10)
        self.label_pass.grid(row=row_number, column=2)
        self.label_fail = tk.Label(self.frm2, text=self.total_fail, relief='groove', borderwidth='2', width=10)
        self.label_fail.grid(row=row_number, column=3)
        self.label_block = tk.Label(self.frm2, text=self.total_block, relief='groove', borderwidth='2',
                                    width=10)
        self.label_block.grid(row=row_number, column=4)

        self.label_case_priority = tk.Label(self.frm2, text=self.toal_case_priority, relief='groove',
                                            borderwidth='2',
                                            width=10)
        self.label_case_priority.grid(row=row_number, column=5)

    def upload_files(self):
        """上傳多個檔案,並插入text中"""
        select_files = tk.filedialog.askopenfilenames(title=u"可選擇1個或多個檔案")
        for file in select_files:
            self.text.insert(tk.END, file + '\n')
            self.text.update()


if __name__ == '__main__':
    r = tk.Tk()
    ParseXmind(r)
    r.mainloop()

 

二、工具使用說明

1、xmind檔案中使用下列圖示進行分類標識:

標記表示p0級別case:數位1

標記表示執行通過case:綠色√

標記表示執行失敗case:紅色×

標記表示執行阻塞case:橙色!

2、執行程式碼

3、在彈框內【上傳xmind檔案】按鈕

4、在彈框內【開始統計】按鈕
 

三、實現效果