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:
[code language=”text”]
. PSUnit.ps1
. “c:\properPath\myBaseFunctions.ps1″
[/code]
4. A sample test case:
[code language=”text”]
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}
}
[/code]
5. Running tests on command line:
PS C:\myDirectory> PSUnit.Run.ps1 -PSUnitTestFile properPath\myBaseFunctions.Test.ps1