Skip to content
knutkj edited this page Oct 3, 2012 · 16 revisions

PsTest is a PowerShell unit testing framework. PsTest is a binary PowerShell module written in C# .NET, developed with Visual Studio Express 2012 for Windows Desktop and tested with the Visual Studio Unit Testing Framework.

PsTest uses a syntax very similar to the QUnit JavaScript unit testing framework, which is used to test jQuery. JavaScript is a dynamic scripting language just like PowerShell. If you have a C# and NUnit background you may need a little time to get used to PsTest.

PsTest is currently optimised for PowerShell ISE "F5" development. That means that you edit your production and test script in PowerShell ISE, and just hit F5 in your test script to run all tests. This is also very similar to QUnit testing where you just hit F5 and refresh the browser for each test run. Cmdlet Format-TestResult, which does test result color formatting, only works in PowerShell ISE at the moment.

Only the most basic stuff is implemented at the moment. Please use the issue tracker for feature requests. Use the enhancement label.

On this page:

  1. Installation
  2. Sample test scripts
  3. PsTest unit testing with PowerShell ISE screen shot
  4. Assertions

Installation

PsTest can be installed in three ways. You can install one of the installers (MSI x64/x86), download and unzip the zipped version of the module or install the NuGet package. Downloads:

  1. Installers and zip: https://github.com/knutkj/pstest/downloads
  2. NuGet package: http://nuget.org/packages/PsTest

For more information about the NuGet package visit the PsTest NuGet package page.

Sample test scripts

Here is a simple sample test script:

# Import the PsTest unit testing framework.
Import-Module -Name PsTest

# Include the script to test.
.\script-to-test.ps1

# A test with expected exception.
(New-Test 'Expected exception' ([ArgumentNullException]) {

    # Arrange.
    $exception = New-Object -TypeName ArgumentNullException

    # Act.
    throw $exception
    
    # Assert.

}),

# A test with an NUnit assert that fails.
(New-Test 'NUnit assert test' {

    # Arrange.
    $expected = 1
    
    # Act.
    $actual = 2
    
    # Assert.
    # IMPORTANT: READ THE SECTION ABOUT ASSERTS BELOW.
    $Assert::That($actual, $Is::EqualTo($expected))

}) |

# Invoke the tests.
Invoke-Test |

# Format all test results.
Format-TestResult -All

If you want to see real world PsTest test scripts you may have a look at the script that tests the SocketToolkit PowerShell module and the PsTest NuGet package init script:

PsTest unit testing with PowerShell ISE screen shot

PsTest unit testing with PowerShell ISE

Assertions

PsTest does not provide any assertions. You may use the assertions provided by the NUnit testing framework. Here is a detailed instruction on how to do NUnit assertions with PowerShell:

http://knutkj.wordpress.com/2012/09/24/how-to-do-nunit-assertions-with-powershell/