HumanEval 编程基准测试(164 道 Python 题)集成到现有 tests/ 自动化框架中,实现“无人值守自动化测试闭环”。test_data.json 格式兼容性,使 HumanEval 题目可直接被 run_single_test.sh 和 evaluate_result.sh 识别。执行对象:Hermes 任务:在系统上安装 Python 3(如果尚未安装)。 具体操作步骤:
python3 --versionpython3 --version 验证输出显示 Python 版本号 ≥ 3.9 - Ubuntu/Debian:sudo apt update && sudo apt install -y python3 python3-pip - CentOS/RHEL:sudo yum install -y python3 python3-pip 验收标准:python3 --version 返回版本号 ≥ 3.9 且退出码为 0
执行对象:Hermes 任务:从 Gitee 镜像下载 HumanEval 数据集。 具体操作步骤:
tests/datasets/(如果不存在则创建): mkdir -p tests/datasets
tests/datasets/ 目录: cd tests/datasets
git clone https://gitee.com/bin-mian/CodeGeeX.git human_eval_repo
ls human_eval_repo/benchmark/
确认可以看到 HumanEval.jsonl 或 HumanEval.jsonl.gz 文件
git clone https://gitee.com/mirrors/agentverse.git human_eval_repo
然后检查 human_eval_repo/dataloader/ 目录下是否有 HumanEval.jsonl 文件 验收标准:tests/datasets/human_eval_repo/ 目录存在且包含 HumanEval 数据文件
执行对象:Hermes 任务:验证下载的 HumanEval 数据文件格式。 具体操作步骤:
find tests/datasets/human_eval_repo -name "HumanEval*" -type f
.jsonl 文件(优先选择不带 .gz 的) wc -l <选定的文件路径>
输出应至少为 100 行(HumanEval 有 164 道题)
head -n 1 <选定的文件路径>
确认输出包含类似 {"task_id": "HumanEval/0", "prompt": "...", "entry_point": "...", "test": "...", ...} 的 JSON 结构
.gz 格式,先解压: gunzip -c <文件路径> > tests/datasets/human_eval.jsonl
验收标准:tests/datasets/human_eval.jsonl 文件存在,至少包含 100 行,每行是合法 JSON
执行对象:Hermes 任务:创建 HumanEval 到 CmdCode 测试框架的格式转换脚本。 具体操作步骤:
tests/ 目录下创建文件 convert_humaneval.py#!/usr/bin/env python3
import json
import sys
import re
def escape_json_for_shell(s):
"""将 JSON 字符串转义为 Shell 安全形式"""
return s.replace('\\', '\\\\').replace('"', '\\"').replace('$', '\\$')
def main():
input_file = 'tests/datasets/human_eval.jsonl'
output_file = 'tests/humaneval_tests.json'
tests = []
try:
with open(input_file, 'r', encoding='utf-8') as f:
for line in f:
if line.strip():
obj = json.loads(line)
tests.append(obj)
except FileNotFoundError:
print(f"Error: {input_file} not found", file=sys.stderr)
sys.exit(1)
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON in {input_file}: {e}", file=sys.stderr)
sys.exit(1)
output = []
for t in tests:
task_id = t['task_id']
entry_point = t.get('entry_point', 'solution')
prompt_text = t['prompt']
test_code = t['test']
full_prompt = f'''{prompt_text}
Write the complete implementation of the function `{entry_point}`, ensuring it passes all the following test cases:
python {test_code}
Output ONLY the Python code in a single code block. Do not include explanations.
'''
expected_pattern = f'def {entry_point}'
output.append({
"id": task_id.replace('/', '_'),
"prompt": full_prompt,
"expected_code_pattern": expected_pattern,
"language": "python",
"expected_files": [
{
"path": "solution.py",
"pattern": f'def {entry_point}'
}
]
})
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(output, f, indent=2, ensure_ascii=False)
print(f"Converted {len(output)} questions to {output_file}")
if __name__ == '__main__':
main()
chmod +x tests/convert_humaneval.py
验收标准:文件 tests/convert_humaneval.py 存在且可执行
执行对象:Hermes 任务:运行格式转换脚本生成题单。 具体操作步骤:
python3 tests/convert_humaneval.py
Converted 164 questions to tests/humaneval_tests.json cat tests/humaneval_tests.json | python3 -m json.tool > /dev/null
确认退出码为 0(JSON 格式合法)
python3 -c "import json; d=json.load(open('tests/humaneval_tests.json')); print(d[0]['id']); print(d[0]['prompt'][:200])"
验收标准:tests/humaneval_tests.json 存在,包含 164 个题目,JSON 格式合法
执行对象:Hermes 任务:将 HumanEval 题单合并到测试框架主题库。 具体操作步骤:
cp tests/test_data.json tests/test_data.json.bak
python3 -c "
import json
with open('tests/test_data.json') as f: existing = json.load(f)
with open('tests/humaneval_tests.json') as f: humaneval = json.load(f)
combined = existing + humaneval
with open('tests/test_data.json', 'w') as f: json.dump(combined, f, indent=2, ensure_ascii=False)
print(f'Total questions: {len(combined)}')
"
验收标准:tests/test_data.json 包含 169 个题目对象
执行对象:Hermes 任务:增强评估脚本以支持 HumanEval 的实际单元测试验证。 具体操作步骤:
tests/ 目录下创建 eval_humaneval.py 文件#!/usr/bin/env python3
import json
import sys
import subprocess
import tempfile
import os
def evaluate_solution(code_file, test_code, entry_point):
"""运行方案代码并执行测试"""
# 写入测试脚本
test_script = f'''
{open(code_file).read()}
{test_code}
'''
# 创建临时测试文件
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write(test_script)
test_file = f.name
try:
result = subprocess.run(
['python3', test_file],
capture_output=True,
text=True,
timeout=30
)
os.unlink(test_file)
if result.returncode == 0:
return True, "Tests passed"
else:
return False, result.stderr[:500] if result.stderr else "Tests failed"
except subprocess.TimeoutExpired:
os.unlink(test_file)
return False, "Tests timed out"
except Exception as e:
try: os.unlink(test_file)
except: pass
return False, str(e)[:500]
def main():
if len(sys.argv) < 3:
print("Usage: python3 eval_humaneval.py <solution_py> <humaneval_jsonl_line>")
sys.exit(1)
solution_file = sys.argv[1]
test_line = sys.argv[2]
try:
test_data = json.loads(test_line)
except:
print(json.dumps({"passed": False, "details": "Invalid test line"}))
sys.exit(0)
passed, details = evaluate_solution(
solution_file,
test_data.get('test', ''),
test_data.get('entry_point', 'solution')
)
print(json.dumps({"passed": passed, "details": details}))
if __name__ == '__main__':
main()
chmod +x tests/eval_humaneval.py
验收标准:文件 tests/eval_humaneval.py 存在且可执行
执行对象:Hermes 任务:修改 tests/evaluate_result.sh 以支持 HumanEval 格式的深度评估。 具体操作步骤:
tests/evaluate_result.sh # 如果题目来自 HumanEval(ID 包含 HumanEval/),使用 Python 单元测试
if [[ "$QUESTION_ID" == *HumanEval* ]]; then
# 查找对应的原始测试数据
TEST_LINE=$(python3 -c "
import json
with open('tests/datasets/human_eval.jsonl') as f:
for line in f:
obj = json.loads(line)
if obj['task_id'].replace('/','_') == '$QUESTION_ID':
print(json.dumps(obj))
break
")
if [ -f "tests/temp/${QUESTION_ID}/solution.py" ]; then
PYTHON_RESULT=$(python3 tests/eval_humaneval.py "tests/temp/${QUESTION_ID}/solution.py" "$TEST_LINE")
PASSED=$(echo "$PYTHON_RESULT" | python3 -c "import sys,json; print(json.load(sys.stdin)['passed'])")
DETAILS=$(echo "$PYTHON_RESULT" | python3 -c "import sys,json; print(json.load(sys.stdin)['details'])")
if [ "$PASSED" == "True" ]; then
echo "{\"passed\":true,\"details\":\"$DETAILS\"}" > "tests/results/${QUESTION_ID}_evaluation.json"
exit 0
else
echo "{\"passed\":false,\"details\":\"$DETAILS\"}" > "tests/results/${QUESTION_ID}_evaluation.json"
exit 1
fi
fi
fi
验收标准:HumanEval 题目评估时调用 eval_humaneval.py 并输出结构化 JSON 结果
执行对象:Hermes 任务:用 HumanEval 第 0 题运行单次闭环测试。 具体操作步骤:
export TEST_ID="HumanEval_0"
bash tests/run_single_test.sh "$TEST_ID"
bash tests/evaluate_result.sh "$TEST_ID"
cat tests/results/${TEST_ID}_evaluation.json
验收标准:三步骤均成功执行,evaluation.json 包含有效 passed 值(true 或 false)
执行对象:Hermes 任务:如果提示词 9 的测试失败,进行最小化诊断。 具体操作步骤:
cat tests/temp/${TEST_ID}/solution.py 2>/dev/null || echo "No solution.py found"
cat tests/results/${TEST_ID}_stdout.txt
cat tests/results/${TEST_ID}_stderr.txt
solution.py 不存在,检查所有生成的文件: ls -la tests/temp/${TEST_ID}/
tests/results/${TEST_ID}_analysis.txt验收标准:完成诊断并产出分析报告
执行对象:Hermes 任务:修改 tests/auto_loop.sh 主循环,添加 HumanEval 题目识别逻辑。 具体操作步骤:
tests/auto_loop.shfor 循环遍历题目的部分 # 如果是 HumanEval 题目,查找原始测试数据并保存
if [[ "$QUESTION_ID" == *HumanEval* ]]; then
python3 -c "
import json
with open('tests/datasets/human_eval.jsonl') as f:
for line in f:
obj = json.loads(line)
if obj['task_id'].replace('/','_') == '$QUESTION_ID':
with open('tests/results/${QUESTION_ID}_humaneval_data.json', 'w') as out:
json.dump(obj, out)
print('HumanEval data saved')
break
" 2>/dev/null || echo "Warning: Could not find HumanEval data for $QUESTION_ID"
fi
验收标准:循环中 HumanEval 题目会自动保存原始测试数据到结果目录
执行对象:Hermes 任务:运行前 5 道 HumanEval 题目的批量测试。 具体操作步骤:
python3 -c "
import json
with open('tests/test_data.json.bak') as f: base = json.load(f)
with open('tests/humaneval_tests.json') as f: he = json.load(f)
with open('tests/test_data_mini.json', 'w') as f: json.dump(base + he[:5], f, indent=2)
print('Created mini test set with', len(base)+5, 'questions')
"
cp tests/test_data.json tests/test_data_full.json
cp tests/test_data_mini.json tests/test_data.json
bash tests/auto_loop.sh
验收标准:5 道 HumanEval 题完成测试,框架输出各题结果
执行对象:Hermes 任务:运行全量 164 道 HumanEval 题目测试。 具体操作步骤:
cp tests/test_data_full.json tests/test_data.json
bash tests/auto_loop.sh
bash tests/generate_report.sh
cat TEST_REPORT.md
验收标准:164 道 HumanEval 题全部测试完成,生成 TEST_REPORT.md 包含完整统计数据
执行对象:Hermes 任务:对首次测试中失败的题目,利用已有分析文件进行针对性修复。 具体操作步骤:
for f in tests/results/HumanEval_*_evaluation.json; do
passed=$(python3 -c "import json; print(json.load(open('$f'))['passed'])")
if [ "$passed" != "True" ]; then
echo $(basename $f _evaluation.json) >> tests/failed_ids.txt
fi
done
tests/failed_ids.txt 中的每个 ID: - 查看分析报告:cat tests/results/${ID}_analysis.txt - 查看 CmdCode 输出:cat tests/results/${ID}_stdout.txt - 根据分析报告修改 src/cli.ts 或 src/chat.ts 等相关源码 - 重新运行该题测试:bash tests/run_single_test.sh $ID && bash tests/evaluate_result.sh $ID 验收标准:失败题目列表中的每道题都经过重新测试
执行对象:Hermes 任务:生成完整测试报告并部署。 具体操作步骤:
bash tests/generate_report.sh
grep -c "HumanEval" TEST_REPORT.md
- 如果项目有部署脚本(如 deploy.sh),运行它 - 否则手动将 TEST_REPORT.md 上传到 CmdCode 网站文档目录
curl -s https://cmdcode.cn/xiaoc/cmdcode-test-report.html | head -20
验收标准:TEST_REPORT.md 包含 HumanEval 测试章节,且在线报告可访问
最终验证:手动运行一次完整闭环。 任务:从零开始运行完整测试框架。 具体操作步骤:
rm -rf tests/temp/* tests/results/*
cp tests/test_data_full.json tests/test_data.json
bash tests/auto_loop.sh
bash tests/generate_report.sh
验收标准:所有步骤均成功完成,TEST_REPORT.md 包含:
以上 16 个提示词构成完整的 HumanEval 集成与测试闭环。每个提示词均具原子性,可被 Hermes 直接执行,无需额外解释。