Skip to content

Instantly share code, notes, and snippets.

@OCram85
Last active July 28, 2017 08:56
Show Gist options
  • Save OCram85/859bc85101879cbb3cf609d583b171f4 to your computer and use it in GitHub Desktop.
Save OCram85/859bc85101879cbb3cf609d583b171f4 to your computer and use it in GitHub Desktop.
Pester Test Header
#region HEADER
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = (Get-GitDirectory).replace('\.git', '')
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
$sut = $sut -replace "\d{2}`_", ''
$suthome = (Get-ChildItem -Path $RepoRoot -Exclude ".\tests\" -Filter $sut -Recurse).FullName
# Skip try loading the source file if it doesn't exists.
If ($suthome.Length -gt 0) {
. $suthome
}
Else {
Write-Warning ("Could not find source file {0}" -f $sut)
}
# load additional functions defined in the repository. Replace the expression <FunctionName>.
# . (Get-ChildItem -Path $RepoRoot -Filter "<Function-Name>.ps1" -Recurse).FullName
#endregion HEADER
@OCram85
Copy link
Author

OCram85 commented Jul 28, 2017

Usage

  • Requieres posh-git module to find the git repository root directory.
  • File has to be named like the following pattern: ^(\d{2})_<SourceFileName>.Tests.ps1$
    • Example:
      • Source file : Invoke-SomeTasks.ps1
      • Pester file: 01_Invoke-SomeTasks.Tests.ps1
  • If you need additonal files loaded before executing the tests use line 27. Uncomment it and fill the replace the

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment