PSUnit PowerShell Unit Testing Framework – Getting Started Guide – Installation - Version 2 Beta 1

by Klaus Graefensteiner 4. August 2009 09:27

Introduction

I am glad to announce that the first official release of PSUnit is now available for download on Codeplex http://www.psunit.org/Release/ProjectReleases.aspx. This release is a fully functional unit testing framework that lets you write and execute unit tests in PowerShell. It also provides a nice test result reporting feature. The Beta release is missing some convenience features, like an automatic deployment of an PowerShell module. Instead there are a few manual steps required to deploy the framework successfully. To increase the usability in the near future I am also extending the PSUnit integration with the PowerShell ISE.

Note: This release of PSUnit requires PowerShell 2.0 CTP3 or later! Click here to download this version of PowerShell

Installing PSUnit

Step 1: Download the zip file

Download the latest release of the PSUnit Unit Testing framework form the following web site: http://www.psunit.org/Release/ProjectReleases.aspx 

Step 2: Extract the files into a folder

Pick a folder and extract the script files into it. In my example I choose the My Documents folder on my Windows Vista computer:

C:\Users\Klaus\Documents\PSUnit

image

Figure 1: Extracted PSUnit files in the Windows Explorer on Windows Vista

Step 3: Modify the User PowerShell profile files

Case 1: You are not using profile files

If you currently don’t use profile files than this step is fairly easy. Just create a folder under your User’s documents and name the folder “WindowsPowerShell”. Copy the three profile files that are located in you PSUnit\Profiles folder:

  • profile.ps1
  • Microsoft.PowerShell_profile.ps1
  • Microsoft.PowerShellISE_profile.ps1

into your user’s PowerShell profiles folder. In my case this folder would be called: C:\Users\Klaus\Documents\WindowsPowerShell

image

Figure 2: Your user’s PowerShell profile files on Windows Vista

Case 2: You are using profile files

In this case you need to manually merge the following code snippets with your existing user profile files.

profile.ps1

Merge the following PowerShell code with your current profile by copy and pasting it at the end of your profile.ps1. The code snippet is included in the profile.ps1 file that is in the Profiles folder under the PSUnit directory.

Note: Change the value of hte $PSUnitPath variable to match the path where you copied the PSUnit files on your machine. In my case it is C:\Users\Klaus\Documents\PSUnit.

   1: #PSUnit: Setting up PATH environment variable to point to PSUnit framework
   2: $PSUnitPath = "C:\Users\Klaus\Documents\PSUnit" #Modify this path to match your local PowerShell installation path
   3: #Make sure to only append this path once
   4: if (!($env:path -like "*$PSUnitPath*"))
   5: {
   6:     $env:path = $env:path + ";$PSUnitPath"
   7: }
   8: #PSUnit: Setting PSUNIT_HOME environment variable to point to PSUnit framework
   9: If(! $(Test-Path -Path "env:PSUNIT_HOME"))
  10: {
  11:     New-Item -Path "env:" -Name "PSUNIT_HOME" -value $PSUnitPath
  12: }
  13:  
  14: #PSUnit: Defining functions to set debug options
  15: function Set-DebugMode()
  16: {
  17:     $Global:DebugPreference = "Continue"
  18:     set-strictmode -version Latest
  19: }
  20:  
  21: function Set-ProductionMode()
  22: {
  23:     $Global:DebugPreference = "SilentlyContinue"
  24:     set-strictmode -Off
  25: }
Microsoft.PowerShell_profile.ps1

Merge the following PowerShell code with your current profile by copy and pasting it at the end of your Microsoft.PowerShell_profile.ps1. The code snippet is included in the Microsoft.PowerShell_profile.ps1 file that is in the Profiles folder under the PSUnit directory.

Note: This step is optional and the settings here will be configured elsewhere in a future release. It provides a better user experience while watching the progress in the PowerShell.exe console during the execution of PSUnit unit tests.

   1: # PSUnit: Setting the console output window size and color
   2: $a = (Get-Host).UI.RawUI
   3: $b = $a.BufferSize
   4: $b.Width = 300
   5: $b.Height = 600
   6: $a.BufferSize = $b
   7:  
   8: $b = $a.WindowSize
   9: $c = $a.MaxWindowSize
  10: $b.Width = $c.Width
  11: $b.Height = $c.Height
  12: $a.WindowSize = $b
  13:  
  14: $a.BackgroundColor = "Black"
  15: $a.ForegroundColor = "DarkGreen"
Microsoft.PowerShellISE_profile.ps1

Merge the following PowerShell code with your current profile by copy and pasting it at the end of your Microsoft.PowerShellISE_profile.ps1. The code snippet is included in the Microsoft.PowerShellISE_profile.ps1 file that is in the Profiles folder under the PSUnit directory.

Note: If you are using PowerShell 2.0 CTP3 then you need to comment out line 3 and un-comment line 2 to get the correct setup of the PowerShell ISE integration.

   1: #PSUnit: Loading the custom menu for PSUnit
   2: #. PSUnit.ISE.CTP3.ps1 #If you are using PowerShell 2.0 CTP3
   3: . PSUnit.ISE.ps1     #If you are using PowerShell 2.0 RC1

Step 4: Associate ps1 file extension with PowerShell ISE

This is also an optional step, but  provides a better user experience. Basically with this step you are going to change the default editor for ps1 files from notepad.exe to the PowerShell ISE. One of the results is that double-clicking a ps1 file in the Windows Explorer will open it in the PowerShell ISE instead of notepad.

Here is a detailed description of the procedure:

Select any ps1 file in the Windows Explorer, right click and open the properties window.

image

Figure 3: Default editor for ps1 files is notepad.exe

Browse to C:\Windows\System32\WindowsPowerShell\v1.0 and select the powershell_ise.exe

image

Figure 4: Select the powershell_ise.exe

Click the open button and confirm your selection.

image

Figure 5: Confirm your selection of the PowerShell ISE as the default editor for ps1 files

image

Figure 6: PS1 files are now associated with the PowerShell ISE

Step 5: Verifying that the installation was successful

To make sure that everything is setup correctly, open the Darth-Enumerator.Test.ps1 file that is located in your PSUnit\Samples folder with the PowerShell ISE. In my case it is in C:\Users\Klaus\Documents\PSUnit\Samples.

If you are using PowerShell 2.0 CTP3, then select the menu command Custom\Run Unit Tests.

If you are using PowerShell 2.0 RC1 or later, then Add Ons\Execute Unit Tests.

This will run the current PowerShell script as unit test.

PowerShellCustomMenuOnVista

Figure 7: Running unit tests from the PowerShel ISE Custom Menu

After the test run is complete verify

  • that there are no red error messages in the console output.
  • that all 43 unit tests pass and are listed in the console output of the PowerShell ISE.
  • that your default browser opens after the test execution to display the test result report.

image

Figure 8: Verify that all 43 tests succeed

image

Figure 9: Verify that your default browser opens and displays the test result report

Questions and comments

Any feedback is welcome. Either leave a comment here on my blog or add a discussion item on the PSUnit Codeplex site: http://www.psunit.org/Thread/List.aspx. In upcoming posts I am going to provide details about how to write unit tests for PSUnit, how to execute them and give some insights about the structure of the framework.

Tags: , ,

PSUnit | PowerShell | Test Automation

Comments are closed

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2012, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2012 by Klaus Graefensteiner

Rendertime:
Page rendered at 2/4/2012 5:08:32 AM (PST Pacific Standard Time UTC DST -7)