You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cbmc/codedetect/scripts/run-comprehensive-tests.sh

149 lines
4.2 KiB

#!/bin/bash
# CodeDetect综合测试运行脚本
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 测试类型
TEST_TYPES=("unit" "integration" "performance" "regression")
SELECTED_TEST_TYPES=("${TEST_TYPES[@]}")
# 解析命令行参数
while [[ $# -gt 0 ]]; do
case $1 in
-t|--type)
shift
SELECTED_TEST_TYPES=($1)
shift
;;
-h|--help)
echo "Usage: $0 [-t|--type TYPE1,TYPE2,...] [-h|--help]"
echo "Available types: unit, integration, performance, regression"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
echo -e "${BLUE}🚀 CodeDetect综合测试运行器${NC}"
echo -e "${YELLOW}测试类型: ${SELECTED_TEST_TYPES[*]}${NC}"
# 创建测试结果目录
RESULTS_DIR="test_results"
mkdir -p "$RESULTS_DIR"
# 运行测试函数
run_tests() {
local test_type=$1
local test_command=$2
local output_file="$RESULTS_DIR/${test_type}_results.xml"
echo -e "${YELLOW}🧪 运行${test_type}测试...${NC}"
if [ "$test_type" = "performance" ]; then
# 性能测试使用不同的标记
pytest -m "not slow" -v --tb=short --junitxml="$output_file" "tests/${test_type}/"
else
$test_command --junitxml="$output_file"
fi
if [ $? -eq 0 ]; then
echo -e "${GREEN}${test_type}测试通过${NC}"
else
echo -e "${RED}${test_type}测试失败${NC}"
return 1
fi
}
# 主测试循环
FAILED_TESTS=()
for test_type in "${SELECTED_TEST_TYPES[@]}"; do
case $test_type in
"unit")
run_tests "unit" "pytest tests/unit/ -v --tb=short" || FAILED_TESTS+=("unit")
;;
"integration")
run_tests "integration" "pytest tests/integration/ -v --tb=short" || FAILED_TESTS+=("integration")
;;
"performance")
run_tests "performance" "pytest tests/performance/ -v --tb=short" || FAILED_TESTS+=("performance")
;;
"regression")
run_tests "regression" "pytest tests/regression/ -v --tb=short" || FAILED_TESTS+=("regression")
;;
*)
echo -e "${RED}❌ 未知的测试类型: $test_type${NC}"
FAILED_TESTS+=("$test_type")
;;
esac
done
# 生成汇总报告
echo -e "${BLUE}📊 生成测试汇总报告...${NC}"
python3 << 'EOF'
import xml.etree.ElementTree as ET
import glob
import os
results_dir = "test_results"
summary = {
"total": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"errors": 0
}
# 解析JUnit XML文件
for xml_file in glob.glob(f"{results_dir}/*_results.xml"):
if os.path.exists(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
for testsuite in root.findall('testsuite'):
summary["total"] += int(testsuite.get('tests', 0))
summary["failed"] += int(testsuite.get('failures', 0))
summary["skipped"] += int(testsuite.get('skipped', 0))
summary["errors"] += int(testsuite.get('errors', 0))
summary["passed"] = summary["total"] - summary["failed"] - summary["skipped"] - summary["errors"]
print(f"测试汇总:")
print(f" 总计: {summary['total']}")
print(f" 通过: {summary['passed']}")
print(f" 失败: {summary['failed']}")
print(f" 跳过: {summary['skipped']}")
print(f" 错误: {summary['errors']}")
print(f" 成功率: {summary['passed']/summary['total']*100:.1f}%")
# 保存汇总
with open(f"{results_dir}/summary.txt", "w") as f:
f.write(f"测试汇总:\n")
f.write(f" 总计: {summary['total']}\n")
f.write(f" 通过: {summary['passed']}\n")
f.write(f" 失败: {summary['failed']}\n")
f.write(f" 跳过: {summary['skipped']}\n")
f.write(f" 错误: {summary['errors']}\n")
f.write(f" 成功率: {summary['passed']/summary['total']*100:.1f}%\n")
EOF
# 最终结果
echo -e "${BLUE}📋 测试完成${NC}"
if [ ${#FAILED_TESTS[@]} -eq 0 ]; then
echo -e "${GREEN}✅ 所有测试通过!${NC}"
exit 0
else
echo -e "${RED}❌ 以下测试失败: ${FAILED_TESTS[*]}${NC}"
echo -e "${YELLOW}详细结果请查看 $RESULTS_DIR/ 目录${NC}"
exit 1
fi