Archive for October 25th, 2009

Integration Test Brought to you by Powershell & NUnit – with a Little Specification Syntax for Flavoring

One of the tools I’ve used the last half of a year and really enjoyed is the C# specification extension methods when writing unit test assertions. If you’re looking for a little more background on the topic, I wrote about Fluent Specification Extensions in a past blog.

Recently I wanted to execute a PowerShell script to do some automated functional testing. I wanted to execute an application and apply some assertions on the output of the software(basically running a console app, parse the xml output and assert on values in the output).

FYI: I’m very new to PowerShell, so any suggestions on how I implemented the below are welcome…

I’ve seen a couple examples of writing test assertions in PowerShell out there. One example is PSUnit; however, this seemed a little heavy for my needs and not quite the syntactic sugar I was looking for.

Besides the syntax flavor I was desiring, another thing I wanted to do was leverage the power of NUnit.Framework’s assertion capabilities. I like the error messages generated when strings and other objects fail the assertion.

Examples of end result ShouldLookLike()…

$true.ShouldBeTrue()
$false.ShouldBeFalse()
“a”.ShouldEqual(”a”)
“a”.ShouldNotEqual(”b”)

Step 1: Figure out how to write a C# style extension method in PowerShell.

I found a great blog post describing how to extend any PowerShell object to add extension methods.

Extension Methods in Windows PowerShell

In short, to extend types in PowerShell leveraging the Extended Type System, you need to define them in an xml file and import the method definitions into the PowerShell runtime instance.

Below is PowerShell XML definition for my NUnit Specification Extensions.

<?xml version="1.0" encoding="utf-16"?>
<Types>
    <Type>
        <Name>System.Object</Name>
        <Members>
            <ScriptMethod>
                <Name>ShouldBeFalse</Name>
                <Script>
                    [NUnit.Framework.Assert]::IsFalse($this)
                </Script>
            </ScriptMethod>
            <ScriptMethod>
                <Name>ShouldBeTrue</Name>
                <Script>
                    [NUnit.Framework.Assert]::IsTrue($this)
                </Script>
            </ScriptMethod>
            <ScriptMethod>
                <Name>ShouldEqual</Name>
                <Script>
                    [NUnit.Framework.Assert]::AreEqual($args[0], $this)
                </Script>
            </ScriptMethod>
            <ScriptMethod>
                <Name>ShouldNotEqual</Name>
                <Script>
                    [NUnit.Framework.Assert]::AreNotEqual($args[0], $this)
                </Script>
            </ScriptMethod>
        </Members>
    </Type>
</Types>

Take the above XML and save it to a file…

NOTE: the file HAS to be saved with the extension .ps1xml

Ex: NunitSpecificationPowerShellExtensions.ps1xml

Step 2: Load the extended type definition into the PowerShell runtime.

Once you’ve saved the XML extended types to a file, you need to load it into the PowerShell runtime by executing the command below.

Update-TypeData -PrependPath NunitSpecificationPowerShellExtensions.ps1xml

Before executing the above statement…Let’s quickly look at a System.String’s members and properties – just to show you what the extension methods look like when applied inside of the runtime.image

After executing the Update-TypeData command you’ll notice there are a number of “ScriptMethod” MemberTypes added to the object.

image

Now if you try to execute one of those newly added extension methods, you may get the following error…

PS C:\> $testVar.ShouldEqual(”hello world”)

Exception calling “ShouldEqual” with “1″ argument(s): “Unable to find type [NUnit.Framework.Assert]: make sure that the

assembly containing this type is loaded.

At line:1 char:21

+ $testVar.ShouldEqual <<<< (”hello world”)

+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

+ FullyQualifiedErrorId : ScriptMethodRuntimeException

This is because we need to load the NUnit.Framework assembly into the runtime before we can leverage the extension methods.

[System.Reflection.Assembly]::LoadFrom(”C:\Program Files\NUnit 2.5.2\bin\net-2.0\framework\nunit.framework.dll”) | Out-Null

Now that the extension methods have been defined and loaded into the runtime, NUnit.Framework is loaded, we can now use the methods on any object that inherits from System.Object (which, as far as I know, is everything in PowerShell).

And now, everything you need in one script (if you have the xml extended type file saved somewhere…)

#
# Update-TypeData -prependPath C:\Code\NunitSpecificationPowerShellExtensions.ps1xml
#

[System.Reflection.Assembly]::LoadFrom("C:\Program Files\NUnit 2.5.2\bin\net-2.0\framework\nunit.framework.dll") | Out-Null

$true.ShouldBeTrue()

$false.ShouldBeFalse()

"a".ShouldEqual("a")

"a".ShouldNotEqual("b")

jQuery Long Dropdown Menu

Dropdown menus containing lots of items can easily go down below the fold when opened, making lower items inaccessible. Normally if your dropdowns are this long, you reconsider your navigational strategy. But with jQuery Long Dropdown Menu solution you can make your menu items scroll up and down as you move mouse through it.

Implementation is fairly simple and only requires an unordered list to create your dropdown menu. You simply set maximum height of the dropdowns and script will automatically calculate speed multiplier based on the height for scrolling menu items up and down.

Developed by Chris Coyier of CSS-Tricks; jQuery Long Dropdown Menu is available for download License Free.  You can find further information, demo & download on CSS-Tricks Website.

Similar Posts:

You can also stay updated by following us on Twitter, becoming a fan on Facebook or by subscribing to our FriendFeed.


Visual Studio 2010 Extension Manager

One of the features I think will be a big hit in Visual Studio 2010 is the Extension Manager. As of Beta 2, you can find the Extension Manager under the Tools menu.

extension manager menu

The Extension Manager allows you to download and install Visual Studio extensions from Microsoft and other 3rd parties. These extensions can include new controls and project templates for ASP.NET, Silverlight, and WPF, as well as new tools that work both inside and outside of Visual Studio. The first tool I saw when opening the Extension Manager was Gibraltar – a feature rich logging, monitoring, profiler tool recently demoed to me by eSymmetrix’s founder Jay Cincotta.

extension manager in actionClick for a closer look 

The Extension Manager is like having the Visual Studio Gallery built into Visual Studio, and saves you the hassle of going to the site, downloading a file, and double-clicking to install (the extension manager automates all this work with some help from web services exposed by the gallery). As a bonus, the manager even understands the dependencies between packages and can ensure you have everything you need for an extension to work. Some of the other extensions you’ll find on the site for 2010 include:

Could It Be Even Better?

Visual Studio 2010 is the most extensible version of Visual Studio yet, and the Extension Manager will really highlight this fact. But, could it be even better?

Yes it could!

It’s painful to get setup with everyday development tools. It’s also frustrating to see how quickly developers on other platforms can get up and running. Package managers in general and RubyGems in particular make setup easy in an open source environment where fragmentation and spontaneity supposedly ruin a good integration story.

I realize the following scenario has some issues (like licensing issues), but in an ideal world, I’d be able to open Visual Studio and tell it I want the following:

… and so on. When I come back from lunch everything is downloaded, installed, and ready to go.

Three weeks later I might get a notification that a new ASP.NET MVC Preview is available. I’d like to punch a button in Visual Studio to pull down the latest instead of poking around on CodePlex where downloads are hard to find.

Tools like the Extension Manager and Web Platform Installer are getting us closer, but getting setup and staying up-to-date is still too big of a drain on productivity.


  • Sponsored Links

  •  

  • .

    Copyright © 1996-2010 Answer My Query. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress