Minimize Boilerplate Code in PowerShell Module Pester Test files

34 views Asked by At

I am building a PowerShell module using the PSModuleDevelopment's MiniModule template generator. It makes a project that looks like this:

PROJECT_ROOT/
├─ the_module_name/
│  ├─ internal/
│  │  ├─ internalFunc.ps1
│  ├─ functions/
│  │  ├─ Get-MyThing.ps1
│  ├─ the_module_name,psd1
│  ├─ the_module_name.psm1
├─ tests/
│  ├─ general/
│  ├─ functions/
│  │  ├─ internalFunc.Tests.ps1
│  │  ├─ Get-MyThings.Tests.ps1
│  ├─ pester.ps1
├─ .gitignore
├─ README.md

In my PROJECT_ROOT/tests/functions/Get-MyThings.Tests.ps1 file I have this block of Pester code to load and unload the module so that I can unit test it:

BeforeAll {
    $modName = 'the_module_name'
    Import-Module "$PSScriptRoot\..\..\$($modName)\$($modName).psd1" -Force
}

AfterAll {
    Remove-Module -Name $modName
}

This works but it feels ugly and repetitive and will be even more so as I add more and more test files. Is there a better way to minimize the boiler plate or to organize my project?

0

There are 0 answers