You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Curriculum_Design/test_weather_insert.py

60 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/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("所有测试完成!")