[feature][for merge][part2] tau2 bench test cases#251
[feature][for merge][part2] tau2 bench test cases#251Keithwwa merged 1 commit intoAISBench:masterfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates a configuration test and introduces a comprehensive unit test suite for the TAU2BenchTask class. The feedback focuses on improving the test code quality by removing redundant imports, moving local imports to the top level for better organization, adhering to PEP 8 standards for import placement, and utilizing mocking decorators instead of manual environment or variable manipulation to ensure test isolation and reliability.
|
|
||
| # Mock litellm and tau2 modules before importing TAU2BenchTask | ||
| import sys | ||
| from unittest import mock |
| sys.modules['tau2.utils.display'].ConsoleDisplay.console.input = mock.MagicMock(return_value="y") | ||
|
|
||
| # Now import TAU2BenchTask | ||
| from ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task import TAU2BenchTask |
There was a problem hiding this comment.
Multiple functions and variables are imported locally within various test methods (e.g., auto_y_input, parse_args, get_tasks, patched_get_response_cost). It is cleaner and more efficient to import all necessary components from the module under test at the top level, following the initial mocks.
| from ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task import TAU2BenchTask | |
| from ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task import ( | |
| TAU2BenchTask, auto_y_input, parse_args, get_tasks, | |
| original_input, _patched_logger_error, patched_get_response_cost, | |
| litellm_get_response_cost | |
| ) |
|
|
||
| def tearDown(self): | ||
| # 清理临时目录 | ||
| import shutil |
| self.assertIn(cfg_path, command) | ||
| self.assertIn(os.path.basename(__file__).replace("test_", ""), command) | ||
|
|
||
| def test_set_api_key(self): |
There was a problem hiding this comment.
Modifying os.environ directly in a test can lead to side effects that affect other tests in the suite. Using @mock.patch.dict is a safer way to temporarily modify the environment and ensure it is restored after the test completes.
| def test_set_api_key(self): | |
| @mock.patch.dict(os.environ, {}, clear=False) | |
| def test_set_api_key(self): |
| def test_patched_get_response_cost(self): | ||
| """测试 patched_get_response_cost 函数""" | ||
| from ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task import patched_get_response_cost | ||
| from ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task import litellm_get_response_cost | ||
|
|
||
| # 保存原始值 | ||
| original_litellm_get_response_cost = litellm_get_response_cost | ||
|
|
||
| try: | ||
| # 测试 litellm_get_response_cost 为 None 的情况 | ||
| import ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task | ||
| ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task.litellm_get_response_cost = None | ||
| result = patched_get_response_cost() | ||
| self.assertEqual(result, 0.0) | ||
| finally: | ||
| # 恢复原始值 | ||
| import ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task | ||
| ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task.litellm_get_response_cost = original_litellm_get_response_cost |
There was a problem hiding this comment.
Manual patching and restoration of module-level variables (like litellm_get_response_cost) is verbose and error-prone. Using the @mock.patch decorator simplifies the test and guarantees that the original value is restored even if the test fails.
| def test_patched_get_response_cost(self): | |
| """测试 patched_get_response_cost 函数""" | |
| from ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task import patched_get_response_cost | |
| from ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task import litellm_get_response_cost | |
| # 保存原始值 | |
| original_litellm_get_response_cost = litellm_get_response_cost | |
| try: | |
| # 测试 litellm_get_response_cost 为 None 的情况 | |
| import ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task | |
| ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task.litellm_get_response_cost = None | |
| result = patched_get_response_cost() | |
| self.assertEqual(result, 0.0) | |
| finally: | |
| # 恢复原始值 | |
| import ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task | |
| ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task.litellm_get_response_cost = original_litellm_get_response_cost | |
| @mock.patch('ais_bench.benchmark.tasks.custom_tasks.tau2_bench_task.litellm_get_response_cost', None) | |
| def test_patched_get_response_cost(self): | |
| """测试 patched_get_response_cost 函数""" | |
| result = patched_get_response_cost() | |
| self.assertEqual(result, 0.0) |
Thanks for your contribution; we appreciate it a lot. The following instructions will make your pull request healthier and help you get feedback more easily. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.
感谢您的贡献,我们非常重视。以下说明将使您的拉取请求更健康,更易于获得反馈。如果您不理解某些项目,请不要担心,只需提交拉取请求并从维护人员那里寻求帮助即可。
PR Type / PR类型
Related Issue | 关联 Issue
Fixes #(issue ID / issue 编号) / Relates to #(issue ID / issue 编号)
full pr:
#192
🔍 Motivation / 变更动机
Please describe the motivation of this PR and the goal you want to achieve through this PR.
请描述您的拉取请求的动机和您希望通过此拉取请求实现的目标。
📝 Modification / 修改内容
Please briefly describe what modification is made in this PR.
请简要描述此拉取请求中进行的修改。
📐 Associated Test Results / 关联测试结果
Please provide links to the related test results, such as CI pipelines, test reports, etc.
请提供相关测试结果的链接,例如 CI 管道、测试报告等。
Does the modification introduce changes that break the backward compatibility of the downstream repositories? If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.
是否引入了会破坏下游存储库向后兼容性的更改?如果是,请描述它如何破坏兼容性,以及下游项目应该如何修改其代码以保持与此 PR 的兼容性。
If the modification introduces performance degradation, please describe the impact of the performance degradation and the expected performance improvement.
如果引入了性能下降,请描述性能下降的影响和预期的性能改进。
🌟 Use cases (Optional) / 使用案例(可选)
If this PR introduces a new feature, it is better to list some use cases here and update the documentation.
如果此拉取请求引入了新功能,最好在此处列出一些用例并更新文档。
✅ Checklist / 检查列表
Before PR:
After PR:
👥 Collaboration Info / 协作信息
🌟 Useful CI Command / 实用的CI命令
/gemini review/gemini summary/gemini help/readthedocs build