PSUnit is a lightweight Unit Testing framework for PowerShell script developers. It is completely written in PowerShell.
Follow me on Twitter for the latest updates on PSUnit:
Source code is actively maintained on github
Codeplex is still the official PSUnit site!
IMPORTANT UPDATE: To cover PowerShell V3 in depth the new Release target date is now set to: 6/30/2013
Here is a list of improvements that are planned for the next release:
Recent Blog Posts
- NEW Support of PowerShell V3
- PSUnit Module
- PSUnit MSI install
- Test run is going to output a collection of test result objects instead of a html file
- NEW Add a XML Test Results output format that is compatible with the one NUnit uses
- Bug fixes
- NEW Add TeamCity Service Messages to the output during the test execution for TeamCity integration.
- NEW RealTime test progress and test result monitoring
- Taking advantage of PowerShell 2.0 features
- Adding message parameter to Assert-That cmdlet
- Reporting message, actual value and expected value in the case of a failing test
- Support for $Null as $Actual in Assert-That
Unit Tests Example:
Writing Unit Tests:
Running Unit Tests:
Helpful Wiki Content
PSUnit 2.0 Beta has been released on August 6th 2009.
Here is a screen shot of a PSUnit test run in the PowerShell ISE (CTP 3)
Here is a screen shot of a HTML PSUnit test report that gets automatically generated after the test run.
Here are some details about PSUnit:
Definition of a PowerShell unit test:
- PSUnit is convention based unit testing. It is taking advantage of the flexibility of PowerShell and the ideas behind unit testing frameworks like NUnit.
- A NUnit test class maps to a test script in PowerShell.
- A NUnit test method maps to a test function in PowerShell.
- Test function implementations typically follow the Arrange, Act, Assert sequence.
- There are two ways to organize and execute tests in PSUnit:
- Discovery of named test functions and generating PSUnit functions based on function declarations and attributes of advanced PowerShell functions.
- List of script block declarations.
- An asserts can be any script block that returns true or false.
- A test function can be parametrized. Parameter sets are organized in specification files.
A PSUnit unit test is a script that calls the script function being tested and checks some assumptions about the logical behavior of that function. It can be written easily and runs fast. Unit tests written with PSUnit will run automatically and are trustworthy,
readable and maintainable. PSUnit unit tests can be executed repeatedly by anyone who has the PowerShell runtime installed.
Test function naming convention
- Test creation and organization
- Provide script templates to quickly create test scripts
- Provide assert functions to verify the result of the functions being tested
- Provide a way to decorate functions as test functions
- Test execution
- Provide a script that executes all or a single test script
- The test runner needs to identify the test functions that decorated as such
- The test runner runs tests automatically
- The test runner indicates the progress while running
- The test runner should provide a GUI
- Test result analysis
- Provide the total number of tests that ran
- Provide the number of tests that didn't run
- Provide the number of tests that failed
- Provide a list of tests that failed
- Provide the reason why a test failed
- Provide the assert message that has been defined in the test
- Provide the code location where the test failed
- Provide the full stack trace of any exceptions caused by the test
The name of a test function should adhere to the following naming convention:
[Function Name]__[Should ... ExpectedBehavior]_[If ... StateUnderTest]
For example: Get-Enumerator_ShouldReturnIEnumerable_IfHashTableIsPassedIn