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

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

python 复制代码
#!/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()

建表语句

sql 复制代码
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
);
相关推荐
莲动渔舟14 小时前
pytest测试专题 - 1.1 运行pytest
笔记·学习·pytest
不亭1 天前
python自动化测试之Pytest框架之YAML详解以及Parametrize数据驱动!
pytest
不亭1 天前
python自动化测试之Pytest断言及Allure报告定制
开发语言·python·pytest
知识倒进我脑里2 天前
pytest生成报告no tests ran in 0.01s
pytest
开源优测3 天前
详解在Pytest中忽略测试目录的三种方法
pytest
明月与玄武5 天前
pytest-xdist 进行多进程并发测试
pytest·pytest-xdist·进行多进程并发测试
程序员小远6 天前
接口自动化测试框架(pytest+allure+aiohttp+ 用例自动生成)
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
小码哥说测试6 天前
pytest-xdist 进行多进程并发测试!
软件测试·网络协议·selenium·单元测试·pytest·压力测试·postman
天才测试猿7 天前
Pytest+selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest