Tuesday, January 22, 2013

PowerShell as a Testing Environment


In developing ASP.NET web applications, I got tired of the cycle of compile, wait for JIT compile and app domain load, click [click|type, click|type...], validate. It is useful to create test harnesses to validate output from things like string parsing and XSL transformations, and any non-trivial application should have some sort of automated testing performed on it. But I don't like the ridiculous amount of overhead generated by TDD (Test-Driven Development) models, and any test harness can become so much extra work that for small methods it hardly seems worth the effort just to see the effect. Or consider exploring an API that you know will give the desired output, if only you can find the right methods and properties.

However, there is an alternative: Windows PowerShell. Due to its integration with the .NET framework, one can easily write C# code directly on the command line to experiment with and test different objects while incurring no real overhead. Even better, any binary loaded in the GAC can be referenced directly without importing anything. 

Here's a session from my prototyping of some code to parse a URI:

PS C:\Users\Darton Williams.IOTA-CORP> [System.Uri]::TryCreate("dkw.mce.dev",[System.UriKind]::Absolute, [ref] $url)

False

PS C:\Users\Darton Williams.IOTA-CORP>[System.Uri]::TryCreate("http://dkw.mce.dev", [System.UriKind]::Absolute, [ref] $url)

True

PS C:\Users\Darton Williams.IOTA-CORP> Write-Host $url.AbsoluteUri

http://dkw.mce.dev/

PS C:\Users\Darton Williams.IOTA-CORP>[System.Uri]::TryCreate("http://dkw.mce.dev/abc", [System.UriKind]::Absolute, [ref] $url)

True

PS C:\Users\Darton Williams.IOTA-CORP> Write-Host $url.AbsoluteUri

http://dkw.mce.dev/abc

PS C:\Users\Darton Williams.IOTA-CORP> $urltext = $url.GetComponents([System.UriComponents]::SchemeAndServer, [System.UriFormat]::SafeUnescaped)

PS C:\Users\Darton Williams.IOTA-CORP> [System.Uri]::TryCreate($urltext, [System.UriKind]::Absolute, [ref] $url)

True

PS C:\Users\Darton Williams.IOTA-CORP> Write-Host $url.AbsoluteUri

http://dkw.mce.dev/

No comments:

Post a Comment