function Invoke-UnitTest { <# .SYNOPSIS Runs all Pester tests within this repo. .DESCRIPTION This Cmdlet is used in Drone pipeline to perform the Pester based unit tests. .PARAMETER CoverageFormat Pester provides the formats JaCoCo ans CoverageGutters. Default is JaCoCo. These are the known use cases: - JaCoCo -> Used as standard coverage report used by sonar - CoverageGutters -> Custom Format to show coverage in VSCode. .PARAMETER Verbosity This parameter sets the Pester detail level. Default is 'Normal.' Available values are: 'None', 'Normal', 'Detailed', 'Diagnostic' .PARAMETER PassThru Tells Invoke-UnitTest to write back the Pester results into your variable / output. .PARAMETER Tag Pester build in tag filter as string array. .PARAMETER ExcludeTag Pester build in exclude filter for tests as string array. .INPUTS [None] No pipeline input. .OUTPUTS [None] No pipeline output. .EXAMPLE Invoke-UnitTest #> [CmdletBinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseConsistentWhitespace', '', Justification = 'Hashtable bug in ScriptAnalyzer 1.19.1' )] param ( [Parameter( Mandatory = $false )] [ValidateSet('JaCoCo', 'CoverageGutters')] [string]$CoverageFormat = 'JaCoCo', [Parameter(Mandatory = $false)] [ValidateSet('None', 'Normal', 'Detailed', 'Diagnostic')] [string]$Verbosity = 'Normal', [Parameter(Mandatory = $false)] [switch]$PassThru, [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [string[]]$Tag, [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [string[]]$ExcludeTag ) process { $Repo = Get-RepoPath Write-Verbose -Message '===== Running Pester =====' -Verbose:$VerbosePreference $PesterConf = New-PesterConfiguration $PesterConf.Run.Path = (Resolve-Path -Path './src').Path $PesterConf.Run.Exit = $false $PesterConf.Run.PassThru = $true $PesterConf.CodeCoverage.Enabled = $true $PesterConf.CodeCoverage.OutputFormat = $CoverageFormat $PesterConf.TestResult.Enabled = $true $CovFiles = Get-ChildItem -Path $Repo.Src.PS1Filter -Recurse | Where-Object { $_.BaseName -notmatch '.Tests' } | Select-Object -ExpandProperty 'FullName' $PesterConf.CodeCoverage.Path = $CovFiles $PesterConf.Output.Verbosity = $Verbosity # Set Tags if given if ($Tag) { $PesterConf.Filter.Tag = $Tag } if ($ExcludeTag) { $PesterConf.Filter.ExcludeTag = $ExcludeTag } $TestResults = Invoke-Pester -Configuration $PesterConf -ErrorAction 'Stop' try { $ResFileParams = @{ InputObject = $TestResults Path = $Repo.Build.PesterLogPath Type = 'Pester' ErrorAction = 'Stop' } Write-ResultFile @ResFileParams } catch { Write-FailureStateFile -StepName 'Pester' throw ('{0} tests failed!' -f $TestResults.FailedCount) } if ($TestResults.FailedCount -gt 0) { Write-FailureStateFile -StepName 'Pester' throw ('{0} tests failed!' -f $TestResults.FailedCount) } if ($PassThru.IsPresent) { Write-Output -InputObject $TestResults } } }