Private/New-PesterTestsFile.ps1
Function New-PesterTestsFile { <# .SYNOPSIS Creates a basic Pester tests file (.tests.ps1) for a module. .DESCRIPTION This function generates a starter Pester tests file with a basic structure. It includes code for module loading and a 'Describe' block for your test cases. .PARAMETER Path Required. Specifies the absolute path where the Pester tests file will be created. .PARAMETER Name Required. The name of the module being tested. .EXAMPLE New-PesterTestsFile -Path C:\MyProject\Tests -Name MyModule This creates a file named "MyModule.tests.ps1" in the "C:\MyProject\Tests" directory. .NOTES Author: owen.heaume Version: 1.0.0 - Initial release #> [CmdletBinding()] param ( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Name ) Begin {} Process { try { $TestPath = Join-Path -Path $Path -ChildPath "$name.tests.ps1" -ea Stop } catch { throw "Error joining path: $_" } $pesterContent = @" `$moduleName = '$name' `$modulePath = Join-Path -Path `$PSScriptRoot -ChildPath "..\`$moduleName\`$moduleName.psm1" Get-Module `$moduleName | Remove-Module -Force Import-Module `$modulePath -Force Describe "<Your-FunctionName>" { } "@ try { Write-Host "Creating Pester tests file: $TestPath" -ForegroundColor DarkCyan if (Test-Path $TestPath) { Write-Host "$name.tests.ps1 file already exists" -ForegroundColor DarkYellow } else { $pesterContent | Out-File -FilePath $TestPath -ea Stop Write-Host "$name.tests.ps1 file created successfully" -ForegroundColor DarkGreen } } catch { throw "Error writing $name.tests.ps1 file: $_" } } End {} } |