Tuesday, July 20, 2010

Fitnesse TestRunner Scripting in PowerShell

I'm running Fitnesse with SharePoint and in trying to automate my tests I ended up wrapping everything up in PowerShell, this is my first full script that handles all the tests I wanted to do.

First set up all the variables needed.
[string]$fitnesseJar = "fitnesse.jar"
[string]$fitnesseClass = "fitnesse.runner.TestRunner"
# Need to get the local machine here, it will be where Fitnesse runs
[string]$serverName = $env:COMPUTERNAME
[string]$portName = "8080"
# These are needed for the CheckFitnessProcess function, placing them here
$fitRunning = $false
# Since we need to manipulate this variable in a few different functions, its Global
[int]$Global:startTries = 0
# Set Java executbale to use within PowerShell
$Java = "java.exe"
# Use a Test Array to be able to bundle all the tests
$smokeArray = ( "FitNesseTestPageName1" )
$qaArray = ( "FitNesseTestPageName2" )

I use a function to check if Fitnesse is running, I wanted to only run it when I needed to and since this script can be automated I cannot be sure that Fitnesse will always be running
# CheckFitnesseProcess determines if Fitnesse is running; in which case
# it jumps down to running the tests, as long as Fitness is running the Check does
# not need to do anything else.
# If Fitnesse is not running then the script attempts to start it, does a
# check to make sure that Fitnesse started then runs the test. If this script
# starts Fitnesse then it will attempt to clean up after itself and stop
# Fitnesse by doing a check on $startTries, if this is > 0 then this script
# started Fitnesse and can shut it down, otherwise it leaves the existing
# process running
# Adapted from: http://odetocode.com/Blogs/scott/archive/2006/07/17/5330.aspx
function CheckFitnesseProcess {
param ([string]$AppPoolName="")
$ProcessId = $null
if ($AppPoolName.ToUpper() -ne $NOT_PASSED) {
[string]$filter = $null
if ((Get-Process java -ErrorAction SilentlyContinue) -ne $null) {
Get-Process java | ForEach {
# use WMI API to get process details
$filter = "Handle='" + $_.Id + "'"
$wmip = Get-WmiObject Win32_Process -filter $filter
# CommandLine property has AppPool name; grab name using contains
if($wmip.CommandLine.contains("fitnesse")) {
# Uncomment the next line for debugging
# Write-Host "Found this java process: $wmip.CommandLine `n"
$fitRunning = $true
# Saving the Process ID to stop the process later,
# using a Global as I could not get this work if I declared
# the variable at the beginning.
$Global:fitProcess = $_.Id
}
# The next statement was put in to check for the processes, there was a problem
# when updating Java that the process find command did not work, if this happens
# again just uncomment the Write-Host line to find out what is going on, otherwise
# this just gets bypassed but caught later on.
if ($fitRunning -eq $false)
{
# Uncomment the next line for debugging
# Write-Host "Did not find a Java/Fitnesse Process. Found $wmip.CommandLine `n"
}
}
}
if (($fitRunning -eq $false) -and ($Global:startTries -lt 3)) {
# Don't need to note that we have had 0 attempts
if ($Global:startTries -gt 0) {
# Need to give Fitnesse some attempts at starting, 3 is a good max
Write-Host "Fitnesse is not running, have tried to start $Global:startTries time(s),`n"
}
$Global:startTries++
startFitnesse
}
if (($fitRunning -eq $false) -and ($Global:startTries -eq 3)) {
# At some point we need to give up starting it
Write-Host "Fitnesse is not running, tried to start $Global:startTries time(s).`n"
Write-Host "Strike Out!" -ForegroundColor Red
Write-Host "Looks like Fitnesse cannot be started or found,`n"
Write-Host "check the server and try again.`n"
# Go back to where we started
Pop-Location
exit;
}
if ($fitRunning -eq $true) {
# Success!!
Write-Host "Fitnesse is good to go.`n" -ForegroundColor Green
}
}
}


Then I needed something to start Fitnesse, this uses the Fitnesse Batch script that comes with many of the installs
function startFitnesse()
{
Write-Host "Attempting to start up Fitnesse...`n"
# Starts the Fitnesse process using the batch configuration file
# Not sure we need the runAs Admin but that can be adjusted
Start-Process -FilePath fitnesse.bat
# Giving it time to wake up
Start-Sleep -Seconds 2
# Need to check that the process actually started, do the check again
CheckFitnesseProcess
}

Every script needs a help option
function myHelp()
{
Write-Host "The Fitnesse Tester understands the following command line arguments:`n"
Write-Host " qa - will run the QA Test Suites, a long set of tests`n"
Write-Host " smoke - which will run the Build Smoke Tests, a short set`n"
Write-Host "`n Running this script with no arguments will display this message`n"
}

Then there is the main and rest of the script that runs everything
function main()
{
# Need to be in the Fitnesse directory to access TestRunner, and
# the Fitnesse batch file in case Fitnesse needs to be started
cd $fitnesseLocale
#Check and see if Fitnesse is running
CheckFitnesseProcess
foreach ($test in $testArray)
{
# Start the test
Write-Host "`nRunning $test`n"
# Get the output from TestRunner so we can scrape it for test passes and fails
$testResults = & $Java "-cp" "$fitnesseJar" "$fitnesseClass" "-v" "$serverName" "$portName" "$test"
Write-Host "Completed $test`n"
Write-Host "Test Results..."
# Scan through the output, then display the results
# Dump $testResults out to a File
$testResults | Out-File "$test.txt"
foreach ($line in $testResults)
{
if ($line -match "Assertions:") {
# Write-Host "$line"
# Do a regular expression to get the Assertion info
# Example: Assertions: 8 right, 0 wrong, 0 ignored, 0 exceptions
$line -match ":\s(?\d+)\sr.*(?\d+)\sw.*(?\d+)\si.*(?\d+)\se.*"
# Send a summary of the results to the console
Write-Host "Passed: "$matches.right -ForegroundColor Green
Write-Host "Failed: "$matches.wrong -ForegroundColor Red
Write-Host "Ignored: "$matches.ignores
Write-Host "Exceptions: "$matches.exception -ForegroundColor Yellow
}
}
}
# Go back to where we started
Pop-Location
# Stop the Fitnesse process so we don't leave things running
# But only if this script started it, which means $startTries is > 0
if ($Global:startTries -ge 1){
Write-Host "`nSince this script started Fitnesse, it'll be shut down now.`n"
Stop-Process -Id $fitProcess
}
}

# Check command line values to see what test is running
if ($args -eq "smoke")
{
$testArray = $smokeArray
Write-Host "Running Smoke Test.`n"
}
elseif ($args -eq "qa")
{
$testArray = $qaArray
Write-Host "Running QA Test Suite.`n"
}
elseif ($args -eq "check")
{
$testArray = $checkArray
Write-Host "Running Check Suite.`n"
}
else
{
myHelp
exit;
}
# Run main
main

I've moved onto REST but in case anyone else is trying to work with the TestRunner and PowerShell you have something you can start with, and not have to work it all from the beginning like I did.