#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import os # 添加src目录到Python路径 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src')) from word_main_window import WordStyleMainWindow from PyQt5.QtWidgets import QApplication, QMessageBox from unittest.mock import patch def test_insert_weather_without_location(): """测试在未定位天气时插入天气信息""" app = QApplication.instance() if app is None: app = QApplication(sys.argv) # 创建主窗口实例 window = WordStyleMainWindow() # 模拟没有定位天气的情况(删除current_weather_data属性) if hasattr(window, 'current_weather_data'): delattr(window, 'current_weather_data') # 模拟用户点击插入天气信息按钮 with patch('PyQt5.QtWidgets.QMessageBox.information') as mock_info: window.insert_weather_info() # 验证是否弹出了"先定位天气"对话框 mock_info.assert_called_once_with(window, "附加工具", "先定位天气") print("测试通过:未定位天气时正确弹出提示对话框") def test_insert_weather_with_location(): """测试在已定位天气时插入天气信息""" app = QApplication.instance() if app is None: app = QApplication(sys.argv) # 创建主窗口实例 window = WordStyleMainWindow() # 模拟已定位天气的情况 window.current_weather_data = { 'city': '北京', 'temperature': 25, 'description': '晴天' } # 模拟用户点击插入天气信息按钮 # 注意:这个测试不会真正插入文本,因为我们没有设置完整的UI环境 window.insert_weather_info() print("测试通过:已定位天气时正确执行插入操作") if __name__ == "__main__": test_insert_weather_without_location() test_insert_weather_with_location() print("所有测试完成!")