美文网首页
Locust批量执行(windows辅助工具)

Locust批量执行(windows辅助工具)

作者: Lutous | 来源:发表于2021-01-27 16:23 被阅读0次

    如上文所说,批量执行指定文件。那么问题来了,指定文件怎么来的呢?

    需要可以勾选文件的图形工具,如图: select_tool.png
    # -*- 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", "压力测试脚本选择工具"))
    
    
    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):
            # 设置列数
            self.treeWidget.setColumnCount(1)
            # 设置树形控件头部的标题
            self.treeWidget.setHeaderLabels(['勾选测试脚本'])
            self.treeWidget.setColumnWidth(0, 120)
    
            # 设置根节点
            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)
    
            # 节点全部展开
            self.treeWidget.expandAll()
            # 链接槽函数
            self.treeWidget.itemChanged.connect(self.handlechanged)
    
        def handlechanged(self, item, column):
            # 获取选中节点的子节点个数
            count = item.childCount()
            # 如果被选中
            if item.checkState(column) == Qt.Checked:
                # 连同下面子子节点全部设置为选中状态
                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:
                # 连同下面子子节点全部设置为取消选中状态
                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_())
    
    

    相关文章

      网友评论

          本文标题:Locust批量执行(windows辅助工具)

          本文链接:https://www.haomeiwen.com/subject/sdydzktx.html