數據庫操作包,引用前面創建的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 datetimefrom 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:# 如果用例名稱解析出多個數字,輸入2matching_files.append((path, '2'))# 打印符合條件的文件列表return matching_filesdef 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 INTEGERprimary key autoincrement,filename VARCHAR(100),jira_num VARCHAR(20),updatetime TIMESTAMP,createtime TIMESTAMP,status INTEGER default 0
);