Skip to content

Is there a style guide for Pester tests? #317

Description

@alx9r

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions