Home > Net >  PowerShell Tests: Module or Individual Files
PowerShell Tests: Module or Individual Files

Time:01-16

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.

CodePudding user response:

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:

  • Subtle behavioral differences between code directly placed in a scope and loaded from an external script via dot-sourcing.

    • Similarly, if were to test your .ps1 files in isolation (if even feasible), you could miss potential (unwanted) interaction between the code in the individual files when placed in a single .psm1 file; also, .psm1 files behave differently than .ps1 files.
  • Flaws in the build script that creates the single .psm1 file.

  • Related