Smoke Testing The Web With Powershell

written by Scott Muc on Tuesday, May 04 2010

 Today I finally sat down and really worked on some powershell code. My end goal is to write some code that performs a series of smoke tests on an integration environment. I was hoping to use a unit testing framework that's already out there but I didn't quite like it. I wanted auto registration and execution of all my tests using some simple conventions. Here's what I spiked out:

As of right now, I have 2 files. One is the Test-Runner and it looks like this:

# Fixtures is currently a hard coded path
Get-ChildItem .\Fixtures | % { . Fixtures\$_ }

ls function:test-* |% { Write-Host -fore DarkCyan $_.Name; iex $_}

The first line dot sources all the files in the Fixtures sub-directory. These are the scripts that will contain tests (which I'll show later). The next line gets all the functions that start with 'Test-', writes the name to the console, then executes the function. So I have 2 conventions in place so far. All test functions must have the 'Test-' prefixed to the function name, and all tests must live in the Fixtures sub-directory.

Now I can write some very simple tests like the following:

function Get-Url($url) {
    Trap { Write-Host -ForegroundColor red Could not access webserver: $_.Exception.Message; Continue }
    
    $wc = New-Object System.Net.WebClient
    $wc.DownloadString($url)
}

function Assert-Url($url, $pattern) {
    Write-Host Making HTTP request to $url

    if (Get-Url $url -match $pattern) {
        Write-Host -ForegroundColor green Success
    } else {
        Write-Host -ForegroundColor red Failure: Response from $url does not match `'$pattern`'
    }
}


function Test-PassingTest() {
    $test_url = "http://scottmuc.com/blog/development/getting-my-build-and-release-on/"
    $expected_response_pattern  = 'Getting My Build and Release On'

    Assert-Url $test_url $expected_response_pattern
}

function Test-FailingTest() {
    $test_url = "http://scottmuc.com/doesnotexist"
    $expected_response_pattern  = 'not important'

    Assert-Url $test_url $expected_response_pattern
}


 Here you can see that I have two helper functions and two tests. The passing test asserts that a certain url contains a particular regular expression, and the failing test hits a non existent page. When executing the Test-Runner here's the output:

My next set of assertions will be to check for Windows Services running on remote servers. I can already see that it's going to be quite easy to setup those tests.

 

Similar Posts

  1. Unit Testing Domain Persistence With NDbUnit, NHibernate and SQLite
  2. My Developer Resolutions For 2009
  3. My Developer Resolutions For 2010

Comments

  • Youssuf on on 5.16.2010 at 3:10 PM

    Youssuf avatar

    Have you looked at writing your smoke tests using Selenium (http://seleniumhq.org/)?

  • Celeron504 on on 6.09.2010 at 5:10 PM

    Celeron504 avatar

    @ Youssuf

    Why use Selenium when you have to install Java?

    @ Scott

    Great article! I too had wanted to try the other framework but was put off by the lack of updates. Thanks for pointing me in the right direction.

Post a comment

Options:

Size

Colors