How to do TDD and unit testing in powershell?

With MS ramming powershell into all new server products, I'm starting to (reluctantly) think I need to take it seriously. Part of "taking it seriously" is TDD. Have you found good methods to unit test power shell scripts?

I've found samples of mocking from Mr Geek Noise - but I'd really like something like RhinoMocks. Brian Hartsock has a sample of running tests on powershell strings from MS Test. A little hacky, but it seems to work.

What I want is a Powershell TDD experience that is as clean as it is in "real" languages.


Update to clarify:

The first two answers attempt to steer me away from testing Powershell. The opinions are interesting. I don't want to know if it's a good idea to test in powershell. That's a subjective question that should be asked in a different forum. I want a solution to unit testing powershell. If you think it's a bad idea (it might be), treat it as a fun academic question.

  • Yes, scripting languages glue together disparate systems. However, as already pointed out, it's also easy to mock and break seams in a dynamic language.
  • I'm not asking about "debugging". Debugging is an extremely useful topic. I'll let someone else ask it.
  • Maybe PS scripts should be simple. The language supports modularity and it is inevitable that complex processes will be implemented in PS (even if a bad idea).
  • The answer to this question is not "You can't". I can see (from linked blogs - which are a little old) that some people have made headway on the problem.

To re-state: How do you implement an automated testing of Powershell logic in the style of xUnit? Integration tests are interesting, unit tests that break dependencies most interesting.


Scott Muc has started a project for a lightweight BDD framework for PowerShell called Pester:

https://github.com/pester/Pester


PsUnit is now updated with a framework. I had the same problem as you a few months ago and I felt PsUnit was to big and complex for the number of scripts I had to write so I wrote my own unit test framework for PS. PS have the same kind of charectaristics as other script languages likepython for example, i.e. you can override functions anywhere at any time even with scope in the test methods which is great for faking (a.k.a. mocking). That is, if you have a function or object you want to test that depends on other functions you can declare them in your test method to create a local fake implementation.

So regardles of which test framework you choose to use I'd say PS is very easy to TDD. That was my experience at least.


I think you're asking about "testing strategies" instead of TDD specifically, so I'll answer both questions.

The bulk of your work in PowerShell will be integrating a bunch of disparate systems via cmdlets and object pipes. If you want to be confident your PowerShell scripts work, put as much effort as possible into building a perfect staging environment, so as to test all these systems as accurately as possible.

Running your scripts in a perfect staging environment will be infinitely more valuable than "fleshing out your design" via TDD or "testing your code's intent" with after-the-fact unit tests.

Small notes that may help:

  • The -whatif switch exists on built-in cmdlets. Also I just found out you can do this as well: -whatif:$someBool - you'll know when you need it.
  • The ISE coming in V2 has a debugger. Sweet.
  • You can always code up a custom cmdlet in C# and do whatever you want there.

I am writing some basic PowerShell scripts with TDD like the following model:

First, the spec in SUT_spec.ps1:

Import-Module -Name .\my_SUT.ps1

$case1_input=@{}
$case1_output=@{}
f1 $case1_input $case1_output

$case1_result = $case1_output["Output"] -eq "expected"
"f1 case1: $case1_result"

# repeat for all test cases

Remove-Module -Name my_SUT

Second, my unit (SUT) as a function in my_SUT.ps1 file:

function f1($the_input, $the_output)
{
    #take input from $the_input hashtable, process it and put output into $the_output hashtable.

    $the_output["Output"]="results"
}

Third, the releasable main entry point as a separated file like SUT_spec.ps1 but with inputs from actual external sources.