Monday, January 4, 2021

PowerShell: Modules (Convert PSUnit.ps1 to a Module)

This post demonstrates how to create a PowerShell module (PSUnit.psm1) and consume/test the functions exported by the module (PSUnitTest.ps1).  The source code for the scripts presented here is contained in a comprehensive post PowerShell: A Practical Example (Logging and Testing). Modules can be compiled code (DLL's) or can be composed PowerShell. For this post, the modules is implemented in using PowerShell and therefore uses the standard PowerShell module file extension psm1. 

The basic unit testing framework was originally presented as script, PSUnit.ps1, in post PowerShell: Unit Test Functions and Accessing the Invoking Code's Line Number. The PSUnit.ps1 script was composed of  a class, PSUnit, and it corresponding functions that support unit tests (AreEqual, IsTrue, etc.). The script also contained functions whose sole purpose was to test the unit test framework and test the PSUnit class. The PSUnit class and unit test functions were used to populate the module, PSUnit.psm1. The functions designed to test unit test framework are contained in PSUnitTest.ps1 which consumes the module PSUnit.psm1.

The PSUnit.psm1 module contains the class PSUnit which implements the following non-hidden (PowerShell's equivalent to public) methods

  • IsFalse: throws an exception if the value under test is not $false 
  • IsTrue: throws an exception if the value under test is not $true
  • AreEqual (comparing objects): throws an exception if the two values under validation is not equal
  • AreEqual (comparing double): throws an exception if the two values under validation is not equal based off of a specified difference tolerance (see PowerShell: Unit Tests Compare Doubles for Equality).
  • AreEqualCollection: throws an exception if the two collections under validation is not equal (see PowerShell: Interfaces). Each element in the collection is comparted individually.
The PSUnit overloaded PSUnit methods AreEqual was discussed in PowerShell: Constructor and Method Overloading. The PSUnit class is not exported from the PSUnit.ps1 module. A set of wrapper functions are exported from the PSUnit.psm1 module. These wrapper functions  pass the invoking script filename and invoking line number to methods of the PSUnit class (see PowerShell: Unit Test Functions and Accessing the Invoking Code's Line Number) as $MyInvocation.ScriptName and $MyInvocation.ScriptLineNumber do not return values when invoked by the methods of a class. The functions exported by the PSUnit.psm1 module are
  • IsFalse: wraps method PSUnit.IsFalse
  • IsTrue: wraps method PSUnit.IsTrue
  • AreEqual: wraps method PSUnit.AreEqual (object parameter type overload)
  • AreEqualDouble: wraps method PSUnit.AreEqual (double parameter type overload)
  • AreEqualCollection: wraps method PSUnit.AreEqualCollection

Exporting Functions

The PSUnit.psm1 module file uses the Export-ModuleMember cmdlet to export functions (see lines 242-247):



The Export-ModuleMember cmdlet is reviewed in Appendix A: Export-ModuleMember.

Importing Functions

The PSUnitTest.ps1 script file imports the PSUnit.psm1 module using the Import-Module cmdlet (see line 1):


The Import-Module cmdlet is reviewed in Appendix B: Import-Module.

Appendix A: Export-ModuleMember

Microsoft documentation for the cmdlet Export-ModuleMember can be found at Export-ModuleMember. The description given for Export-ModuleMember in the documentation is as follows:

Appendix B: Import-Module

Microsoft documentation for the cmdlet Import-Module can be found at Import-Module. The description given for Import-Module in the documentation is as follows:



No comments :

Post a Comment