python pytest脚本执行工具

pytest脚本执行工具

支持获取当前路径下所有.py脚本

添加多个脚本,一起执行


import tkinter as tk
from tkinter import filedialog
import subprocess
import os
from datetime import datetime

def select_script():
    script_path = filedialog.askopenfilename(filetypes=[("Python files", "*.py")])
    script_entry.delete(0, tk.END)
    script_entry.insert(tk.END, script_path)


def add_script():
    script_path = script_listbox.get(tk.ACTIVE)
    selected_scripts_listbox.insert(tk.END, script_path)


def delete_script():
    selected_index = selected_scripts_listbox.curselection()
    if selected_index:
        selected_scripts_listbox.delete(selected_index)


def run_tests():
    selected_scripts = selected_scripts_listbox.get(0, tk.END)
    for script_path in selected_scripts:
        result = subprocess.run(["pytest", "-q", script_path], capture_output=True, text=True)
        # 输出执行结果倒序
        output_text.insert(1.0, result.stdout)
        output_text.insert(1.0, f"--- {script_path} --- ###执行详情### \n", "red")
        output_text.insert(1.0, "\n")

        # 获取系统时间,输出执行结果文本框
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
        output_text.insert(1.0, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
        output_text.insert(1.0, f">>>>>>>>>>>>>>>>>>>>>>>>>  {script_path} @ 脚本执行时间:{current_time} <<<<<<<<<<<<<<<<<<<<<<<<<\n")
        output_text.insert(1.0, "\n")
        output_text.insert(1.0, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        output_text.insert(1.0, "\n")
        output_text.insert(1.0, "\n")


# 清空执行结果文本框
def clear_output():
    output_text.delete(1.0, tk.END)


# 清空已选脚本列表
def clear_selected_scripts():
    selected_scripts_listbox.delete(0, tk.END)

# 创建主窗口
window = tk.Tk()
window.title("Test Runner")

window.geometry('800x800')

window.resizable(0, 0)  # 防止用户调整尺寸

# 创建框架容器
frame = tk.Frame(window)
frame.pack()

# 获取当前目录下所有的.py文件
script_files = [file for file in os.listdir() if file.endswith(".py")]

# 创建脚本选择列表框
script_listbox = tk.Listbox(frame, width=30, height=10)
script_listbox.pack(side=tk.LEFT, padx=0, pady=10)


# 将.py文件添加到选择列表框中
for file in script_files:
    script_listbox.insert(tk.END, file)

# 创建已选脚本列表框
selected_scripts_listbox = tk.Listbox(frame, width=30, height=10)
selected_scripts_listbox.pack(side=tk.LEFT, padx=0, pady=10)


# 创建添加脚本按钮
add_button = tk.Button(frame, text="添加脚本", command=add_script)
add_button.pack(side=tk.LEFT, padx=5)

# 创建删除脚本按钮
delete_button = tk.Button(frame, text="删除脚本", command=delete_script)
delete_button.pack(side=tk.LEFT, padx=5)

# 创建执行脚本按钮
run_button = tk.Button(frame, text="执行脚本", command=run_tests)
run_button.pack(side=tk.LEFT, padx=5)

# 创建清空输出按钮
clear_output_button = tk.Button(frame, text="清空输出", command=clear_output)
clear_output_button.pack(side=tk.LEFT, padx=5)

# 创建清空已选列表按钮
clear_button = tk.Button(frame, text="清空已选列表", command=clear_selected_scripts)
clear_button.pack(side=tk.LEFT, padx=5)


# 创建滚动条
scrollbar = tk.Scrollbar(window)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

# 创建文本框用于显示执行结果
output_text = tk.Text(window, height=50, width=110)
output_text.pack()

# 将滚动条与文本框绑定
output_text.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=output_text.yview)

# 运行主循环
window.mainloop()

python pytest脚本执行工具_第1张图片

已打包上传资源 

 (36条消息) 【免费】可以选择.py脚本,以pytest执行脚本资源-CSDN文库

你可能感兴趣的:(#,python,#,pytest测试框架,python)