You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I counted what you need to know to test a module, by walking the path a person actually takes.
To get one passing test against a public function:
get Pester 5/6 loaded and not the Pester 3 that Windows ships
*.Tests.ps1 naming so Invoke-Pester finds it
import in BeforeAll and not at the top of the file
$PSScriptRoot for the path, the working directory is not the test folder
-Force, and removing a stale or differently versioned copy
Describe / It / Should
To test anything internal, or to mock at all, add:
InModuleScope, or -ModuleName on every Mock
BeforeAll variables do not flow into InModuleScope, you need -Parameters
Should -Invoke with -ParameterFilter, and the filter evaluates in module scope too
mocking a command that is not installed on the machine needs a stub function first
To generate tests from the module surface, add:
-ForEach is evaluated at discovery, so the module has to be imported in BeforeDiscovery as well, you import it twice
$_ and hashtable key expansion rules
To run it in CI, add:
configuration object, exit code, Run.Throw
TestResult format and output verbosity
coverage paths, breakpoints or profiler
point all of it at the built module and not at src
6 to say hello, 10 to write a real test, 16 to ship it.
The part that bothers me
Items 3, 7, 8 and 11 are not four problems, they are one problem seen four times. We have two phases, discovery and run, and two scopes, test and module. The user has to hold that 2x2 in their head and put every line of the test file into the right cell. Nothing in the API tells them the grid is there. They find out when they get $null.
How common is the simple case
I checked, because most of the proposals below only work when a repo has one module. 400 repos tagged powershell-module, kept the ones that have Pester test files and at least one .psd1 whose content declares RootModule or ModuleToProcess. n=171.
modules in repo
repos
1
156 (91%)
2
10 (97% cumulative)
3
2
5, 10, 12
1 each
Most of the 2 module ones are not two products, they are one module plus a vendored dependency, a build helper, a template placeholder, or a second manifest for the same module. Genuinely multi module products were 4 of 171.
The content check matters. A first pass that used only path shape counted Pode's src/Locales/*/Pode.psd1 and PSKoans' drafts/ fixtures as modules, and reported 50%.
Proposals
Filed separately so they can be taken or dropped one by one.
I counted what you need to know to test a module, by walking the path a person actually takes.
To get one passing test against a public function:
*.Tests.ps1naming soInvoke-Pesterfinds itBeforeAlland not at the top of the file$PSScriptRootfor the path, the working directory is not the test folder-Force, and removing a stale or differently versioned copyDescribe/It/ShouldTo test anything internal, or to mock at all, add:
InModuleScope, or-ModuleNameon everyMockBeforeAllvariables do not flow intoInModuleScope, you need-ParametersShould -Invokewith-ParameterFilter, and the filter evaluates in module scope tooTo generate tests from the module surface, add:
-ForEachis evaluated at discovery, so the module has to be imported inBeforeDiscoveryas well, you import it twice$_and hashtable key expansion rulesTo run it in CI, add:
Run.Throwsrc6 to say hello, 10 to write a real test, 16 to ship it.
The part that bothers me
Items 3, 7, 8 and 11 are not four problems, they are one problem seen four times. We have two phases, discovery and run, and two scopes, test and module. The user has to hold that 2x2 in their head and put every line of the test file into the right cell. Nothing in the API tells them the grid is there. They find out when they get
$null.How common is the simple case
I checked, because most of the proposals below only work when a repo has one module. 400 repos tagged
powershell-module, kept the ones that have Pester test files and at least one.psd1whose content declaresRootModuleorModuleToProcess. n=171.Most of the 2 module ones are not two products, they are one module plus a vendored dependency, a build helper, a template placeholder, or a second manifest for the same module. Genuinely multi module products were 4 of 171.
The content check matters. A first pass that used only path shape counted Pode's
src/Locales/*/Pode.psd1and PSKoans'drafts/fixtures as modules, and reported 50%.Proposals
Filed separately so they can be taken or dropped one by one.
-ModuleNameis not something you have to know about. Validated rule, no new syntax, falls back to today's behaviour whenever it is unsure.InModuleScopeis doing for people today.Separately, #2992 came out of the same investigation, mocks lose parameter defaults,
ArgumentCompleterand custom attributes.@fflaten
🤖