Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
Precipitous Avatar asked Jun 02 '09 16:06

Precipitous


People also ask

Is TDD the same as unit testing?

Unit testing is the art of creating automated tests for your code, usually after you have written the code. Test-driven development (TDD) is a set of practices where you write your unit tests before writing your production code and continuously run those tests as you write new code.

Is Unit Testing TDD or BDD?

Unit testing is a type of automated testing. You can write unit testing without using TDD or BDD, just by writing the tests after the production code. TDD is a software development methodology, in which the developer writes code in very short cycles, always starting with a failing test.

Does TDD test single unit at time?

These are the rules of TDD. This means that you are only ever writing one unit test and one bit of code at a time and doing this iteratively. The point of TDD is not writing tests, it is using your tests to drive out the design.


1 Answers

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

https://github.com/pester/Pester

like image 159
Martin Suchanek Avatar answered Sep 28 '22 02:09

Martin Suchanek