first commit

This commit is contained in:
jianxin committed 2025-04-10 11:24:02 +08:00
1 parent d2b2284bd4
commit 92b1584aaa
10 files changed
+142 -13

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
Binary file not shown.
+14
View File
@@ -0,0 +1,14 @@
import logging
import pytest
# 在测试开始前配置日志
@pytest.fixture(autouse=True, scope="session")
def configure_logging():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)",
datefmt="%Y-%m-%d %H:%M:%S",
)
# 返回一个日志对象供测试函数使用
return logging.getLogger()
+13
View File
@@ -0,0 +1,13 @@
import sys
import os
sys.path.append(
os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src")
)
from ziniao import Ziniao
if __name__ == "__main__":
ziniao = Ziniao()
ziniao.launch_ziniao()
+11
View File
@@ -0,0 +1,11 @@
[pytest]
# 显示日志输出
log_cli = True
# 设置日志级别
log_cli_level = INFO
# 日志格式
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
# 时间戳格式
log_cli_date_format = %Y-%m-%d %H:%M:%S
# 捕获stdout/stderr输出
addopts = -v --capture=tee-sys
Binary file not shown.
+60 -12
View File
@@ -1,7 +1,13 @@
import os
import time
import winreg import winreg
import win32gui
import subprocess
class Ziniao: class Ziniao:
def __init__(self, name): def __init__(self):
pass pass
def login(self, username, password): def login(self, username, password):
@@ -16,22 +22,64 @@ class Ziniao:
def close(self): def close(self):
pass pass
def launch_ziniao(self):
"""启动紫鸟并等待窗口出现"""
exe_path = self.get_ziniao_exe_path()
# 启动紫鸟的exe文件, 独立进程
subprocess.Popen(
exe_path, creationflags=0x01000000 | 0x00000008, close_fds=True
)
# 延迟导入win32gui以避免在测试收集阶段崩溃
try:
# 等待窗口出现
max_wait_time = 60 # 最大等待时间(秒)
start_time = time.time()
print("等待紫鸟窗口出现...")
# 延迟导入
# 使用win32gui查找窗口
def enum_windows_callback(hwnd, found_windows):
if win32gui.IsWindowVisible(hwnd):
window_title = win32gui.GetWindowText(hwnd)
if "紫鸟" in window_title:
found_windows.append(hwnd)
return True
while time.time() - start_time < max_wait_time:
found_windows = []
win32gui.EnumWindows(enum_windows_callback, found_windows)
if found_windows:
print("紫鸟窗口已出现")
return
time.sleep(1) # 每秒检查一次
if time.time() - start_time >= max_wait_time:
print("紫鸟窗口未在规定时间内出现")
except ImportError:
print("警告: 未安装win32gui模块,无法检测紫鸟窗口出现")
except Exception as e:
print(f"检测窗口出错: {e}")
def get_ziniao_exe_path(self): def get_ziniao_exe_path(self):
""" """返回紫鸟的安装路径"""
Returns:
str: The DisplayIcon value if found, None otherwise
"""
try: try:
# Open the registry key # Open the registry key
reg_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\8c327052-d9e8-5248-931f-e5b8c47f82b5" reg_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\8c327052-d9e8-5248-931f-e5b8c47f82b5"
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path) key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path)
# Query the DisplayIcon value # Query the UninstallString value
display_icon, _ = winreg.QueryValueEx(key, "DisplayIcon") uninstall_string, _ = winreg.QueryValueEx(key, "UninstallString")
winreg.CloseKey(key) winreg.CloseKey(key)
return display_icon uninstall_exe_path = uninstall_string.rsplit(" ", 1)[0].strip('"')
except FileNotFoundError: os.path.split(uninstall_exe_path)[0] # 获取路径部分
return "Registry key not found" exe_path = os.path.join(os.path.split(uninstall_exe_path)[0], "ziniao.exe")
except WindowsError as e:
return f"Error accessing registry: {e}" return exe_path
except Exception as e:
raise RuntimeError(f"获取紫鸟EXE路径失败: {e}") from e
+37
View File
@@ -0,0 +1,37 @@
import os
import pytest
import logging
from src.ziniao import Ziniao
@pytest.fixture
def ziniao_instance():
"""创建并返回一个Ziniao实例,可以被多个测试函数重复使用"""
return Ziniao()
def test_get_ziniao_exe_path(ziniao_instance):
"""测试get_ziniao_exe_path方法是否返回合法的可执行文件路径"""
exe_path = ziniao_instance.get_ziniao_exe_path()
# 使用pytest的日志功能替代print
logging.info(f"紫鸟EXE路径: {exe_path}")
assert isinstance(exe_path, str)
assert len(exe_path) > 0
# 验证路径是否存在(如果此测试在未安装紫鸟的环境中可能会失败)
assert os.path.exists(exe_path)
def test_launch_ziniao(ziniao_instance):
"""测试launch_ziniao方法是否能成功启动紫鸟"""
try:
ziniao_instance.launch_ziniao()
logging.info("紫鸟启动成功")
except Exception as e:
pytest.fail(f"启动紫鸟失败: {e}")
if __name__ == "__main__":
# 保留原来的手动测试代码,但仅在直接运行此文件时执行
ziniao_instance = Ziniao()
print(f"紫鸟EXE路径: {ziniao_instance.get_ziniao_exe_path()}")
-1
View File
@@ -1 +0,0 @@