8000 update test framework and tests to support 4x version of Pester by JamesWTruher · Pull Request #6064 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

update test framework and tests to support 4x version of Pester #6064

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

Merged
Prev Previous commit
Next Next commit
[feature] Add link for migrating tests from Pester v3 to v4
Fix up capitalization and white space issues
Change one test to check FullyQualifiedErrorId rather than just `Should Throw`
  • Loading branch information
JamesWTruher committed Feb 2, 2018
commit 3f8df89f6ca049d9f8a433e455d8b767b3a9996e
20 changes: 12 additions & 8 deletions docs/testing-guidelines/WritingPesterTests.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
### Writing Pester Tests
Note that this document does not replace the documents found in the [Pester](https://github.com/pester/pester "Pester") project. This is just
some quick tips and suggestions for creating Pester tests for this project. The Pester community is vibrant and active, if you have questions
about Pester or creating tests, the [Pester Wiki](https://github.com/pester/pester/wiki) has a lot of great information.
Note that this document does not replace the documents found in the [Pester](https://github.com/pester/pester "Pester") project.
This is just some quick tips and suggestions for creating Pester tests for this project.
The Pester community is vibrant and active, if you have questions about Pester or creating tests, the [Pester Wiki](https://github.com/pester/pester/wiki) has a lot of great information.
As of January 2018, PowerShell Core is using Pester version 4x which has some changes from earlier versions.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speller test failed on "4x"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corresponding error in Travis CI:

The command "pwsh -File tools/travis.ps1" exited with 0.
2.37s$ if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then mdspell '**/*.md' '!**/Pester/**/*.md' --ignore-numbers --ignore-acronyms --report; fi
    docs/testing-guidelines/WritingPesterTests.md
        5 |  Core is using Pester version 4x which has some changes from e 
>> 1 spelling error found in 67 files

See [Migrating from Pester 3 to Pester 4](https://github.com/pester/Pester/wiki/Migrating-from-Pester-3-to-Pester-4) for more information.



When creating tests, keep the following in mind:
* Tests should not be overly complicated and test too many things
Expand All @@ -16,7 +20,7 @@ Here's the simplest of tests
Describe "A variable can be assigned and retrieved" {
It "Create a variable and make sure its value is correct" {
$a = 1
$a | Should be 1
$a | Should Be 1
}
}
```
Expand All @@ -27,7 +31,7 @@ If you need to do type checking, that can be done as well
Describe "One is really one" {
It "Compare 1 to 1" {
$a = 1
$a | Should be 1
$a | Should Be 1
}
It "1 is really an int" {
$i = 1
Expand All @@ -42,7 +46,7 @@ alternatively, you could do the following:
Describe "One is really one" {
It "Compare 1 to 1" {
$a = 1
$a | Should be 1
$a | Should Be 1
}
It "1 is really an int" {
$i = 1
Expand Down Expand Up @@ -136,7 +140,7 @@ $testCases = @(
Describe "A test" {
It "<a> -xor <b> should be <expectedresult>" -testcase $testcases {
param ($a, $b, $ExpectedResult)
$a -xor $b | Should be $ExpectedResult
$a -xor $b | Should Be $ExpectedResult
}
}
```
Expand All @@ -151,7 +155,7 @@ The following example illustrates simple use:
Context "Get-Random is not random" {
Mock Get-Random { return 3 }
It "Get-Random returns 3" {
Get-Random | Should be 3
Get-Random | Should Be 3
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Describe "Get-Variable" -Tags "CI" {
New-Variable globalVar -Value 1 -Scope global -Force

Get-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue -ErrorVariable removeGlobalAsLocal
$removeGlobalAsLocal.FullyQualifiedErrorId | Should Be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
$removeGlobalAsLocal.FullyQualifiedErrorId | Should Be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
}

It "Should be able to get a global variable when there's one in the script scope" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Describe "Remove-Variable" -Tags "CI" {
It "Should throw an error when a dollar sign is used in the variable name place" {
New-Variable -Name var1 -Value 4

{ Remove-Variable $var1 -ErrorAction Stop } | Should Throw
Remove-Variable $var1 -ErrorAction SilentlyContinue -ErrorVariable err
$err.FullyQualifiedErrorId | Should Be "VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}

It "Should not throw error when used without the Name field, and named variable is properly specified and exists" {
Expand Down Expand Up @@ -138,7 +139,7 @@ Describe "Remove-Variable" -Tags "CI" {

{ Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should Throw

$var1 | Should be context
$var1 | Should Be context
}

It "Should be able to remove an item locally using the global switch" {
Expand Down Expand Up @@ -200,7 +201,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
}

Expand All @@ -214,7 +215,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo| Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
Remove-Variable foo -Force
$var1 = Get-Variable -Name foo -EA SilentlyContinue
Expand All @@ -231,7 +232,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}

try
Expand All @@ -242,7 +243,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
}

Expand All @@ -258,7 +259,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "ItemNotFoundException"
$_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
}
}

Expand Down
0