Welcome to MSDN Blogs Sign in | Join | Help

Why Should I Test With PowerShell?

Today, on our internal discussion list, someone asked if there were any advantages to testing with PowerShell versus testing with C#.

I was able to come up with 10 quick reasons to test with PowerShell:

  1. Being able to run command line programs easier within PowerShell
  2. Dynamically generating code or test data for a test case (fuzzing or data-driven testing get a lot easier with this)
  3. Being able to access COM easier within PowerShell
  4. Being able to embed PowerShell in C# (so you can avoid writing a framework and just embed PowerShell in your infrastructure)
  5. Being able to use weakly typed variables within PowerShell
  6. Being able to test APIs on the command line, so you test manually and then automate
  7. Getting a history of commands, to take what you've explored and turn it into a test case
  8. Being able to strongly cast or coerce types when needed
  9. Support for verbose, warning, and debug streams to provide additional test information
  10. Being able to use PowerShell's systems administration features to help  setup or cleanup a test environment

I hope these reasons help convince other software testers to use PowerShell. It can really make testing a lot simpler.

Hope this helps,

James Brundage [MSFT]

Published Monday, January 19, 2009 9:40 PM by PowerShellTeam
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Why Should I Test With PowerShell?

Can you please tell me the name of for internal discussion group on which this was discussed? I wish to join it.

Thanks,

Jigar

Monday, January 19, 2009 7:24 PM by Jigar Mehta

# re: Why Should I Test With PowerShell?

FWIW I wrote our software products regression test suite in PowerShell for many of the same reasons you list above.  Primary among those being the ease of invoking command line operations and monitoring stdout/stderr.  We also need to do a fair amount of test setup work involving munging files with regexes.  Powershell makes this a snap.

Tuesday, January 20, 2009 1:00 AM by Keith Hill

# re: Why Should I Test With PowerShell?

Could you please have some examples on each of these points, so that it would be helpful to all developers.

Tuesday, January 20, 2009 1:37 AM by Renil

# re: Why Should I Test With PowerShell?

I wrote this comment on the suggestions thread but I'll copy it here too. This is a real difficulty with testing in PowerShell:

Creating a new object from an assembly in a certain folder (C:\Users\Eric\someassm.dll) fails if there are dependent assemblies in that same folder (C:\Users\Eric\dependassm.dll) UNLESS you specifically include those assemblies too. I know this is due to Code Access Security in .NET but it really takes away a potential use of PS: quickly testing objects you just created in Visual Studio. Right now I have to rebuild simple command line programs in order to manually test a rebuilt assembly. It'd be much easier to just open a new PS window, load the main assembly and work with the new object there.

Tuesday, January 20, 2009 9:34 AM by Eric

# re: Why Should I Test With PowerShell?

More information here on this subject would really be useful.  Currently, many groups use C# for test automation. I think one important reason for using Powershell to test with is the "virtuous cycle". A scripting language links administrators with test engineers. Often these two groups share a greater subset of skills with each other than developers. Writing test suites in C# often creates a full time job for a developer in test.  Creating the test suite in Powershell means that every Windows Server 2008 and Windows 7 administrator can eventually learn test engineering skills and Windows APIs without dealing with the C family of languages. Additionally, this adds API troubleshooting to "field workers" (e.g. network administrators, IT personal) who are going to be less likely to learn Windbg and C++ than you might imagine.

Tuesday, January 20, 2009 1:07 PM by rferrisx

# re: Why Should I Test With PowerShell?

Real life applications have long namespace and class names. They pollute PowerShell code too much. Interactive typing is hard, in fact one has to type a lot if a method being tested on the fly accepts objects that have to be created by new-object with long type name + their parameters as well. Honestly, PowerShell *can* be used for interactive testing, but in reality it is not yet well designed for this.

Thursday, January 22, 2009 3:42 AM by Roman Kuzmin

# re: Why Should I Test With PowerShell?

You can shorten class names with a number of tricks in PowerShell.

#1. You can always omit SYSTEM namespace when specifying a type

#2. You can use -as [type] on a string to get a type, e.g.

$sma = "Management.Automation"

"$sma.PSObject" -as [type]

#3. The same sort of trick applies to New-Object

#4.  You can take a method on an existing object and see its overloads by omitting parenthesis, e.g.

"a".Replace

#5.  You can get method information on constructors in a readable form with PowerShell, e.g.

[string].GetConstructors() | % { "$_" }

Hope this Helps,

James Brundage [MSFT]

Friday, January 23, 2009 9:25 PM by PowerShellTeam

# re: Why Should I Test With PowerShell?

These are very nice tricks, for testing or not, thank you. But this is getting too smart, not natural and obfuscating, too.

For testing or not, it would be useful to have some native PowerShell accelerators like "using" directives or user defined namespace and type aliases, etc. Due to lack of this now "glide path" from C# to PowerShell is not that glide. C# code normally is a bunch of "using" directives and then types are used just by names. Often there are no full type names in C#, so that there is nothing to "just copy\paste". The funny thing is that sometimes for a type in C# code you do not even know (or remember) what namespace it belongs to. If you have just C# source being "converted" into PowerShell and no .NET development tools at hand (e.g. just a handy MSDN example and Notepad or ISE) then this task may be tough even for some programmers.

Saturday, January 24, 2009 3:41 AM by Roman Kuzmin

# re: Why Should I Test With PowerShell?

I'm not sure I would want to use PowerShell for low-level unit testing of managed code.  I'd rather use NUnit or mstest.  However for functional & regression testing of our applicaiton which involves firing off the app EXE with various input files, collecting and evaluating the outputs (both files and stderr), I think PowerShell is a great way to go.

Monday, February 02, 2009 3:25 PM by KeithH

# re: Why Should I Test With PowerShell?

>>"embed PowerShell in C#"

Does anybody point me to some sample for this.

If I understand right, I can develop custom cmdlets that implement some setting of my framwork. Then I use such cmdlets to pipline them the way my application logic requires it, and the users can use the same thing to script on their own.

This way I kill the 2 flies in one shot - I develop applicaton logic and make application automation-ready automatically.

Is all that true ?

Wednesday, February 04, 2009 6:18 PM by majkinetor

# re: Why Should I Test With PowerShell?

majkinetor - this might overlap with what you're after (make application automation-ready automatically): PowerShellTunnel (http://code.msdn.microsoft.com/PowerShellTunnel).  For general embedding of PowerShell in C# I think look for the details of PowerShell's Runspace.

Another reason why testing with powerShell is great:

It's all just Text!  Given some helper functions then you have easy ad-hoc testing.  It's all text so just copy-paste into your 'reproduction steps' in your change tracking system...

Thursday, February 05, 2009 4:48 AM by Mat

# re: Why Should I Test With PowerShell?

Thanks Mat.

Tunnel looks very intersting.

ZOMG! I just started with PS before few days ago and I already have tones of things already here from Web applications to GUI apps over tunnels and ....

I never thought I will say congratz to MS.

And, yes, nothing compares to pure text (especialy no compiling).

Thursday, February 05, 2009 4:01 PM by majkinetor

# re: Why Should I Test With PowerShell?

Re: PowerShell "Using" statement ... in v2 you can just add type accelerators:

http://poshcode.org/762

Friday, February 13, 2009 11:26 PM by Joel "Jaykul" Bennett

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker