I'm having issues with the using statement which can be seen here. This statement (unlike Import-Module
) is required in order to get an actual class definition from a PowerShell module. I have a use case for this because I need the class definition itself for Pester unit testing.
The problem is I can't find any way to provide the module path dynamically. I'm unable to use variables like $PSScriptPath
in the path. I'm only able to provide it with a absolute path or a relative path. Neither of these really work for me.
- Absolute path: Issue here is unit tests will be run on different machines (build servers). Therefore, having a hardcoded absolute path from a dev machine isn't an option.
- Relative path: This also is problematic, since unit tests are triggered from a base directory which then run all Test files recursively. So I would have to
Set-Location
to specific directories for each test in order for relative paths to function. To tests dozens of modules, this is a lot of overhead to have to manage when I should be able to just run all my unit tests without this hassle.
Is there a better way I can manage this? General directory structure is like this...
src
project1
someModule1
someModule1.psm1
someModule2
someModule2.psm1
someModule3
someModule3.psm1
tests
someModule1.Tests.ps1
someModule2.Tests.ps1
someModule3.Tests.ps1
From src
directory, run Invoke-Pester -Script .
to run all unit test files. The "Tests" files need to be able to import classes from their corresponding modules. Worse yet, I'm not getting any errors when the using
statement doesn't find the module path unless I run the using statement interactively in the console. Any ideas?