[CmdletBinding()] param( [ValidateSet('Debug', 'Release')] [string]$Configuration = 'Debug', [switch]$SkipPerformance ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $projectRoot = Split-Path -Parent $PSScriptRoot $qtRoot = 'D:\Qt5.15.2\5.15.2\mingw81_64' $mingwBin = 'D:\Qt5.15.2\Tools\mingw810_64\bin' $qmakePath = Join-Path $qtRoot 'bin\qmake.exe' $makePath = Join-Path $mingwBin 'mingw32-make.exe' $configurationName = $Configuration.ToLowerInvariant() $testSourceRoot = Join-Path $projectRoot 'app\tests' $buildRoot = Join-Path $projectRoot "build\tests\$configurationName" foreach ($requiredPath in @($qmakePath, $makePath, $testSourceRoot)) { if (-not (Test-Path -LiteralPath $requiredPath)) { throw "未找到必要文件:$requiredPath" } } $targets = @( 'domain_tests', 'alarm_service_tests', 'hmi_editor_service_tests', 'logic_editor_service_tests', 'offline_simulation_service_tests', 'project_management_tests', 'register_monitor_service_tests', 'runtime_mode_service_tests', 'plc_runtime_tests', 'main_window_tests' ) if (-not $SkipPerformance) { $targets += 'performance_tests' } $originalPath = $env:Path $originalPlatform = $env:QT_QPA_PLATFORM $env:Path = "$qtRoot\bin;$mingwBin;$env:Path" $env:QT_QPA_PLATFORM = 'offscreen' try { foreach ($target in $targets) { $projectFile = Join-Path $testSourceRoot "$target.pro" $buildDirectory = Join-Path $buildRoot $target $executablePath = Join-Path $buildDirectory "$configurationName\$target.exe" New-Item -ItemType Directory -Path $buildDirectory -Force | Out-Null Push-Location $buildDirectory try { & $qmakePath $projectFile "CONFIG+=$configurationName" "CONFIG-=$(if ($configurationName -eq 'debug') { 'release' } else { 'debug' })" if ($LASTEXITCODE -ne 0) { throw "qmake 失败:$target,退出码:$LASTEXITCODE" } & $makePath -j2 if ($LASTEXITCODE -ne 0) { throw "构建失败:$target,退出码:$LASTEXITCODE" } if (-not (Test-Path -LiteralPath $executablePath)) { throw "未生成测试可执行文件:$executablePath" } Write-Host "运行 $target" & $executablePath if ($LASTEXITCODE -ne 0) { throw "测试失败:$target,退出码:$LASTEXITCODE" } } finally { Pop-Location } } } finally { $env:Path = $originalPath if ($null -eq $originalPlatform) { Remove-Item Env:QT_QPA_PLATFORM -ErrorAction SilentlyContinue } else { $env:QT_QPA_PLATFORM = $originalPlatform } }