添加PyQt GUI自动化测试套件
新增5个GUI测试模块,覆盖所有主要功能: - test_login_gui.py: 登录和密码管理测试(7个测试) - test_stock_gui.py: 库存管理测试(4个测试) - test_raw_material_gui.py: 原料管理测试(7个测试) - test_garment_gui.py: 款式管理测试(2个测试) - test_purchase_order_gui.py: 采购单生成测试(2个测试) 测试特点: - 真实GUI交互测试(填写表单、点击按钮、搜索过滤) - 业务逻辑验证(重复数据拒绝、空值验证、计算正确性) - 独立测试环境(临时数据库,自动清理) - 自动化消息框(Mock QMessageBox) 总计22个GUI测试,全部通过 ✓ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
163
test/test_login_gui.py
Normal file
163
test/test_login_gui.py
Normal file
@@ -0,0 +1,163 @@
|
||||
"""
|
||||
登录对话框GUI测试模块
|
||||
使用PyQt测试框架测试登录和密码管理功能
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from PyQt5.QtWidgets import QApplication, QMessageBox, QInputDialog
|
||||
from PyQt5.QtTest import QTest
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
# 添加父目录到路径以导入模块
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from database import DatabaseManager
|
||||
from login_dialog import LoginDialog
|
||||
|
||||
|
||||
class TestLoginGUI(unittest.TestCase):
|
||||
"""登录对话框GUI测试类"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""测试类初始化:创建QApplication实例"""
|
||||
cls.app = QApplication.instance()
|
||||
if cls.app is None:
|
||||
cls.app = QApplication(sys.argv)
|
||||
|
||||
def setUp(self):
|
||||
"""每个测试前准备:创建临时数据库和对话框"""
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
self.db_path = os.path.join(self.temp_dir, "test_fabric.db")
|
||||
self.db_manager = DatabaseManager(self.db_path)
|
||||
|
||||
# 创建对话框实例
|
||||
self.dialog = LoginDialog(self.db_path)
|
||||
|
||||
# 保存原始消息框
|
||||
self._original_msgbox = QMessageBox.information
|
||||
self._original_warning = QMessageBox.warning
|
||||
self._original_critical = QMessageBox.critical
|
||||
|
||||
# Mock消息框
|
||||
QMessageBox.information = lambda *args, **kwargs: None
|
||||
QMessageBox.warning = lambda *args, **kwargs: None
|
||||
QMessageBox.critical = lambda *args, **kwargs: None
|
||||
|
||||
def tearDown(self):
|
||||
"""每个测试后清理"""
|
||||
# 恢复消息框
|
||||
QMessageBox.information = self._original_msgbox
|
||||
QMessageBox.warning = self._original_warning
|
||||
QMessageBox.critical = self._original_critical
|
||||
|
||||
# 关闭对话框
|
||||
self.dialog.close()
|
||||
self.dialog.deleteLater()
|
||||
|
||||
# 清理数据库
|
||||
import gc
|
||||
gc.collect()
|
||||
try:
|
||||
if os.path.exists(self.db_path):
|
||||
os.remove(self.db_path)
|
||||
if os.path.exists(self.temp_dir):
|
||||
os.rmdir(self.temp_dir)
|
||||
except:
|
||||
pass
|
||||
|
||||
# ========== 登录功能测试 ==========
|
||||
|
||||
def test_admin_login_success(self):
|
||||
"""测试管理员登录成功"""
|
||||
# 输入正确的管理员密码(默认123456)
|
||||
self.dialog.admin_input.setText("123456")
|
||||
|
||||
# 调用登录方法
|
||||
self.dialog.login_mode(True)
|
||||
|
||||
# 验证登录成功
|
||||
self.assertTrue(self.dialog.is_admin, "应该设置为管理员模式")
|
||||
|
||||
def test_admin_login_failure(self):
|
||||
"""测试管理员登录失败"""
|
||||
warning_called = []
|
||||
def mock_warning(*args, **kwargs):
|
||||
warning_called.append(args)
|
||||
QMessageBox.warning = mock_warning
|
||||
|
||||
# 输入错误的密码
|
||||
self.dialog.admin_input.setText("wrong_password")
|
||||
|
||||
# 调用登录方法
|
||||
self.dialog.login_mode(True)
|
||||
|
||||
# 验证登录失败
|
||||
self.assertFalse(self.dialog.is_admin, "不应该设置为管理员模式")
|
||||
self.assertTrue(len(warning_called) > 0, "应该显示警告消息")
|
||||
|
||||
def test_user_login_success(self):
|
||||
"""测试普通用户登录成功"""
|
||||
# 输入正确的用户密码(默认123456)
|
||||
self.dialog.user_input.setText("123456")
|
||||
|
||||
# 调用登录方法
|
||||
self.dialog.login_mode(False)
|
||||
|
||||
# 验证登录成功(is_admin应该为False)
|
||||
self.assertFalse(self.dialog.is_admin, "应该是普通用户模式")
|
||||
|
||||
def test_user_login_failure(self):
|
||||
"""测试普通用户登录失败"""
|
||||
warning_called = []
|
||||
def mock_warning(*args, **kwargs):
|
||||
warning_called.append(args)
|
||||
QMessageBox.warning = mock_warning
|
||||
|
||||
# 输入错误的密码
|
||||
self.dialog.user_input.setText("wrong_password")
|
||||
|
||||
# 调用登录方法
|
||||
self.dialog.login_mode(False)
|
||||
|
||||
# 验证登录失败
|
||||
self.assertTrue(len(warning_called) > 0, "应该显示警告消息")
|
||||
|
||||
# ========== 密码管理测试 ==========
|
||||
|
||||
def test_get_default_password(self):
|
||||
"""测试获取默认密码"""
|
||||
admin_pwd = self.dialog.get_stored_password("admin")
|
||||
user_pwd = self.dialog.get_stored_password("user")
|
||||
|
||||
self.assertEqual(admin_pwd, "123456", "默认管理员密码应该是123456")
|
||||
self.assertEqual(user_pwd, "123456", "默认用户密码应该是123456")
|
||||
|
||||
def test_set_password(self):
|
||||
"""测试设置新密码"""
|
||||
# 设置新的管理员密码
|
||||
result = self.dialog.set_password("admin", "new_admin_pass")
|
||||
self.assertTrue(result, "设置密码应该成功")
|
||||
|
||||
# 验证密码已更新
|
||||
stored_pwd = self.dialog.get_stored_password("admin")
|
||||
self.assertEqual(stored_pwd, "new_admin_pass", "密码应该被更新")
|
||||
|
||||
def test_login_with_new_password(self):
|
||||
"""测试使用新密码登录"""
|
||||
# 设置新密码
|
||||
self.dialog.set_password("admin", "newpass123")
|
||||
|
||||
# 使用新密码登录
|
||||
self.dialog.admin_input.setText("newpass123")
|
||||
self.dialog.login_mode(True)
|
||||
|
||||
# 验证登录成功
|
||||
self.assertTrue(self.dialog.is_admin, "应该使用新密码登录成功")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user