Import-Module triggers Pester tests to run

I’m new to Pester and trying to use it as an excuse to reorganize my PS modules. Currently, I have put my functions in the .psm1 file, so my modules have a flat folder structure with just the .psm1, .psd1, a test file, and some PowerShell Studio project files.

With the introduction of Pester, I’m trying to adopt a structure where:

  • Each function resides in an individual .ps1 file in .\Functions\Public or .\Functions\Private.
  • The .psm1 file has a generic process that dot sources each of the public and private functions and exports the public functions.
  • The Pester tests will either reside at .\Tests or .\Functions\Public\Tests or .\Functions\Private\Tests (I haven't decided yet). They will also likely be broken into Unit and Integration folders, but again, not decided yet.
The initial problem I'm having is that when I run Import-Module <module_name>, the Pester tests are getting executed. I don't understand what is triggering this, so I'm not sure how to stop it, but it's definitely not desired. Any ideas are greatly appreciated.

Additionally, I’m interested in any thoughts on this module organization and any recommended changes. Thanks.

I resolved the auto-execution of the Pester tests when I found and deleted a test that was still in the same folder as .ps1 script files. However, I’m still interested in how others structure their modules.

Slightly out of date at this point (I now generally use a source folder), but:

  • Source Folder containing
    • Public Folder, with individual public function .ps1 files
    • Private Folder, with individual private function .ps1 files
    • Classes / Data folders if required
  • Tests folder, containing the same structure as source - one unit test file per function, integration tests in a separate folder.
  • Build / Pipelines / GitVersion files in the root

We use ModuleBuilder which takes the arrangement above (plus some arguments, maybe in a Build.psd1) and “compiles” the different files into a single PSD1/PSM1 (and automatically handles exporting the public functions). This results in a significant performance increase when signing and importing the module.

Edit: Really need to do an update pass on that example repository.

I have been using Pester for about six months for this project. During this time he managed to adjust the program to his requirements. However, I still have tests. I will try to use your tips.