|
| 1 | +""" |
| 2 | +示例测试 - 展示增强版测试基类的用法 |
| 3 | +演示:Session ID 管理、智能等待、重试机制、测试数据管理 |
| 4 | +""" |
| 5 | + |
| 6 | +from tests.base_cli_test import BaseOpenClawCLITest |
| 7 | +from utils.test_utils import TestData |
| 8 | + |
| 9 | + |
| 10 | +class TestEnhancedFeatures(BaseOpenClawCLITest): |
| 11 | + """ |
| 12 | + 增强功能演示测试 |
| 13 | + 展示如何使用 Session ID 管理、智能等待、重试机制、测试数据管理 |
| 14 | + """ |
| 15 | + |
| 16 | + def test_auto_session_id(self): |
| 17 | + """ |
| 18 | + 演示:自动 Session ID 管理 |
| 19 | + - 每个测试方法自动获得唯一的 session_id |
| 20 | + - 通过 self.current_session_id 访问 |
| 21 | + """ |
| 22 | + self.logger.info(f"当前测试自动生成的 Session ID: {self.current_session_id}") |
| 23 | + |
| 24 | + message = "我叫测试用户,今年25岁" |
| 25 | + response = self.send_and_log(message) |
| 26 | + |
| 27 | + self.wait_for_sync() |
| 28 | + |
| 29 | + response2 = self.send_and_log("我是谁") |
| 30 | + self.assertAnyKeywordInResponse(response2, [["测试用户", "25岁"]]) |
| 31 | + |
| 32 | + def test_custom_session_id(self): |
| 33 | + """ |
| 34 | + 演示:自定义 Session ID |
| 35 | + - 使用 generate_unique_session_id() 生成自定义 session_id |
| 36 | + - 可以指定前缀 |
| 37 | + """ |
| 38 | + custom_session = self.generate_unique_session_id(prefix="custom_test") |
| 39 | + self.logger.info(f"自定义 Session ID: {custom_session}") |
| 40 | + |
| 41 | + message = "我喜欢吃苹果" |
| 42 | + response = self.send_and_log(message, session_id=custom_session) |
| 43 | + |
| 44 | + self.wait_for_sync() |
| 45 | + |
| 46 | + response2 = self.send_and_log("我喜欢吃什么", session_id=custom_session) |
| 47 | + self.assertAnyKeywordInResponse(response2, [["苹果"]]) |
| 48 | + |
| 49 | + def test_smart_wait(self): |
| 50 | + """ |
| 51 | + 演示:智能等待 |
| 52 | + - 使用 smart_wait_for_sync() 替代固定等待 |
| 53 | + - 轮询检查记忆是否同步完成 |
| 54 | + """ |
| 55 | + message = "我的爱好是打篮球和游泳" |
| 56 | + self.send_and_log(message) |
| 57 | + |
| 58 | + success = self.smart_wait_for_sync( |
| 59 | + check_message="我的爱好是什么", |
| 60 | + keywords=["篮球", "游泳"], |
| 61 | + timeout=30.0, |
| 62 | + poll_interval=2.0, |
| 63 | + ) |
| 64 | + |
| 65 | + self.assertTrue(success, "智能等待超时,记忆未同步") |
| 66 | + |
| 67 | + def test_retry_on_failure(self): |
| 68 | + """ |
| 69 | + 演示:重试机制 |
| 70 | + - 使用 send_with_retry() 在失败时自动重试 |
| 71 | + - 使用 send_and_log(retry_on_failure=True) 启用重试 |
| 72 | + """ |
| 73 | + message = "我在北京工作" |
| 74 | + |
| 75 | + response = self.send_with_retry( |
| 76 | + message, |
| 77 | + max_retries=3, |
| 78 | + ) |
| 79 | + |
| 80 | + self.wait_for_sync() |
| 81 | + |
| 82 | + response2 = self.send_and_log("我在哪里工作", retry_on_failure=True) |
| 83 | + self.assertAnyKeywordInResponse(response2, [["北京"]]) |
| 84 | + |
| 85 | + def test_data_driven_with_default_data(self): |
| 86 | + """ |
| 87 | + 演示:使用默认测试数据 |
| 88 | + - 使用 get_test_data() 获取预定义的测试数据 |
| 89 | + - 使用 run_with_test_data() 快速运行测试 |
| 90 | + """ |
| 91 | + _, query_response = self.run_with_test_data( |
| 92 | + data_name="user_xiaoming", |
| 93 | + query_message="我是谁,今年多大", |
| 94 | + ) |
| 95 | + |
| 96 | + self.assertIsNotNone(query_response) |
| 97 | + |
| 98 | + def test_data_driven_with_custom_data(self): |
| 99 | + """ |
| 100 | + 演示:使用自定义测试数据 |
| 101 | + - 创建 TestData 对象 |
| 102 | + - 注册到 data_manager |
| 103 | + """ |
| 104 | + custom_data = TestData( |
| 105 | + name="custom_user", |
| 106 | + description="自定义测试用户", |
| 107 | + input_data={ |
| 108 | + "message": "我叫自定义用户,职业是数据分析师", |
| 109 | + }, |
| 110 | + expected_keywords=[ |
| 111 | + ["自定义用户"], |
| 112 | + ["数据分析师"], |
| 113 | + ], |
| 114 | + tags=["custom", "user"], |
| 115 | + ) |
| 116 | + |
| 117 | + self.data_manager.register_data(custom_data) |
| 118 | + |
| 119 | + _, query_response = self.run_with_test_data( |
| 120 | + data_name="custom_user", |
| 121 | + query_message="我的职业是什么", |
| 122 | + ) |
| 123 | + |
| 124 | + self.assertIsNotNone(query_response) |
| 125 | + |
| 126 | + def test_combined_features(self): |
| 127 | + """ |
| 128 | + 演示:组合使用多个增强功能 |
| 129 | + - 自动 Session ID |
| 130 | + - 智能等待 |
| 131 | + - 重试机制 |
| 132 | + - 测试数据 |
| 133 | + """ |
| 134 | + data = self.get_test_data("fruit_cherry") |
| 135 | + self.assertIsNotNone(data, "测试数据不存在") |
| 136 | + |
| 137 | + message = data.input_data.get("message") |
| 138 | + self.send_and_log(message, retry_on_failure=True) |
| 139 | + |
| 140 | + success = self.smart_wait_for_sync( |
| 141 | + check_message="我喜欢吃什么水果", |
| 142 | + keywords=data.expected_keywords[0], |
| 143 | + timeout=30.0, |
| 144 | + ) |
| 145 | + |
| 146 | + self.assertTrue(success, "智能等待超时") |
| 147 | + |
| 148 | + |
| 149 | +class TestDataDrivenTests(BaseOpenClawCLITest): |
| 150 | + """ |
| 151 | + 数据驱动测试示例 |
| 152 | + 使用预定义的测试数据运行多个测试用例 |
| 153 | + """ |
| 154 | + |
| 155 | + def test_fruit_cherry(self): |
| 156 | + """测试水果偏好 - 樱桃""" |
| 157 | + _, response = self.run_with_test_data( |
| 158 | + data_name="fruit_cherry", |
| 159 | + query_message="我喜欢吃什么水果,平时爱喝什么", |
| 160 | + ) |
| 161 | + self.assertIsNotNone(response) |
| 162 | + |
| 163 | + def test_fruit_mango(self): |
| 164 | + """测试水果偏好 - 芒果""" |
| 165 | + _, response = self.run_with_test_data( |
| 166 | + data_name="fruit_mango", |
| 167 | + query_message="我喜欢吃什么水果,平时爱喝什么", |
| 168 | + ) |
| 169 | + self.assertIsNotNone(response) |
| 170 | + |
| 171 | + def test_fruit_strawberry(self): |
| 172 | + """测试水果偏好 - 草莓""" |
| 173 | + _, response = self.run_with_test_data( |
| 174 | + data_name="fruit_strawberry", |
| 175 | + query_message="我喜欢吃什么水果,平时爱喝什么", |
| 176 | + ) |
| 177 | + self.assertIsNotNone(response) |
0 commit comments