run_tests.ps1 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # =============================================================================
  2. # 一键跑完全部测试:Python 引擎/接口 + BFF 单元与端到端 + 前端单测 + 全链路联调
  3. #
  4. # pwsh -File scripts/run_tests.ps1 # 全部
  5. # pwsh -File scripts/run_tests.ps1 -SkipE2E # 跳过需要真实服务的联调
  6. # pwsh -File scripts/run_tests.ps1 -Only python # 只跑某一组
  7. # =============================================================================
  8. param(
  9. [ValidateSet('all', 'python', 'bff', 'web', 'e2e', 'ui')]
  10. [string]$Only = 'all',
  11. [switch]$SkipE2E
  12. )
  13. $ErrorActionPreference = 'Continue'
  14. $root = Split-Path -Parent $PSScriptRoot
  15. $env:PYTHONIOENCODING = 'utf-8'
  16. $results = @()
  17. function Write-Section([string]$text) {
  18. Write-Host ''
  19. Write-Host ("=" * 68) -ForegroundColor DarkGray
  20. Write-Host " $text" -ForegroundColor Cyan
  21. Write-Host ("=" * 68) -ForegroundColor DarkGray
  22. }
  23. function Invoke-Suite([string]$name, [string]$workdir, [string]$command, [string[]]$arguments) {
  24. Write-Section $name
  25. Push-Location $workdir
  26. try {
  27. & $command @arguments
  28. $code = $LASTEXITCODE
  29. } catch {
  30. Write-Host "执行失败:$_" -ForegroundColor Red
  31. $code = 1
  32. } finally {
  33. Pop-Location
  34. }
  35. $script:results += [pscustomobject]@{ Suite = $name; ExitCode = $code }
  36. if ($code -eq 0) {
  37. Write-Host "✓ $name 通过" -ForegroundColor Green
  38. } else {
  39. Write-Host "✗ $name 失败(exit $code)" -ForegroundColor Red
  40. }
  41. }
  42. # ------------------------------------------------------------------ Python
  43. if ($Only -in @('all', 'python')) {
  44. Invoke-Suite 'Python 引擎与接口测试(pytest)' "$root\tools-service" 'python' @('-m', 'pytest', 'tests/', '-q')
  45. }
  46. # ------------------------------------------------------------------ BFF
  47. if ($Only -in @('all', 'bff')) {
  48. Push-Location "$root\bff"
  49. Write-Section 'BFF 类型检查与构建'
  50. & npx tsc -p tsconfig.json
  51. Pop-Location
  52. Invoke-Suite 'BFF 单元与接口测试(node:test)' "$root\bff" 'node' @('--test', 'dist/test/*.test.js')
  53. }
  54. # ------------------------------------------------------------------ Web
  55. if ($Only -in @('all', 'web')) {
  56. Invoke-Suite '前端类型检查(tsc)' "$root\web" 'npx' @('tsc', '--noEmit')
  57. Invoke-Suite '前端单元测试(vitest)' "$root\web" 'npx' @('vitest', 'run')
  58. Invoke-Suite '前端生产构建(vite build)' "$root\web" 'npx' @('vite', 'build')
  59. }
  60. # ------------------------------------------------------------------ E2E
  61. if (($Only -in @('all', 'e2e')) -and (-not $SkipE2E)) {
  62. Invoke-Suite '端到端联调(真实服务 + 真实模型)' $root 'node' @('scripts/e2e.mjs')
  63. }
  64. # ------------------------------------------------------------------ 浏览器交互
  65. if ($Only -in @('all', 'ui')) {
  66. Invoke-Suite '真实浏览器交互探针(CDP)' $root 'node' @('scripts/ui_probe.mjs')
  67. }
  68. # ------------------------------------------------------------------ 汇总
  69. Write-Section '测试汇总'
  70. $results | ForEach-Object {
  71. $mark = if ($_.ExitCode -eq 0) { '✓' } else { '✗' }
  72. $color = if ($_.ExitCode -eq 0) { 'Green' } else { 'Red' }
  73. Write-Host (" $mark {0,-46} exit={1}" -f $_.Suite, $_.ExitCode) -ForegroundColor $color
  74. }
  75. $failed = ($results | Where-Object { $_.ExitCode -ne 0 }).Count
  76. Write-Host ''
  77. if ($failed -eq 0) {
  78. Write-Host "全部 $($results.Count) 组测试通过" -ForegroundColor Green
  79. exit 0
  80. } else {
  81. Write-Host "$failed / $($results.Count) 组测试失败" -ForegroundColor Red
  82. exit 1
  83. }