First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
« PowerShell, AnkhSVN and Subversion | Main | Capturing Video a Web Camera using WIA -... »

We've been doing lots of PowerShell at work, but we're also a continuous integration shop and we try to do TDD so testing, specifically NUnit, is very important to us.

Here's how Jason Scheuerman from my team tests PowerShell scripts with NUnit.

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;
using System.Management.Automation;
using NUnit.Framework;
using System.Security;

namespace PSUnitTestLibrary.Test
{
    [TestFixture]
    public class Program
    {
        private Runspace myRunSpace;
 
        [TestFixtureSetUp]
        public void PSSetup()
        {
            myRunSpace = RunspaceFactory.CreateRunspace();
            myRunSpace.Open();
            Pipeline cmd = myRunSpace.CreatePipeline(@"set-Location 'C:\dev\someproject");
            cmd.Invoke();
         } 

        [Test]
        public void PSTest()
        {
            Pipeline cmd = myRunSpace.CreatePipeline("get-location");
            Collection<PSObject> resultObject = cmd.Invoke();
            string currDir = resultObject[0].ToString();
            Assert.IsTrue(currDir == @"'C:\dev\someproject");

            cmd = myRunSpace.CreatePipeline(@".\new-securestring.ps1 password");
            resultObject = cmd.Invoke();
            SecureString ss = (SecureString)resultObject[0].ImmediateBaseObject;
            Assert.IsTrue(ss.Length == 8);

            myRunSpace.SessionStateProxy.SetVariable("ss", ss);
            cmd = myRunSpace.CreatePipeline(@".\getfrom-securestring.ps1 $ss");
            resultObject = cmd.Invoke();
            string clearText = (string)resultObject[0].ImmediateBaseObject;
            Assert.IsTrue(clearText == "password");
        }
    }
} 

The "CreatePipeline" calls are us telling PowerShell "do that!" The Pipeline of commands is created, passed into cmd then Invoke'd.

Also in this case we're assuming one PowerShell RunSpace per TestFixture and we're using the TestFixtureSetUp method to get that RunSpace going, but you could certainly move things around if you wanted different behavior or isolation.

Tracked by:
"interesting things to do in windows powershell" (Tiernans Comms Closet) [Trackback]
"Power to the PowerShell" (Impersonation Failure) [Trackback]
"Articles + Posts to Check Out" (Noah Coad's Code) [Trackback]


Thursday, June 29, 2006 11:25:53 PM (Pacific Standard Time, UTC-08:00)
Scott,

I'm interested to know the kinds of things you are ending up doing with Monad.

I'm *trying* to use them mainly for adminy things. Stop service, start service, create virtual directories, the like.

If you are writing NUnit tests, I'm assuming you are using Monad as a more integral part of your solution.

Sachin
Sachin Rao
Friday, June 30, 2006 2:24:27 PM (Pacific Standard Time, UTC-08:00)
Wow, that looks just sooooo cool. I'm a fan of JPSofts' 4NT, looks like PowerShell has all the more and I'm looking forward to trying it out. Being able to run them as part of NUnit tests is just awesome.

Hey, and I just started listening to Hanselminutes with my new iPod Shuffle, very cool! I like the way you talk for a little bit, but not too long on topics. Lots of good info short and concise. Thanks Scott!
Wednesday, July 05, 2006 5:05:10 AM (Pacific Standard Time, UTC-08:00)
[Experimenting with a folksonomy]
PSMDTAG:FAQ: How do I test Scripts?
PSMDTAG:FAQ: Can I use NUNIT to test scripts?
PSMDTAG:SDK: RunspaceFactory.CreateRunspace(), CreatePipeline(), SessionStateProxy.SetVariable()

Jeffrey Snover [MSFT]
Windows PowerShell Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Jeffrey Snover
Wednesday, July 05, 2006 7:29:56 PM (Pacific Standard Time, UTC-08:00)

typo alert!

myRunSpace.CreatePipeline(@"set-Location 'C:\dev\someproject");

has a missing single quote down near the end...

should be:

myRunSpace.CreatePipeline(@"set-Location 'C:\dev\someproject'");
Comments are closed.

Contact

Sponsors

On this page...

Tags

Calendar

<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Archives

Google Ads