-
-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PSBoundParameters is empty in v5 at runtime of tests #1828
Comments
Note to self: The Data should be merged with the splat to allow this in It and BeforeAll etc..
|
This is way more difficult than #1542 and has little benefit at the moment. Moving it out of the milestone. To make this work consistently we would have to merge all parent data with the internal scriptBlock contexts (like $Pester object) when invoking every block. Which seems costly for almost no benefit because the variables nicely flow down using variable inheritance. |
I’ll look to put it in the docs somewhere that one shouldn’t use it |
Was about to add it to breaking changes from v4 to v5, but I can't really reproduce it in Pester 4.10.1.
|
@simonsabin friendly ping on this :) |
The repro is a little confusing. It shows that whilst PSBoundParameters is not set, the variable $foo has been set. |
The issue mentions this behavior as a v5-regression, but I can't even get it to work in v4. Can you? This affects whether it should be documented in breaking changes or just some general doc. |
When you run this code, for the v 4.10.1 The output from the script block outside of the describe runs and has PSBoundparameters assigned see my results below. get-module pester | remove-module
import-module pester -MaximumVersion 4.10.1
{
param($foo)
write-verbose "Pester version $((get-module pester).Version)" -verbose
Write-verbose "Before Test Not in any code block" -Verbose
Write-verbose "Bound Parameters is " -Verbose
Write-verbose ($PSBoundParameters| Convertto-json |out-string) -Verbose
Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
Describe "Test"{
It "Test psboundparameters"{
Write-verbose "During Test" -Verbose
Write-verbose "Bound Parameters is " -Verbose
Write-verbose ($PSBoundParameters| Convertto-json |out-string) -Verbose
Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
}
}
} |out-file ".\my.tests.ps1";
invoke-pester -Script @{Path=".\my.tests.ps1";Parameters=@{foo="A new value for foo"}}
remove-module Pester
import-module pester -MinimumVersion 5.0.0
{
param($foo)
BeforeDiscovery{
write-verbose "Pester version $((get-module pester).Version)" -verbose
Write-verbose "Before Test in BeforeDiscovery" -Verbose
Write-verbose ($PSBoundParameters| convertto-json|out-string) -Verbose
Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
}
BeforeAll{
write-verbose "Pester version $((get-module pester).Version)" -verbose
Write-verbose "Before Test in BeforeAll" -Verbose
Write-verbose ($PSBoundParameters| convertto-json|out-string) -Verbose
Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
}
Describe "Test"{
It "Test psboundparameters"{
Write-verbose "During Test" -Verbose
Write-verbose "Bound Parameters is " -Verbose
Write-verbose ($PSBoundParameters| Convertto-json |out-string) -Verbose
Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
}
}
}|out-file ".\my.tests2.ps1";
invoke-pester -Container (New-PesterContainer -Path ".\my.tests2.ps1" -Data @{foo="A new value for foo"})
My results are as below
Pester v4.10.1 Executing script .\my.tests.ps1 VERBOSE: The Variable is Foo is set to =(A new value for foo) Describing Test VERBOSE: The Variable is Foo is set to =(A new value for foo) Starting discovery in 1 files. VERBOSE: The Variable is Foo is set to =(A new value for foo) VERBOSE: The Variable is Foo is set to =(A new value for foo) VERBOSE: The Variable is Foo is set to =(A new value for foo)
|
I should add this is in relation to PSBound paramaters in an undefined block outside of describe in V4 compared to a BeforeAll or BeforeDiscovery in V5. Its not in relation to use within an It block |
Aha, thanks! So it's only missing in the discovery phase in v5, It's never been available during test execution (It-blocks in v4, Run-phase in general in v5). Thoughts on where this should be documented on https://pester.dev? A note-block at the end of https://pester.dev/docs/usage/data-driven-tests#providing-external-data-to-tests maybe saying that |
PsBoundParamIssue.ps1.txt
General summary of the issue
PSBoundParameters isn't populated as the scripts are now run in two phases. I believe this is a breaking change in v5.
Rather than checking PSBoundParameters on has to check the variables PSDrive (or other variable checking solution) to check if the variable has been defined, and has a value, i.e. passed in using a PesterContainer
Describe your environment
Pester version : 5.1.1 C:\Users\SimonSabin\OneDrive\Documents2\PowerShell\Modules\Pester\5.1.1\Pester.psm1
PowerShell version : 7.0.1
OS version : Microsoft Windows NT 10.0.19041.0
Steps to reproduce
See Attached file
Expected Behavior
PSBoundParameters is populated with parameters at runtime.
Current Behavior
PSBoundParameters is only available at discovery not at runtime, this is different to the behaviour in v4.
Possible Solution? (optional)
Not sure if a change is required, except to document the change in behaviour.
The text was updated successfully, but these errors were encountered: