#!/usr/bin/env pwsh <# .SYNOPSIS Focused Apex test execution wrapper for Salesforce CLI .DESCRIPTION A user-friendly wrapper around 'sf apex run test' that simplifies test execution with better formatting, intelligent defaults, and comprehensive reporting options. .PARAMETER TestClasses Comma-separated list of test class names to run .PARAMETER TestMethods Comma-separated list of specific test methods to run (format: ClassName.methodName) .PARAMETER TestLevel Test level to run (RunLocalTests, RunAllTestsInOrg, RunSpecifiedTests) .PARAMETER Suite Test suite name to run .PARAMETER Coverage Generate code coverage report .PARAMETER Wait Wait time in minutes for test execution (default: 10) .PARAMETER TargetOrg Target org username or alias (uses default if not specified) .PARAMETER OutputDir Directory to store test results and reports .PARAMETER Verbose Enable verbose test output .PARAMETER Help Show this help message .EXAMPLE .\sf-test-run.ps1 -TestClasses "AccountTest,ContactTest" .\sf-test-run.ps1 -TestLevel "RunLocalTests" -Coverage .\sf-test-run.ps1 -TestMethods "AccountTest.testCreate,ContactTest.testUpdate" .\sf-test-run.ps1 -Suite "AllTests" -Wait 15 -TargetOrg "staging" .NOTES This script automatically checks for Salesforce CLI installation and runs diagnostics if the CLI is not found. #> param( [Parameter(ParameterSetName="Classes")] [string]$TestClasses, [Parameter(ParameterSetName="Methods")] [string]$TestMethods, [Parameter(ParameterSetName="Level")] [ValidateSet("RunLocalTests", "RunAllTestsInOrg", "RunSpecifiedTests")] [string]$TestLevel, [Parameter(ParameterSetName="Suite")] [string]$Suite, [switch]$Coverage, [int]$Wait = 10, [string]$TargetOrg, [string]$OutputDir, [switch]$Verbose, [switch]$Help ) # Show help if requested if ($Help) { Get-Help $MyInvocation.MyCommand.Path -Detailed exit 0 } # Function to check if Salesforce CLI is installed function Test-SalesforceCLI { try { $null = Get-Command sf -ErrorAction Stop return $true } catch { return $false } } # Function to run sf-check diagnostics function Invoke-SalesforceCheck { $checkScript = if (Test-Path "sf-check.ps1") { ".\sf-check.ps1" } elseif (Test-Path "sf-check.sh") { "bash sf-check.sh" } else { $null } if ($checkScript) { Write-Host "Running Salesforce CLI diagnostics..." -ForegroundColor Yellow Invoke-Expression $checkScript } else { Write-Host "Salesforce CLI not found and no diagnostic script available." -ForegroundColor Red Write-Host "Please install the Salesforce CLI: https://developer.salesforce.com/tools/salesforcecli" -ForegroundColor Red } } # Silently check for Salesforce CLI if (-not (Test-SalesforceCLI)) { Invoke-SalesforceCheck exit 1 } # Validate that at least one test specification is provided $testSpecCount = @($TestClasses, $TestMethods, $TestLevel, $Suite | Where-Object { $_ }).Count if ($testSpecCount -eq 0) { Write-Host "Error: Must specify one of: -TestClasses, -TestMethods, -TestLevel, or -Suite" -ForegroundColor Red Write-Host "" Write-Host "Usage examples:" -ForegroundColor Yellow Write-Host " .\sf-test-run.ps1 -TestClasses `"AccountTest,ContactTest`"" -ForegroundColor Gray Write-Host " .\sf-test-run.ps1 -TestLevel `"RunLocalTests`" -Coverage" -ForegroundColor Gray Write-Host " .\sf-test-run.ps1 -TestMethods `"AccountTest.testCreate`"" -ForegroundColor Gray Write-Host " .\sf-test-run.ps1 -Suite `"AllTests`"" -ForegroundColor Gray Write-Host "" Write-Host "Use -Help for detailed usage information." -ForegroundColor Yellow exit 1 } # Build the sf command $sfArgs = @("apex", "run", "test") # Add test specification if ($TestClasses) { $sfArgs += "--class-names" $sfArgs += $TestClasses Write-Host "Running test classes: $TestClasses" -ForegroundColor Green } elseif ($TestMethods) { $sfArgs += "--tests" $sfArgs += $TestMethods Write-Host "Running test methods: $TestMethods" -ForegroundColor Green } elseif ($TestLevel) { $sfArgs += "--test-level" $sfArgs += $TestLevel Write-Host "Running tests at level: $TestLevel" -ForegroundColor Green } elseif ($Suite) { $sfArgs += "--suite-names" $sfArgs += $Suite Write-Host "Running test suite: $Suite" -ForegroundColor Green } # Add optional parameters if ($TargetOrg) { $sfArgs += "--target-org" $sfArgs += $TargetOrg Write-Host "Target org: $TargetOrg" -ForegroundColor Cyan } if ($Wait -ne 10) { $sfArgs += "--wait" $sfArgs += $Wait.ToString() } if ($Coverage) { $sfArgs += "--code-coverage" Write-Host "Code coverage: Enabled" -ForegroundColor Yellow } if ($OutputDir) { if (-not (Test-Path $OutputDir)) { Write-Host "Creating output directory: $OutputDir" -ForegroundColor Yellow New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null } $sfArgs += "--output-dir" $sfArgs += $OutputDir } # Always use detailed output for better reporting $sfArgs += "--detailed-coverage" $sfArgs += "--result-format" $sfArgs += "human" # Add verbose flag if requested if ($Verbose) { $sfArgs += "--verbose" } # Display test execution info Write-Host "" Write-Host "๐Ÿงช Starting Apex Test Execution" -ForegroundColor Blue Write-Host "================================" -ForegroundColor Blue # Display the command being run Write-Host "" Write-Host "Executing: sf $($sfArgs -join ' ')" -ForegroundColor Gray Write-Host "" # Execute the command try { $startTime = Get-Date & sf @sfArgs $exitCode = $LASTEXITCODE $endTime = Get-Date $duration = $endTime - $startTime Write-Host "" Write-Host "โฑ๏ธ Test execution completed in $($duration.TotalSeconds.ToString('F1')) seconds" -ForegroundColor Gray if ($exitCode -eq 0) { Write-Host "" Write-Host "โœ… Tests completed successfully!" -ForegroundColor Green if ($Coverage) { Write-Host "๐Ÿ“Š Code coverage report generated" -ForegroundColor Yellow } if ($OutputDir) { Write-Host "๐Ÿ“ Results saved to: $OutputDir" -ForegroundColor Cyan } } else { Write-Host "" Write-Host "โŒ Test execution failed with exit code: $exitCode" -ForegroundColor Red Write-Host "๐Ÿ’ก Check test failures above for details" -ForegroundColor Yellow exit $exitCode } } catch { Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red exit 1 }