#!/bin/bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" API_DIR="$PROJECT_ROOT/everything-is-suitable-api" REPORT_DIR="$PROJECT_ROOT/test-automation/test-reports" TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") CURRENT_REPORT_DIR="$REPORT_DIR/$TIMESTAMP" mkdir -p "$CURRENT_REPORT_DIR" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" } log "=========================================" log "API测试执行开始" log "=========================================" log "项目根目录: $PROJECT_ROOT" log "报告输出目录: $CURRENT_REPORT_DIR" log "=========================================" cd "$API_DIR" TEST_RESULT=0 log "[API测试] 开始执行API测试..." API_TEST_LOG="$CURRENT_REPORT_DIR/api-test.log" if mvn clean test -Dspring.profiles.active=test > "$API_TEST_LOG" 2>&1; then log "[API测试] ✓ API测试通过" else log "[API测试] ✗ API测试失败" TEST_RESULT=1 fi log "[API测试] 复制测试报告..." find "$API_DIR" -name "TEST-*.xml" -exec cp {} "$CURRENT_REPORT_DIR/" \; 2>/dev/null || true find "$API_DIR" -name "surefire-reports" -type d -exec cp -r {} "$CURRENT_REPORT_DIR/" \; 2>/dev/null || true log "[API测试] 复制JaCoCo覆盖率报告..." find "$API_DIR" -path "*/target/site/jacoco/*" -exec cp -r {} "$CURRENT_REPORT_DIR/jacoco/" \; 2>/dev/null || true log "[API测试] 生成Allure报告..." find "$API_DIR" -path "*/target/allure-results/*" -exec cp -r {} "$CURRENT_REPORT_DIR/allure-results/" \; 2>/dev/null || true if [ -d "$CURRENT_REPORT_DIR/allure-results" ]; then allure generate "$CURRENT_REPORT_DIR/allure-results" -o "$CURRENT_REPORT_DIR/allure-report" 2>/dev/null || log "[API测试] Allure报告生成失败,但继续执行" fi log "[API测试] API测试完成" log "=========================================" if [ $TEST_RESULT -eq 0 ]; then log "✓ API测试通过" log "退出码: 0" exit 0 else log "✗ API测试失败" log "退出码: 1" exit 1 fi