I've been using Richard Berg's style guide for normal powershell code. It has helped a great deal in making my code more consistent, terse, idiomatic, and readable. Pester tests are so different from normal powershell code that that style guide doesn't help a bit for Pester tests.
I've learned some pretty time-consuming lessons with Pester that probably could have been avoided with an opinionated style guide. The main topics where I think I could have benefited from a strong opinion are as follows:
- that multiple
Assert-MockCalled's in a single It block ought to be avoided
- how to deal with powershell's unreliable module auto-loading
- scope and repetition of code across Describe blocks
- scope and variables in
-MockWith script blocks
- scope and variables in
-ParameterFilter script blocks
- when to start a new
Describe block vs continuing an existing Describe block
- how to deal with the material differences in
InModuleScope scope behavior when running a .Test.ps1 using F5 from ISE vs Invoke-Pester
- tradeoffs between using plain-but-repetitive clearly-readable code, and terse-but-error-prone code in
It blocks
- when to use
-Scope It instead of a Context block when using Assert-MockCalled
I'm sure someone with a greater corpus of tests has many more lessons learned that could be shared in a style guide.
Is there a style guide for Pester tests? If so where is it? If not, is this project the right place to create one?
I've been using Richard Berg's style guide for normal powershell code. It has helped a great deal in making my code more consistent, terse, idiomatic, and readable. Pester tests are so different from normal powershell code that that style guide doesn't help a bit for Pester tests.
I've learned some pretty time-consuming lessons with Pester that probably could have been avoided with an opinionated style guide. The main topics where I think I could have benefited from a strong opinion are as follows:
Assert-MockCalled's in a singleItblock ought to be avoided-MockWithscript blocks-ParameterFilterscript blocksDescribeblock vs continuing an existingDescribeblockInModuleScopescope behavior when running a.Test.ps1usingF5from ISE vsInvoke-PesterItblocks-Scope Itinstead of aContextblock when usingAssert-MockCalledI'm sure someone with a greater corpus of tests has many more lessons learned that could be shared in a style guide.
Is there a style guide for Pester tests? If so where is it? If not, is this project the right place to create one?