确保所有使用 @evaluation_test 装饰的函数都能被 pytest 自动发现,无论函数名是否符合 pytest 命名规范。
当函数名不以 test_ 开头时,decorator 会:
- 自动在调用者的全局命名空间中注册一个以
test_开头的别名 - Pytest 扫描模块时会发现这个别名
- 用户无需修改任何代码或命名
- ✅ 移除了警告功能
- ✅ 添加了自动注册逻辑(使用
sys._getframe访问调用者的全局命名空间)
- ✅ 确保 wrapper 的
__name__属性以test_开头
- ✅ 确保 dual_mode_wrapper 的
__name__属性以test_开头
from eval_protocol.pytest import evaluation_test
from eval_protocol.models import EvaluationRow, EvaluateResult
# ✅ 不需要以 test_ 开头 - 会自动注册为 test_my_evaluation
@evaluation_test(
input_rows=[[EvaluationRow(messages=[{"role": "user", "content": "Hello"}])]]
)
async def my_evaluation(row: EvaluationRow) -> EvaluationRow:
row.evaluation_result = EvaluateResult(score=1.0)
return row
# ✅ 已经符合命名规范 - 正常工作
@evaluation_test(
input_rows=[[EvaluationRow(messages=[{"role": "user", "content": "Hello"}])]]
)
async def test_my_evaluation(row: EvaluationRow) -> EvaluationRow:
row.evaluation_result = EvaluateResult(score=1.0)
return row$ pytest --collect-only tests/test_auto_discovery_simple.py -v
collected 2 items
<Module test_auto_discovery_simple.py>
<Coroutine test_my_custom_eval[rows(len=1)]> # 自动注册!
<Coroutine test_proper_eval[rows(len=1)]>
$ pytest tests/test_auto_discovery_simple.py -v
============================== 2 passed in 0.15s ==============================- 零配置:无需任何额外配置
- 无需警告:静默自动处理,不打扰用户
- 完全兼容:不影响已有代码
- 简单直接:用户只需使用
@evaluation_test,其他都自动处理 - 可靠:经过测试验证
- Pytest 通过扫描模块的全局命名空间来发现测试
- 我们在装饰时自动在命名空间中注册正确命名的别名
- 用户原始函数名保持不变,可以继续使用
- ✅
tests/test_auto_discovery_simple.py- 验证自动发现功能- 测试不以
test_开头的函数能被发现 - 测试以
test_开头的函数正常工作 - 所有测试通过
- 测试不以
development/auto_test_discovery.md- 详细技术文档development/FINAL_SUMMARY.md- 本文档
现在,用户只需要:
@evaluation_test(...)
async def any_function_name(row: EvaluationRow) -> EvaluationRow:
# 无论函数名是什么,都能被 pytest 发现!
...就这么简单! 🎉
不需要:
- ❌ 记住命名规范
- ❌ 收到警告信息
- ❌ 手动配置 pytest
- ❌ 修改现有代码
只要使用 @evaluation_test,就能保证测试被发现!✨