Powershell Assertion Framework

# Based upon: http://www.leeholmes.com/blog/2005/09/05/unit-testing-in-powershell-%E2%80%93-a-link-parser/
# Updated to Powershell 2

Set-StrictMode -Version Latest

## A simple assert function.  Verifies that $condition
## is true.  If not, outputs the specified error message.
function assert
{
param
(
[Parameter(Mandatory=$True)]
[bool] $condition,
[string] $message = “Test failed.”
)

if(-not $condition)
{
write-host “FAIL. $message”
}
else
{
write-host -NoNewLine “.”
}
}

## A simple “assert equals” function.  Verifies that $expected
## is equal to $actual.  If not, outputs the specified error message.
function assertEquals
{
param
(
[Parameter(Mandatory=$True)]
[AllowNull()]
$expected,
[Parameter(Mandatory=$True)]
[AllowNull()]
$actual,
[string] $message = “Test failed.”
)

if(-not ($expected -eq $actual))
{
write-host “FAIL.  Expected: ‘$expected’.  Actual: ‘$actual’.  $message.”
}
else
{
write-host -NoNewLine “.”;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s