Python的click库做命令行工具
- 需求是MeterSphere测试计划状态是已完成/已结束,测试进度不是100%。
- 排查发现是test_plan_test_case表中已取消关联的用例算在了测试用例总数导致的
- 所以做了一个命令行工具方便其他人处理该问题
python click库常用函数详解_click函数-CSDN博客
python Click库知识点汇总_python click.choice-CSDN博客
from pprint import pprint
import click
import pymysql
class PostgresContext:
def __init__(self):
self.conn = pymysql.connect(host='',
port=3307,
user='root',
password='',
database='metersphere')
# 防止报 UnicodeDecodeError 错误
self.cursor = self.conn.cursor()
def __enter__(self):
return self.cursor, self.conn
def __exit__(self, exc_type, exc_val, exc_tb):
self.cursor.close()
self.conn.close()
class Options:
def __int__(self):
pass
def execute_sql(self, sql):
with PostgresContext() as pc:
cursor, conn = pc
print(sql)
cursor.execute(sql)
conn.commit()
def select_sql(self, sql):
with PostgresContext() as pc:
cursor, conn = pc
print(sql)
cursor.execute(sql)
return cursor.fetchall()
def find_prepare_case(self, test_plan_name):
sql = f"select num, name from test_case where id in (select case_id from test_plan_test_case where plan_id = (select id from test_plan where name = '{test_plan_name}') and status = 'Prepare')"
pprint(self.select_sql(sql))
def delete_prepare_case_by_id(self, test_plan_name, case_num):
try:
sql = f"delete from test_plan_test_case where plan_id = (select id from test_plan where name = '{test_plan_name}') and status = 'Prepare' and case_id = (select id from test_case where num = '{case_num}')"
self.execute_sql(sql)
print('删除成功')
except Exception as e:
print(e)
def delete_prepare_case_all(self, test_plan_name):
try:
sql = f"delete from test_plan_test_case where plan_id = (select id from test_plan where name = '{test_plan_name}') and status = 'Prepare'"
self.execute_sql(sql)
print('删除成功')
except Exception as e:
print(e)
@click.command()
@click.option('-p', default='default', help='输入测试计划名称,显示测试计划里所有状态是Prepare的用例')
@click.option('-o', default='one', help='根据测试用例id删除测试计划里状态是Prepare的用例')
@click.option('-a', default='two', help='删除测试计划里所有状态是Prepare的用例')
def test_plan_case(p, o, a):
option = Options()
if p != 'default' and o == 'one' and a == 'two':
option.find_prepare_case(p)
if p != 'default' and o != 'one':
option.delete_prepare_case_by_id(p, o)
if p != 'default' and a != 'two':
option.delete_prepare_case_all(p)
# yes parameters
def abort_if_false(ctx, param, value):
if not value:
ctx.abort()
if __name__ == '__main__':
test_plan_case()
- 打包 pyinstaller.exe .\test_plan_case.py 会生成_internal文件夹,需要的一些库和文件会放在这个文件夹里
- pyinstaller.exe --onefile .\test_plan_case.py 所以用这个命令
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)