run_api_tests.ps1 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. $ErrorActionPreference = "Stop"
  2. $projectRoot = Resolve-Path "$PSScriptRoot\..\.."
  3. $backendRoot = Join-Path $projectRoot "project\backend"
  4. $reportDir = Join-Path $PSScriptRoot "reports"
  5. $logFile = Join-Path $reportDir "api_test_log.txt"
  6. $summaryFile = Join-Path $reportDir "api_test_summary.txt"
  7. New-Item -ItemType Directory -Path $reportDir -Force | Out-Null
  8. Push-Location $backendRoot
  9. try {
  10. $env:NO_COLOR = "1"
  11. $oldErrorActionPreference = $ErrorActionPreference
  12. $ErrorActionPreference = "Continue"
  13. $output = & cmd.exe /d /s /c "npm.cmd test -- tests/health.spec.ts tests/api-contract.spec.ts 2>&1"
  14. $exitCode = $LASTEXITCODE
  15. $ErrorActionPreference = $oldErrorActionPreference
  16. } finally {
  17. $ErrorActionPreference = "Stop"
  18. Pop-Location
  19. }
  20. $ansiPattern = "$([char]27)\[[0-9;]*m"
  21. $textOutput = $output | ForEach-Object { $_.ToString() -replace $ansiPattern, "" }
  22. $textOutput | Set-Content -Path $logFile -Encoding UTF8
  23. $textOutput | ForEach-Object { Write-Host $_ }
  24. $result = "PASS"
  25. if ($exitCode -ne 0) {
  26. $result = "FAIL"
  27. }
  28. @(
  29. "ServicePilot 接口测试执行记录",
  30. "执行时间:$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
  31. "测试对象:project/backend/tests/health.spec.ts、project/backend/tests/api-contract.spec.ts",
  32. "测试范围:健康检查、Parse 前端配置、敏感字段保护、404 契约",
  33. "预期标准:HTTP 状态码、响应结构和错误信息符合接口契约",
  34. "执行结果:$result",
  35. "详细日志:$logFile"
  36. ) | Set-Content -Path $summaryFile -Encoding UTF8
  37. Write-Host "接口测试日志已生成:$logFile"
  38. Write-Host "接口测试摘要已生成:$summaryFile"
  39. if ($exitCode -ne 0) {
  40. exit $exitCode
  41. }