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.
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
Figure 2: Test run in the ISE with debug enabled
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:
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"
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.
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:
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.