powershellunit-testingpester

PowerShell Tests: Module or Individual Files


Background

I have a PowerShell module where I am starting to write the tests. Each function is stored in its own file with the same name (e.g. function MyFunction is stored in a file called MyFunction.ps1). Similarly, the tests are stored in a file called MyFunction.Tests.ps1. The structure is as below.

Source\
    Public\
        MyFunction.ps1
    Private\
Tests\
    MyFunction.Tests.ps1

In order to make the module faster to load, during build, all functions are copied into the .psm1 file.

Question

Should the tests (unit tests with code coverage, integration, regression, etc) be run against the function in the individual .ps1 files or against the whole .psm1 file? Any information on the pros and cons of each would be appreciated.


Solution

  • during build, all functions are copied into the .psm1 file.

    Generally speaking, you should always test the code that is actually used at runtime, which means you should test against the .psm1 file.

    This guards against hiding the following potential problems, for instance: