PSUnit PowerShell Unit Testing Framework – Getting Started Guide – Running Unit Tests – Version 2 Beta 1

by Klaus Graefensteiner 7. August 2009 09:46

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

Note: For information about how to install PSUnit read the following blog post: Installing PSUnit

Note: For information about how to write unit tests with PSUnit read the following blog post: Writing unit tests for PSUnit

Running PSUnit tests

Running the test script from the ISE

The easiest way to run PSUnit unit tests is to open a test file in the PowerShell ISE and select the Custom\Run Unit Tests command (PowerShell 2.0 CTP3) or the Add-On\Execute Unit Tests command (PowerShell 2.0 RTM). Make sure that the unit tests script is the currently selected script.

Running PSUnit unit tests from the PowerShell ISE

Figure 1: Running the current PSUnit unit test script via PowerShell ISE custom menu command

The default implementation of the custom menu command is going to execute the current unit test script. At the end it automatically opens the test result report in the default browser. The progress of the test run will be displayed in the output window of the ISE. Depending on the debug settings of your PowerShell runtime the output can contain just a table of the test result messages or a detailed list of debug statements and the test result statements.

Here is the command that the ISE menu is hooked up to:

   1: PSUnit.Run.ps1 -PSUnitTestFile $($psISE.CurrentFile.FullPath) -ShowReportInBrowser

Note: Debug or production mode can be enabled by either calling the functions Set-DebugMode or Set-ProductionMode

image

Figure 2: Test run in the ISE with debug enabled

image

Figure 3: Test run in the ISE with debug disabled

Running the test script form PowerShell.exe

The same statement can also be executed from the PowerShell CMD shell console. Open PowerShell.exe and enter the following command:

   1:  PSUnit.Run.ps1 -PSUnitTestFile (Join-Path -Path $env:PSUNIT_HOME -ChildPath "Samples\Interpolate-Records.Test.ps1") -ShowReportInBrowser

The following screenshot shows the output in the case where debug is not enabled:

image

Figure 3: Test run in the PowerShell.exe CMD shell

PSUnit-Run.ps1 parameters

The script that executes the test is called PSUnit-Run.ps1. It has three parameters that determine its behavior:

PSUnitTestFile

This parameter is required and specifies the name of script file that contains the PSUnit unit tests. Here is an example:

   1:  PSUnit.Run.ps1 -PSUnitTestFile (Join-Path -Path $env:PSUNIT_HOME -ChildPath "Samples\Interpolate-Records.Test.ps1")

Category

The Category parameter specifies the category of tests to filter out and execute. By default all tests are executed and therefore the name of the default Category is “All”. In the example here only tests that have the Category_ParameterValidation parameter will be executed. Click here for more details.

   1:  PSUnit.Run.ps1 -PSUnitTestFile (Join-Path -Path $env:PSUNIT_HOME -ChildPath "Samples\Interpolate-Records.Test.ps1") -Category "ParameterValidation"

image

Figure 4: Running only a subset of tests by specifying the Category parameter

ShowReportInBrowser

The ShowReportInBrowser switch parameter will, if set, automatically open the test result report in the default browser. Here is an example command:

   1:  PSUnit.Run.ps1 -PSUnitTestFile (Join-Path -Path $env:PSUNIT_HOME -ChildPath "Samples\Interpolate-Records.Test.ps1") -ShowReportInBrowser

The test results report

By default each test run will generate a detailed test result report in the PSUNIT_HOME folder.

image

Figure 5: Test result reports

Test report name

The name of the test result reports contains the following information:

  • Name of the script file that contains the unit test functions
  • The date and time of the start of the test run
  • The test result (T=Total number of tests, P=Passed, F=Failed, S=Skipped)

Test report content

A test report contains information about the test environment, the actual test result and detailed error information:

TestResultReport.

Figure 6: At last 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 the structure of the framework.

Tags: ,

PowerShell | PSUnit | Test Automation

Comments

8/16/2009 11:36:39 AM #

pingback

Pingback from powerscripting.wordpress.com

Episode 80 – Klaus Graefensteiner «  PowerScripting Podcast

powerscripting.wordpress.com |

9/3/2009 12:11:49 AM #

Klaus

Test

Klaus United States |

9/3/2009 12:14:11 AM #

Klaus

test1

Klaus United States |

9/3/2009 12:16:19 AM #

Klaus

Cool nested comments work.

Klaus United States |

9/3/2009 1:04:35 AM #

pingback

Pingback from tech.7sites.co.cc

PSUnit PowerShell Unit Testing Framework – Getting Started Guide – Running Unit Tests – Version 2 Beta 1 - Technology - TechBlog

tech.7sites.co.cc |

9/13/2009 11:37:10 AM #

pingback

Pingback from powerscripting.wordpress.com

Episode 83 – with Pragma Systems «  PowerScripting Podcast

powerscripting.wordpress.com |

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 2014, 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 2014 by Klaus Graefensteiner

Rendertime:
Page rendered at 7/30/2014 8:08:43 AM (PST Pacific Standard Time UTC DST -7)