如上文所說碌廓,批量執(zhí)行指定文件机蔗。那么問題來了,指定文件怎么來的呢魔策?
需要可以勾選文件的圖形工具匈子,如圖:
# -*- coding: utf-8 -*-
# @Time : 2020/12/1 17:28
# @Author : Lee
# @File : case_tool.py
import os
import re
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtCore
from Tools.constants import base_dir
from PyQt5.QtCore import QCoreApplication
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setup_ui(self, main_window):
main_window.setObjectName("MainWindow")
main_window.resize(618, 594)
self.centralwidget = QtWidgets.QWidget(main_window)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
self.treeWidget.setObjectName("treeWidget")
self.treeWidget.headerItem().setText(0, "1")
self.verticalLayout.addWidget(self.treeWidget)
self.reset_btn = QtWidgets.QPushButton(self)
self.reset_btn.setText("重置")
self.check_btn = QtWidgets.QPushButton(self)
self.check_btn.setText("確定")
self.layout = QtWidgets.QHBoxLayout()
self.layout.addStretch(1)
self.layout.addWidget(self.reset_btn)
self.layout.addWidget(self.check_btn)
self.verticalLayout.addLayout(self.layout)
main_window.setCentralWidget(self.centralwidget)
self.retranslate_ui(main_window)
QtCore.QMetaObject.connectSlotsByName(main_window)
def retranslate_ui(self, main_window):
_translate = QtCore.QCoreApplication.translate
main_window.setWindowTitle(_translate("main_window", "壓力測(cè)試腳本選擇工具"))
def get_case_file_name(case_directory, locust_case_dict):
case_file_list = list()
if not os.path.isdir(case_directory):
return case_file_list
for index, child_file in enumerate(os.listdir(case_directory)):
if os.path.isdir(os.path.join(case_directory, child_file)):
if child_file != "__pycache__":
locust_case_dict[child_file] = dict()
get_case_file_name(os.path.join(case_directory, child_file), locust_case_dict[child_file])
else:
ret = re.match(r"^load_.*.py$", child_file)
if ret:
case_file_list.append({ret.group(): os.path.join(case_directory, ret.group())})
if case_file_list:
locust_case_dict["cases_path"] = case_file_list
return locust_case_dict
class CaseTreeView(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(CaseTreeView, self).__init__()
self.case_data_dict = get_case_file_name(os.path.join(base_dir, "TestCases"), {})
self.setup_ui(self)
self.AllTestCase = None
self.selected_cases = list()
self.init_ui()
def init_ui(self):
# 設(shè)置列數(shù)
self.treeWidget.setColumnCount(1)
# 設(shè)置樹形控件頭部的標(biāo)題
self.treeWidget.setHeaderLabels(['勾選測(cè)試腳本'])
self.treeWidget.setColumnWidth(0, 120)
# 設(shè)置根節(jié)點(diǎn)
self.AllTestCase = QTreeWidgetItem(self.treeWidget)
self.AllTestCase.setText(0, 'TestCases')
self.AllTestCase.setCheckState(0, Qt.Unchecked)
self.set_up_tree_items(self.case_data_dict, self.AllTestCase)
self.reset_btn.clicked.connect(self.reset_button_click)
self.check_btn.clicked.connect(self.selected_button_click)
@staticmethod
def set_widget_item(item_name, parent_widget_item):
widget_item = QTreeWidgetItem(parent_widget_item)
widget_item.setText(0, item_name)
widget_item.setCheckState(0, Qt.Unchecked)
return widget_item
def set_up_tree_items(self, case_data_dict, parent_widget_item):
for key in case_data_dict.keys():
if key == "cases_path":
for case_item in case_data_dict[key]:
for case_name in case_item.keys():
self.set_widget_item(case_name, parent_widget_item)
else:
widget_item = self.set_widget_item(key, parent_widget_item)
self.set_up_tree_items(case_data_dict[key], widget_item)
# 節(jié)點(diǎn)全部展開
self.treeWidget.expandAll()
# 鏈接槽函數(shù)
self.treeWidget.itemChanged.connect(self.handlechanged)
def handlechanged(self, item, column):
# 獲取選中節(jié)點(diǎn)的子節(jié)點(diǎn)個(gè)數(shù)
count = item.childCount()
# 如果被選中
if item.checkState(column) == Qt.Checked:
# 連同下面子子節(jié)點(diǎn)全部設(shè)置為選中狀態(tài)
for f in range(count):
if item.child(f).checkState(0) != Qt.Checked:
item.child(f).setCheckState(0, Qt.Checked)
# 如果取消選中
if item.checkState(column) == Qt.Unchecked:
# 連同下面子子節(jié)點(diǎn)全部設(shè)置為取消選中狀態(tài)
for f in range(count):
if item.child(f).checkState != Qt.Unchecked:
item.child(f).setCheckState(0, Qt.Unchecked)
def un_select_all_items(self, item):
if item.childCount():
for index in range(item.childCount()):
item.child(index).setCheckState(0, Qt.Unchecked)
if item.child(index).childCount():
self.un_select_all_items(item.child(index))
def reset_button_click(self):
self.AllTestCase.setCheckState(0, Qt.Unchecked)
self.un_select_all_items(self.AllTestCase)
self.selected_cases = list()
def get_selected_item(self, item, item_name_dict):
if item.childCount():
for index in range(item.childCount()):
if item.child(index).childCount():
self.get_selected_item(item.child(index), item_name_dict[item.child(index).text(0)])
else:
if item.child(index).checkState(0) == Qt.Checked:
self.selected_cases.append(item_name_dict["cases_path"][index][item.child(index).text(0)])
def selected_button_click(self):
self.selected_cases = list()
self.get_selected_item(self.AllTestCase, self.case_data_dict)
with open(os.path.join(base_dir, "case_file.txt"), mode='w') as file:
for index, case_name in enumerate(self.selected_cases):
file.write(case_name.split(base_dir)[1] + "\r")
QCoreApplication.instance().quit()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
case_view = CaseTreeView()
case_view.show()
sys.exit(app.exec_())