PowerShell TDD with PSUnit: some usage examples

I discussed setting up PSUnit for unit testing PowerShell before. This is a quick followup for my own record and consumption in the future. I will update this post as I find more interesting things to record.

1. Put PowerShell functions in one file such as myBaseFunctions.ps1;
2. Create a test directory and under that directory, create a test case file called myBaseFunctions.Test.ps1;
3. Here is what the first two lines of the test case file:

. PSUnit.ps1
. "c:\properPath\myBaseFunctions.ps1"

4. A sample test case:

function Test.getFileName()
{
#Arrange
    	$expectedResult = "X:\somePath\someName.sql"
	
#Act
	$actualResult = functionBeingTested -Para1 "someValue" -Para2 "someValue"

#Assert
	Assert-That -ActualValue $actualResult -Constraint {$actualResult -eq $expectedResult}
}

5. Running tests on command line:
PS C:\myDirectory> PSUnit.Run.ps1 -PSUnitTestFile properPath\myBaseFunctions.Test.ps1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.