Invoke Pester - PowerShell


Overview

The Invoke-Pester command is a powerful tool for testing PowerShell scripts and modules. It provides a simple and efficient way to write and execute tests, helping to ensure the reliability and correctness of your code.

Syntax

Invoke-Pester [-TestFile] <String[]> [-Filter] <String> [-OutputFile] <String> [-OutVariable] <String> [-ErrorAction] <ActionPreference> [-Verbose] [-Debug] [-WhatIf] [-Confirm] [CommonParameters]

Options/Flags

  • -TestFile: Specifies the path to the Pester test file to be executed. Multiple files can be specified.
  • -Filter: Filters tests to be executed based on their name or description.
  • -OutputFile: Specifies the path to the file where the test results should be written.
  • -OutVariable: Specifies the variable name where the test results should be stored.
  • -ErrorAction: Controls the behavior when errors occur during test execution.
  • -Verbose: Enables verbose output, providing detailed information about test execution.
  • -Debug: Enables debug output, providing even more detailed information for troubleshooting purposes.
  • -WhatIf: Performs a dry run of the command without actually executing any tests.
  • -Confirm: Prompts for confirmation before executing the tests.

Examples

Simple Example:

Invoke-Pester -TestFile "MyTest.ps1"

Example with Filter:

Invoke-Pester -TestFile "MyTest.ps1" -Filter "MyTest*"

Example with OutputFile:

Invoke-Pester -TestFile "MyTest.ps1" -OutputFile "MyResults.txt"

Common Issues

  • Missing Test Files: Ensure that the specified test files exist and are accessible.
  • Syntax Errors: Check test files for syntax errors before execution.
  • Unexpected Results: Carefully review test logic and expected results to identify any discrepancies.

Integration

Invoke-Pester can be integrated with other PowerShell commands and tools. For example:

  • Use Invoke-Pester with Invoke-Build to automatically test code during the build process.
  • Pipe test results to Out-File to save them to a file.
  • Combine Invoke-Pester with Invoke-CI to create a continuous integration pipeline.