-
-
Notifications
You must be signed in to change notification settings - Fork 468
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
Data driven tests incorrectly generate the $null case #2320
Comments
I've had found that behavior of Let's have a param($makeStrict)
if ($makeStrict) {
Set-StrictMode -Version Latest
Write-Host "latest StrictMode!"
}
Write-Host "magic module imported" Then, with a test file like: BeforeDiscovery {
$makeModuleStrict = $false
# $makeModuleStrict = $true
Import-Module "$PSScriptRoot\magic-module.psm1" `
-Force -ArgumentList $makeModuleStrict
}
Describe "Pester 'ForEach' support" {
InModuleScope "magic-module" {
It "provides `$_ with: '<_>'" -ForEach @($null) {
Test-Path variable:_ | Should -BeTrue
}
}
} Then, output will show that
And, when module under investigation sets strict-mode (i.e.
|
Thanks for the detailed issue and repro!
This hashtable is actually inherited. In your example it's the default empty hashtable
We should probably differentiate between no data or a null-value inside an array. 🙂 |
When you use |
Thanks for very fast response 🙂 I'm not sure if I understood you correctly, in regard to Describe "Pester 'ForEach' support" {
InModuleScope "magic-module" {
It "provides `$_ with: '<_>'" -ForEach `
@("stuff", $null, 42, @(), $false) `
{
Test-Path variable:_ | Should -BeTrue
@("stuff", $null, 42, @(), $false) `
| Should -Contain $_
}
}
} Then, the result for
Moreover, I'm not sure what should change there with
Well to be clear, as in my use-case, I have a list of different values, where the |
PS / Off Topic: I've learned one strange thing with PowerShell itself today 😕, as I was writing the comment above, and wanted to have The thing is: a list with Describe "This is PowerShell" {
It "makes a list with `$true in it, to contains almost anything, like: '<_>'" -ForEach `
@($false, "other", "", 24, @(), 3.14, [DateTime]"1989-05-15", @{ x = "y" }, 0, $null) {
$theList = @("someting", 42, $(Get-Date), @(1, 2, 3))
$theList -contains $_ | Should -BeFalse
$theList += $true
$theList -contains $_ | Should -BeTrue
}
} |
The other values are not PowerShell use scopes to isolate local changes to variables. Every function/scriptblock runs in a new child scope by default. The new scope inherits variables from parent scopes, but any changes are lost when the scope is removed (function/scriptblock is done). Pester follows the same pattern. When All scopes are associated with a session state. A session state is a isolated "world" with it's own set of scopes. They can't see each other (except global-scoped variables). Each module has it's own state (and scopes). When you invoke a function from a module or use Get-Module Demo | Remove-Module
New-Module Demo { } | Import-Module
# Invoking scriptblock in new scope to avoid defining a global variable (that's the exception to the rule)
& {
$myvar = 'outer scope value'
Write-Host "Running in session state '$($ExecutionContext.SessionState.Module)' - myvar is: '$myvar'"
& {
Write-Host "Running in session state '$($ExecutionContext.SessionState.Module)' - myvar is inherited: '$myvar'"
$myvar = 'inner scope value'
Write-Host "Running in session state '$($ExecutionContext.SessionState.Module)' - myvar is changed in current scope: '$myvar'"
InModuleScope Demo {
Write-Host "Running in session state '$($ExecutionContext.SessionState.Module)' - myvar is not visible: '$myvar'"
}
}
}
# output - state '' below means "script state" (default console session)
Running in session state '' - myvar is: 'outer scope value'
Running in session state '' - myvar is inherited: 'outer scope value'
Running in session state '' - myvar is changed in current scope: 'inner scope value'
Running in session state 'Demo' - myvar is not visible: ''
Yes, they are not the same scenario. Your testcase isn't skipped, but the value is just not mapped to As a workaround for now, you can use a hashtable per testcase. Then you can access the value with It "provides `$_ with: '<ValueToTest>'" -ForEach @(
@{ ValueToTest = 'stuff' },
@{ ValueToTest = $null },
@{ ValueToTest = 42 },
@{ ValueToTest = @() },
@{ ValueToTest = $false }) {
Test-Path variable:ValueToTest | Should -BeTrue
@('stuff', $null, 42, @(), $false) | Should -Contain $ValueToTest
} |
Thanks for your detailed explanation 🙂 Yes, I've made that workaround too, and I think that using items like: @{ Tested = $whatever; What = "useful and human-readable" } is often even better than just pure values, as one has a "full-control" over test cases name. |
Checklist
What is the issue?
There is a great feature, where one can generate many tests based on an array of examples using
-ForEach
or-TestCases
parameters. However, for the$null
case, the test does not get that$null
under$_
variable.Expected Behavior
Generated parametrized test when executing should obtain given value under
$_
- i.e.$true
when test cases array contains$true
, a42
if there is that number, and the$null
when list contains that.Moreover, templated name of it, should present
$null
(or something "meaningful") and notSystem.Collections.Hashtable
as currently. I'm not sure if the$null
is the-correct-name, as currently-ForEach
renders simple values like:@()
->
- a little bit "unmeaning"@{}
->System.Collections.Hashtable
$null
->System.Collections.Hashtable
$true
/$false
->True
/False
Should -BeTrue
renders them as:Expected $true, but got $false.
Steps To Reproduce
Have a test like this one:
Then, when executed:
Describe your environment
Possible Solution?
No response
The text was updated successfully, but these errors were encountered: