测试运行脚本#
脚本说明#
测试运行脚本用于自动化运行测试、生成测试报告、管理测试环境等。
脚本代码#
#!/bin/bash
# 测试运行脚本
# 功能:自动化运行测试、生成测试报告
# 作者:System Admin
# 日期:2024-01-01
set -euo pipefail
# 配置变量
PROJECT_DIR=""
TEST_TYPE="all"
TEST_ENV="test"
REPORT_DIR="/tmp/test_reports"
PARALLEL=false
COVERAGE=false
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 日志函数
log() {
local level=$1
shift
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$timestamp] [$level] $@"
}
log_info() {
log "INFO" "$@"
}
log_error() {
log "ERROR" "$@"
}
# 检查项目目录
check_project_dir() {
if [ ! -d "$PROJECT_DIR" ]; then
log_error "项目目录不存在: $PROJECT_DIR"
return 1
fi
return 0
}
# 创建报告目录
create_report_dir() {
if [ ! -d "$REPORT_DIR" ]; then
mkdir -p "$REPORT_DIR"
log_info "创建报告目录: $REPORT_DIR"
fi
}
# 运行Python测试
run_python_tests() {
local dir=$1
log_info "运行Python测试: $dir"
cd "$dir"
# 运行pytest
if command -v pytest &> /dev/null; then
log_info "使用pytest运行测试"
local pytest_cmd="pytest"
if [ "$COVERAGE" = true ]; then
pytest_cmd="$pytest_cmd --cov=. --cov-report=html --cov-report=term"
fi
if [ "$PARALLEL" = true ]; then
pytest_cmd="$pytest_cmd -n auto"
fi
eval $pytest_cmd
fi
# 运行unittest
if command -v python &> /dev/null; then
log_info "使用unittest运行测试"
python -m unittest discover
fi
}
# 运行JavaScript测试
run_javascript_tests() {
local dir=$1
log_info "运行JavaScript测试: $dir"
cd "$dir"
# 运行Jest
if command -v jest &> /dev/null; then
log_info "使用Jest运行测试"
local jest_cmd="jest"
if [ "$COVERAGE" = true ]; then
jest_cmd="$jest_cmd --coverage"
fi
if [ "$PARALLEL" = true ]; then
jest_cmd="$jest_cmd --maxWorkers=4"
fi
eval $jest_cmd
fi
# 运行Mocha
if command -v mocha &> /dev/null; then
log_info "使用Mocha运行测试"
local mocha_cmd="mocha"
if [ "$COVERAGE" = true ]; then
mocha_cmd="$mocha --coverage"
fi
eval $mocha_cmd
fi
}
# 运行Go测试
run_go_tests() {
local dir=$1
log_info "运行Go测试: $dir"
cd "$dir"
# 运行go test
log_info "使用go test运行测试"
local go_test_cmd="go test ./..."
if [ "$COVERAGE" = true ]; then
go_test_cmd="$go_test_cmd -coverprofile=coverage.out -covermode=atomic"
fi
if [ "$PARALLEL" = true ]; then
go_test_cmd="$go_test_cmd -parallel 4"
fi
eval $go_test_cmd
# 生成覆盖率报告
if [ "$COVERAGE" = true ]; then
go tool cover -html=coverage.out -o coverage.html
fi
}
# 运行Java测试
run_java_tests() {
local dir=$1
log_info "运行Java测试: $dir"
cd "$dir"
# 运行Maven测试
if [ -f "pom.xml" ]; then
log_info "使用Maven运行测试"
local mvn_cmd="mvn test"
if [ "$COVERAGE" = true ]; then
mvn_cmd="$mvn_cmd jacoco:report"
fi
eval $mvn_cmd
fi
# 运行Gradle测试
if [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then
log_info "使用Gradle运行测试"
local gradle_cmd="./gradlew test"
if [ "$COVERAGE" = true ]; then
gradle_cmd="$gradle_cmd jacocoTestReport"
fi
eval $gradle_cmd
fi
}
# 运行C/C++测试
run_cpp_tests() {
local dir=$1
log_info "运行C/C++测试: $dir"
cd "$dir"
# 运行Google Test
if [ -f "CMakeLists.txt" ]; then
log_info "使用CMake运行测试"
mkdir -p build
cd build
cmake ..
make
ctest --output-on-failure
fi
# 运行Makefile测试
if [ -f "Makefile" ]; then
log_info "使用Makefile运行测试"
make test
fi
}
# 运行集成测试
run_integration_tests() {
local dir=$1
log_info "运行集成测试: $dir"
cd "$dir"
# 检查是否有集成测试脚本
if [ -f "run_integration_tests.sh" ]; then
log_info "运行集成测试脚本"
bash run_integration_tests.sh
fi
# 检查是否有pytest集成测试
if command -v pytest &> /dev/null; then
pytest -m integration
fi
}
# 运行端到端测试
run_e2e_tests() {
local dir=$1
log_info "运行端到端测试: $dir"
cd "$dir"
# 运行Cypress测试
if command -v cypress &> /dev/null; then
log_info "使用Cypress运行测试"
cypress run
fi
# 运行Selenium测试
if command -v selenium &> /dev/null; then
log_info "使用Selenium运行测试"
python run_selenium_tests.py
fi
}
# 运行性能测试
run_performance_tests() {
local dir=$1
log_info "运行性能测试: $dir"
cd "$dir"
# 运行JMeter测试
if command -v jmeter &> /dev/null; then
log_info "使用JMeter运行测试"
jmeter -n -t test_plan.jmx -l results.jtl -e -o report
fi
# 运行Locust测试
if command -v locust &> /dev/null; then
log_info "使用Locust运行测试"
locust -f locustfile.py --headless -u 100 -r 10 -t 1m --csv results
fi
}
# 生成测试报告
generate_test_report() {
local dir=$1
local test_type=$2
log_info "生成测试报告: $test_type"
create_report_dir
local timestamp=$(date +%Y%m%d_%H%M%S)
local report_file="$REPORT_DIR/test_report_${test_type}_${timestamp}.txt"
{
echo "测试报告"
echo "======"
echo "测试类型: $test_type"
echo "测试时间: $(date)"
echo "项目目录: $dir"
echo "测试环境: $TEST_ENV"
echo ""
echo "测试结果"
echo "========"
# 查找测试结果文件
if [ -f "pytest-report.html" ]; then
echo "Pytest报告: pytest-report.html"
fi
if [ -f "coverage.html" ]; then
echo "覆盖率报告: coverage.html"
fi
if [ -f "junit.xml" ]; then
echo "JUnit报告: junit.xml"
fi
if [ -f "results.jtl" ]; then
echo "JMeter结果: results.jtl"
fi
echo ""
echo "报告生成时间: $(date)"
} > "$report_file"
log_info "测试报告已生成: $report_file"
}
# 清理测试环境
cleanup_test_env() {
local dir=$1
log_info "清理测试环境: $dir"
cd "$dir"
# 清理临时文件
rm -rf __pycache__
rm -rf .pytest_cache
rm -rf node_modules/.cache
rm -rf build
rm -rf dist
rm -rf coverage.out
rm -rf coverage.html
log_info "测试环境清理完成"
}
# 显示帮助
show_help() {
echo "用法: $0 [选项] <项目目录>"
echo ""
echo "选项:"
echo " -t <类型> 测试类型(all/unit/integration/e2e/performance)"
echo " -e <环境> 测试环境(dev/test/staging/prod)"
echo " -r <目录> 报告目录(默认: /tmp/test_reports)"
echo " -p 并行运行测试"
echo " -c 生成覆盖率报告"
echo " -h 显示帮助信息"
echo ""
echo "示例:"
echo " $0 /path/to/project"
echo " $0 -t unit /path/to/project"
echo " $0 -t integration /path/to/project"
echo " $0 -p -c /path/to/project"
}
# 主函数
main() {
# 解析选项
while getopts "t:e:r:pch" opt; do
case $opt in
t)
TEST_TYPE="$OPTARG"
log_info "测试类型: $TEST_TYPE"
;;
e)
TEST_ENV="$OPTARG"
log_info "测试环境: $TEST_ENV"
;;
r)
REPORT_DIR="$OPTARG"
log_info "报告目录: $REPORT_DIR"
;;
p)
PARALLEL=true
log_info "并行运行测试"
;;
c)
COVERAGE=true
log_info "生成覆盖率报告"
;;
h)
show_help
exit 0
;;
*)
log_error "无效选项: $opt"
show_help
exit 1
;;
esac
done
shift $((OPTIND - 1))
# 检查项目目录
if [ $# -eq 0 ]; then
log_error "缺少项目目录"
show_help
exit 1
fi
PROJECT_DIR="$1"
if ! check_project_dir; then
exit 1
fi
# 执行测试
case $TEST_TYPE in
all)
run_python_tests "$PROJECT_DIR"
run_javascript_tests "$PROJECT_DIR"
run_go_tests "$PROJECT_DIR"
run_java_tests "$PROJECT_DIR"
run_cpp_tests "$PROJECT_DIR"
generate_test_report "$PROJECT_DIR" "all"
;;
unit)
run_python_tests "$PROJECT_DIR"
run_javascript_tests "$PROJECT_DIR"
run_go_tests "$PROJECT_DIR"
run_java_tests "$PROJECT_DIR"
run_cpp_tests "$PROJECT_DIR"
generate_test_report "$PROJECT_DIR" "unit"
;;
integration)
run_integration_tests "$PROJECT_DIR"
generate_test_report "$PROJECT_DIR" "integration"
;;
e2e)
run_e2e_tests "$PROJECT_DIR"
generate_test_report "$PROJECT_DIR" "e2e"
;;
performance)
run_performance_tests "$PROJECT_DIR"
generate_test_report "$PROJECT_DIR" "performance"
;;
*)
log_error "无效的测试类型: $TEST_TYPE"
show_help
exit 1
;;
esac
# 清理测试环境
cleanup_test_env "$PROJECT_DIR"
log_info "测试运行完成"
}
# 执行主函数
main "$@"使用说明#
添加执行权限:
chmod +x test_runner.sh基本用法:
# 运行所有测试 ./test_runner.sh /path/to/project # 运行单元测试 ./test_runner.sh -t unit /path/to/project # 运行集成测试 ./test_runner.sh -t integration /path/to/project # 运行端到端测试 ./test_runner.sh -t e2e /path/to/project高级用法:
# 并行运行测试 ./test_runner.sh -p /path/to/project # 生成覆盖率报告 ./test_runner.sh -c /path/to/project # 指定测试环境 ./test_runner.sh -e staging /path/to/project # 指定报告目录 ./test_runner.sh -r /custom/reports /path/to/project
功能特点#
- 多语言支持(Python、JavaScript、Go、Java、C/C++)
- 单元测试
- 集成测试
- 端到端测试
- 性能测试
- 并行测试执行
- 代码覆盖率报告
- 测试报告生成
- 测试环境清理
依赖项#
- pytest/unittest: Python测试框架
- jest/mocha: JavaScript测试框架
- go test: Go测试框架
- maven/gradle: Java测试工具
- ctest: C/C++测试工具
- cypress/selenium: 端到端测试工具
- jmeter/locust: 性能测试工具
注意事项#
- 需要安装相应的测试工具
- 确保测试环境配置正确
- 并行测试可能影响测试结果
- 覆盖率报告需要额外配置
- 端到端测试需要浏览器环境