【python】连接Jira获取token以及jira对象
【python】解析自动化脚本文件并按照=测试周期=存储记录
【python】向Jira推送自动化用例执行成功
【python】向Jira测试计划下,附件中增加html测试报告
将已编写的自动化测试用例按照jira号解析出来,并按照测试计划,测试周期,存储到pytest_ready表中,方便接下来调用使用
python">#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2025-02-12 8:45
# @Author : duxiaowei
# @File : save_case_name_db.py
# @Software: 将编写的pytest自动测试用例,中所有用例路径名,存储到pytest_ready表中import os
from datetime import datetimefrom com.connect_sqllite import DBlitedef find_int_convertible(lst):'''从 一个 字符串list中,找到可以转换成int的值有哪些'''def is_int_convertible(item):try:int(item)return Trueexcept ValueError:return Falsereturn list(map(int, filter(is_int_convertible, lst)))def parse_filename(filename):"""解析pytest文件名,中的jira号"""name_list = filename.split('&')# print(name_list)jira_num_list = []if len(name_list) == 1:l_list = name_list[0].split("_")f_list = find_int_convertible(l_list)jira_num_list.append(str(f_list[0]))return jira_num_listelse:first = name_list[0].split("_")[-1]jira_num_list.append(first)# 再对最后一个处理last = name_list[-1].split("_")[0]jira_num_list.append(last)# 删除第一个跟最后一个元素,重新拼接name_list.pop(0)name_list.pop()jira_num_list.extend(name_list)return jira_num_listdef get_test_files(current_dir):""" 获取所有用例名称,"""matching_files = []# 遍历当前目录下的所有文件和文件夹for root, dirs, files in os.walk(current_dir):for file in files:# 检查文件是否以 .py 结尾,并且以 test_ 开头或以 _test 结尾if file.endswith('.py') and (file.startswith('test_') or file.endswith('_test.py')):# 文件名path = os.path.join(root, file).replace(current_dir, "")# print(path)name_list = parse_filename(path)for i in name_list:p_list = []p_list.append(path)p_list.append(i)matching_files.append(p_list)return matching_filesdef insert_pytest_ready(cycle_id, plan_id):'''将本项目中,所有用例【所有人,已经编写的自动化测试用例】名称存到表pytest_ready表'''folder_path = os.path.abspath('..')test_files = get_test_files(folder_path)for jira in test_files:# 先查询是否有记录sql_select = "select count(1) from pytest_ready where filename = ? and jira_num= ? and cycle_id=? and plan_num=?"arg = (jira[0], jira[1], cycle_id, plan_id)result = DBlite().select(sql_select, arg)if result[0][0] == 0:current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")sql_insert = "insert into pytest_ready(filename, jira_num, createtime, cycle_id, plan_num) values (?,?,?,?,?)"arg = (jira[0], jira[1], current_time, cycle_id, plan_id)DBlite().change(sql_insert, arg)else:current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")sql_update = "update pytest_ready set updatetime=? where filename=? and jira_num= ? and cycle_id=? and plan_num=?"arg = (current_time, jira[0], jira[1], cycle_id, plan_id)DBlite().change(sql_update, arg)