当前位置: 首页 > news >正文

【pytest】获取所有用例名称并存于数据库

数据库操作包,引用前面创建的py文件,【sqlite】python操作sqlite3(含测试)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025-02-11 8:45
# @Author  : duxiaowei
# @File    : get_filename.py
# @Software: 这个文件是 : 将编写的pytest自动测试用例,中所有用例路径名,存储到pytest_ready表中


import os
import re
from datetime import datetime

from com.connect_sqllite import DBlite

"""
1. 获取所有用例名称
2. 将所有用例名称存到表pytest_ready表
"""
# 获取所有用例名称
def 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, "")
                # 洗出来,文件名中jira号,如果没有jira号,输入''
                numbers = re.findall(r'\d+', path)
                if len(numbers) == 0:
                    # # 如果用例名称解析不出数组,输入''
                    matching_files.append((path, ''))
                elif len(numbers) == 1:
                    matching_files.append((path, numbers[0]))
                else:
                    # 如果用例名称解析出多个数字,输入2
                    matching_files.append((path, '2'))
    # 打印符合条件的文件列表
    return matching_files

def insert_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 = ?"
        arg = (jira[0],)
        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) values (?,?,?)"
            arg = (jira[0], jira[1], current_time)
            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=?"
            arg = (current_time, jira[0])
            DBlite().change(sql_update, arg)
#
# 获取所有用例名称
# 插入数据库,pytest_ready
# insert_pytest_ready()




建表语句

create table pytest_ready_dg_tmp
(
	id INTEGER
		primary key autoincrement,
	filename VARCHAR(100),
	jira_num VARCHAR(20),
	updatetime TIMESTAMP,
	createtime TIMESTAMP,
	status INTEGER default 0
);

相关文章:

  • 联想电脑如何进入BIOS?
  • 新数据结构(9)——Java异常体系
  • AI编程01-生成前/后端接口对表-豆包(或Deepseek+WPS的AI
  • 【Vue3 入门到实战】15. 组件间通信
  • CEF132 编译指南 Linux 篇 - 构建编译环境:工具链配置(二)
  • DatePicker 实现:日期范围截止时间为23:59:59
  • 蓝桥杯备赛笔记(二)
  • 番外04:前端面试八股文-HTML
  • QML 快捷键与Shortcut的使用
  • 一个简单的Windows TCP服务器实现
  • windows系统 从 Hugging Face网站上使用 huggingface-cli 命令下载AI大模型到本地
  • 在npm上传属于自己的包
  • ARM64 Trust Firmware [一]
  • 【Copilot】Redis SCAN SSCAN
  • Linux探秘坊-------4.进度条小程序
  • DeepSeek 突然来袭,AI 大模型变革的危机与转机藏在哪?
  • ES6 Proxy 用法总结以及 Object.defineProperty用法区别
  • 【redis】数据类型之bitmaps
  • 【H5自适应】高端科技类pbootcms网站模板 – 三级栏目、下载与招聘功能支持
  • Python自动化办公之Excel拆分
  • 什么网站做任务可以赚钱/你就知道
  • 中国建设银行联行号查询网站/最新疫情最新消息
  • 全国网站备案/昆明网络推广方式有哪些
  • webform网站开发经历/网站怎样优化seo
  • 怎么建设一个外国网站/抖音seo是什么
  • 设计网站公司的口号/知乎关键词排名