[CmdletBinding()] param( [ValidateSet('Debug', 'Release')] [string]$Configuration = 'Debug', [ValidateSet('Functional', 'Performance', 'All')] [string]$Suite = 'Functional' ) 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" } } $functionalTargets = @( 'domain_tests', 'application_settings_tests', 'alarm_service_tests', 'hmi_editor_service_tests', 'logic_editor_service_tests', 'offline_simulation_service_tests', 'project_management_tests', 'register_monitor_service_tests', 'runtime_project_bundle_tests', 'runtime_settings_tests', 'runtime_mode_service_tests', 'runtime_panel_controller_tests', 'plc_connection_dialog_tests', 'plc_runtime_tests' ) $performanceTargets = @('performance_tests') $targets = switch ($Suite) { 'Functional' { $functionalTargets } 'Performance' { $performanceTargets } 'All' { $functionalTargets + $performanceTargets } } $originalPath = $env:Path $env:Path = "$qtRoot\bin;$mingwBin;$env:Path" Write-Host "测试类型:$Suite" 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" if ($target -eq 'performance_tests') { $benchmarkResultPath = Join-Path $buildDirectory 'benchmark.csv' & $executablePath '-o' "$benchmarkResultPath,csv" } else { & $executablePath } if ($LASTEXITCODE -ne 0) { throw "测试失败:$target,退出码:$LASTEXITCODE" } if ($target -eq 'performance_tests') { Write-Host "性能结果:$benchmarkResultPath" Get-Content -LiteralPath $benchmarkResultPath -Encoding utf8 } } finally { Pop-Location } } } finally { $env:Path = $originalPath }