Skip to content

应用状态测试 tests/state/test_app.py ​

验证 objection.state.app.app_state 单例:默认调试钩子标志、should_debug_hooks 判断、命令历史添加与清空。

📋 模块概览 ​

项目值
文件路径tests/state/test_app.py
被测对象objection.state.app.app_state
用例数4
框架pytest + unittest

🎯 测试意图 ​

  • 确认 should_debug_hooks() 默认返回 False。
  • 确认设置 debug_hooks=True 后 should_debug_hooks() 返回 True。
  • 确认 add_command_to_history 追加命令到 successful_commands。
  • 确认 clear_command_history 清空列表。tearDown 复位状态。

🧪 用例清单 ​

用例行号验证点
test_app_should_not_debug_hooks_by_default11默认 False
test_app_should_debug_hooks_if_true14置 True 后返回 True
test_adds_command_to_history19追加 1 条且内容正确
test_clears_command_history25清空后长度为 0

⚙️ 测试手法 ​

直接操作 app_state 单例属性并调用方法,断言布尔/长度/内容。无 mock,无 capture。tearDown 强制复位 debug_hooks=False、successful_commands=[] 隔离用例。

关键代码 tests/state/test_app.py:19:

python
def test_adds_command_to_history(self):
    app_state.add_command_to_history('foo')
    self.assertEqual(len(app_state.successful_commands), 1)
    self.assertEqual(app_state.successful_commands[0], 'foo')

🔍 源码索引 ​

用例位置
test_app_should_not_debug_hooks_by_defaulttests/state/test_app.py:11
test_app_should_debug_hooks_if_truetests/state/test_app.py:14
test_adds_command_to_historytests/state/test_app.py:19
test_clears_command_historytests/state/test_app.py:25

🔗 相关文档 ​

基于 GPL-3.0-or-later 许可发布