Skip to content
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

Default parameter values are not applied in the test script #1980

Closed
Glober777 opened this issue Jun 9, 2021 · 3 comments · Fixed by #1986
Closed

Default parameter values are not applied in the test script #1980

Glober777 opened this issue Jun 9, 2021 · 3 comments · Fixed by #1986

Comments

@Glober777
Copy link

General summary of the issue

I'm working on a new test script and I need it to have a parameter defining the environment to be tested (Dev/Prod). I configured it to use Dev as a default, however it seems that those defaults are just ignored.

Describe your environment

Pester version     : 5.2.0 C:\Users\username\OneDrive - PPD\Documents\PowerShell\Modules\Pester\5.2.0\Pester.psm1
PowerShell version : 7.1.3
OS version         : Microsoft Windows NT 10.0.19042.0

Steps to reproduce

Test script:

[CmdletBinding()]
param (
    [Parameter()]
    [string]
    [ValidateSet('DEV', 'PROD')]
    $Environment = 'DEV'
)
 
Describe "My tests" {
    It "Environment parameter should be accessible" {
        $Environment | Should -BeExactly 'DEV'
    }
}

Running it either with Invoke-Pester -Path or with VSCode's Debug tests intellisense link resolves with a failed test:

[-] My tests.Environment parameter should be accessible 4ms (3ms|1ms)
 Expected exactly 'DEV', but got $null.
 at $Environment | Should -BeExactly 'DEV', C:\Users\username\Desktop\Tests\MyPester.Tests.ps1:11
 at <ScriptBlock>, C:\Users\username\Desktop\Tests\MyPester.Tests.ps1:11

-->

It only works if I initialize a PesterContainer with the Data provided

Expected Behavior

I wish it would pick up the default value for the environment, cause that would make it so much easier to debug it through VSCode

Current Behavior

Defaults for script parameters are completely ignored

Possible Solution? (optional)

I can workaround the script to use environmental variables to drive the tests, but this approach seems more intuitive.

@fflaten
Copy link
Collaborator

fflaten commented Jun 10, 2021

Parameters in the script are treated the same as variables defined in BeforeDiscovery (or outside any *All/*Each/It-blocks) - they are only available in the Discovery-phase by default and used to generate tests. See https://pester.dev/docs/usage/data-driven-tests#variables-defined-during-discovery-are-not-available-in-run .
Those variables won't be available in the Run-phase when the *All/*Each/It-blocks are executed unless you explicitly bind them to a block. You can do this with -ForEach on Describe/Context/It-block just like testcases. Try the following sample. It should work like you'd expect.

[CmdletBinding()]
param (
    [Parameter()]
    [string]
    [ValidateSet('DEV', 'PROD')]
    $Environment = 'DEV'
)
 
# Using the same key-name as original parameter name just to make it clean.
# -ForEach @{ MyVar = $Environment } would require you to use $MyVar | Should -BeExactly 'DEV'  in the test
Describe "My tests" -ForEach @{ Environment = $Environment } {
    It "Environment parameter should be accessible" {
        $Environment | Should -BeExactly 'DEV'
    }
}

The reason Invoke-Pester -Container (New-PesterContainer -Path ./demoIssue1980.tests.ps1 -Data @{ Environment = "PROD" }) works is because the Data-hashtable is linked to a block just like we did above with -ForEach ... The key difference is that the hashtable is splatted in discovery as well and during Run-phase it is linked to the "root-block" representing the whole container (file).

@nohwnd
Copy link
Member

nohwnd commented Jun 11, 2021

I wish it would pick up the default value for the environment, cause that would make it so much easier to debug it through VSCode

Yeah, valid.

@fflaten Even with the workaround you provided. I think we should look into detecting the default values from the parameter set and assigning them as default Data to the root, because then you can use them in the top-level BeforeAll. And it also works nicely if you have more than 1 parameter with default value.

@fflaten
Copy link
Collaborator

fflaten commented Jun 11, 2021

I agree. The comment was meant as a workaround and explanation for now while I figured out if something better is even possible. Think I might have an idea in upcoming draft PR, though I don't think I'm able to detect default values set to $null. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants