views:

553

answers:

4

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. I you think it's a bad idea (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.

+2  A: 

From your question I think you are headed for disappointment. Powershell is just a little command line language. Sure, it can do anything that C# can do, and even more, but then so can assembly language. Of course it's also OO and hooked in to the .NET libraries, but so is C#, which is a much much cleaner language.

If a solution is longer than a one liner, or you think you need to TDD it, then you don't want to be using Powershell. It's a cryptic language that is full of surprises, to be avoided for anything complicated.

If you want to do some ad hoc search and replace or formatting of text, or look around in your file system, then Powershell is your friend. What you really want to do is to use it a little every day, and repeat yourself often, in order to stay familiar with the syntax. For this reason, also avoid open source Powershell libraries, and forget about writing your own CmdLets, unless you have a very specialized case for ad hoc command line usage. The pipe binding rules are arcane and ugly.

This is all just my opinion of course, but I am a long time Powershell user and am much happier with it now that I look at it like this.

Mike
I assume that PowerShell scripts will be written because MS is pushing it hard. It will quickly become useful. Because PS scripts will be written and they will be programs (however humble), I want them to be robust. So, I want to test them. You might disagree but then, let's call this an academic exercise: how would you unit test a powershell script? The practical conclusion may be that it is too hard ( I think not impossible ) and I should use IronPython for scripting instead if I insist on quality. :)
Precipitous
No one doubts that TDD in Powershell is possible. It is definitely possible, after all, Powershell can do everything C# can do, and then some. The functional nature of Powershell in theory can make it an even better testing language than C#. The problem is just that the syntax and general design of Powershell is...well, ugly, and suprising. Let's talk again after you've had a year of experience with it...
Mike
I've been using PS for at least 3 years for day to day work (mostly for robust file capabilities) - we can talk now :) I agree it's ugly. However, it hooks into a LOT of functionality. The surprises are WHY I want to test it.
Precipitous
BTW, whenever I ask a Powershell question, I also tend to get answers telling me to not use Powershell, and rarely get an actual answer, so I realize that it is annoying. So sorry about that! And there are cases where Powershell is your only option, like perhaps at customer sites.
Mike
+2  A: 

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.
Peter Seale
Hmm ... you also doubt its possible. Note that the Geek Noise blog demonstrates a solution to mocking (the disparate systems).
Precipitous
It's possible I assume, but who cares? What value is it to know that you typed "\\server\fileshare" in both your test and your script? You'll either overspecify interactions with external cmdlets, or you'll only test interactions within your own system (which will end up being a VERY small portion of your script.) I suppose that has value, but again, you'll reap a far greater benefit by doing real testing in a staging environment (and in prod itself if possible.)
Peter Seale
Neither system nor unit testing gives "far greater benefit" - both needed. If I'm testing "interactions with external cmdlets" it is not a unit test. Here's a trivial example of common logic that benefits from unit testing / mocking: We have a post-deployment script that identifies interesting errors in a windows event log - excluding spam. Posh excels here - no need for cmdlet. The filter criteria is complex and delicate. Extract the where-object criteria to function and test various inputs. Easy to come up with other examples.
Precipitous
+1  A: 

Look at PsUnit

stej
Except for a noble mission statement and an amusing "Darth-Enumerator.ps1", PSUnit is empty. Will see if I can contact developer for something else ..
Precipitous
I think the hopes are high :) tdd is needed for posh..
stej
+5  A: 

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.

Cellfish