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

Rename TestCases parameter to ForEach #2311

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/functions/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
# [Switch] $Focus,
[Switch] $Skip,

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '', Justification = 'ForEach is not used in Foreach-Object loop')]
$ForEach
)

Expand Down
1 change: 1 addition & 0 deletions src/functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
# [Switch] $Focus,
[Switch] $Skip,

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '', Justification = 'ForEach is not used in Foreach-Object loop')]
$ForEach
)

Expand Down
27 changes: 14 additions & 13 deletions src/functions/It.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
commenting out a test, because the test remains listed in the output. Use the Strict parameter
of Invoke-Pester to force all skipped tests to fail.

.PARAMETER TestCases
Optional array of hashtable (or any IDictionary) objects. If this parameter is used,
Pester will call the test script block once for each table in the TestCases array,
splatting the dictionary to the test script block as input. If you want the name of
the test to appear differently for each test case, you can embed tokens into the Name
.PARAMETER ForEach
(Formerly called TestCases.) Optional array of hashtable (or any IDictionary) objects.
If this parameter is used, Pester will call the test script block once for each table in
the ForEach array, splatting the dictionary to the test script block as input. If you want
the name of the test to appear differently for each test case, you can embed tokens into the Name
parameter with the syntax 'Adds numbers <A> and <B>' (assuming you have keys named A and B
in your TestCases hashtables.)
in your ForEach hashtables.)

.PARAMETER Tag
Optional parameter containing an array of strings. When calling Invoke-Pester,
Expand Down Expand Up @@ -97,14 +97,14 @@
@{ a = 'two'; b = 'three'; expectedResult = 'twothree' }
)

It 'Correctly adds <a> and <b> to get <expectedResult>' -TestCases $testCases {
It 'Correctly adds <a> and <b> to get <expectedResult>' -ForEach $testCases {
$sum = Add-Numbers $a $b
$sum | Should -Be $expectedResult
}
}
```

Using It with -TestCases to run the same tests with different parameters and expected results.
Using It with -ForEach to run the same tests with different parameters and expected results.
Each hashtable in the `$testCases`-array generates one tests to a total of four. Each key-value pair in the
current hashtable are made available as variables inside It.

Expand All @@ -128,8 +128,9 @@
[Parameter(Position = 1)]
[ScriptBlock] $Test,

[Alias("ForEach")]
[object[]] $TestCases,
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '', Justification = 'ForEach is not used in Foreach-Object loop')]
[Alias("TestCases")]
[object[]] $ForEach,
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

[String[]] $Tag,

Expand Down Expand Up @@ -162,9 +163,9 @@
}
}

if ($PSBoundParameters.ContainsKey('TestCases')) {
if ($null -ne $TestCases -and 0 -lt @($TestCases).Count) {
New-ParametrizedTest -Name $Name -ScriptBlock $Test -StartLine $MyInvocation.ScriptLineNumber -Data $TestCases -Tag $Tag -Focus:$Focus -Skip:$Skip
if ($PSBoundParameters.ContainsKey('ForEach')) {
if ($null -ne $ForEach -and 0 -lt @($ForEach).Count) {
New-ParametrizedTest -Name $Name -ScriptBlock $Test -StartLine $MyInvocation.ScriptLineNumber -Data $ForEach -Tag $Tag -Focus:$Focus -Skip:$Skip
}
else {
# @() or $null is provided do nothing
Expand Down