Pester: Loading PowerShell Modules for Testing

When you’re testing PowerShell modules, importing, referencing, and removing modules can be a little bit tricky. These are some things that I’ve found to make tests more reliable.

Use $PSScriptRoot Environment Variable

Modules should be imported using the $PSSCRIPTROOT environment variable. This environment variable allows the module path to be relative so that the test will execute correctly regardless of the path where invoke-pester is run. If you use a normal relative reference(./) the root folder would be the location from which the test session started. $PSSCRIPTROOT makes the reference relative to the test file and allows Pester to be invoked from any directory.

import-module $PSSCRIPTROOT/Invoke-Something.psm1

Force Module Loading

For local module imports be sure to -Force the import so that the test will always load the latest module and not a cached version from the previous test execution.

By default, if the module is already imported into the context then PowerShell will not re-import it. This is problematic because if the module file logic has changed, it will not be reloaded for the test.

import-module $PSSCRIPTROOT/Invoke-Something.psm1 -Force

Use BeforeAll to Import Modules

Import modules for testing in BeforeAll blocks to ensure the modules are loaded as part of the test execution and to prevent conflicts between tests. When the logic is outside the context of Pester sections it is executed as the files are loaded during discovery. This means all this logic is executed before any of the tests are run. To prevent this, be sure to wrap all setup code, including module importing, in BeforeAll statements.

BeforeAll {
    Import-Module $PSScriptRoot/Invoke-Something.psm1 -Force
}

Use AfterAll to Remove Modules

To ensure that imported module dependencies do not conflict between tests make sure to remove any imported modules after the test.

AfterAll {
    Remove-Module Invoke-Something -Force
}

Following these rules should help ensure the reliability of your Pester Module test. Happy Testing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.