Testing Ruby Applications on Windows Azure - part 2: Output to File

In part 1, I presented an overview of testing on Windows Azure. One thing that I missed in the original article is that while there’s no testing framework for Ruby that exposes a web front-end, it’s perfectly possible to have your test results saved out to a file.

The Test

For this example, I’m using RSpec and formatting the output as HTML. I’m also using the deployment sample I mentioned in a previous post. For the test, I created a test for a user class, user_spec.rb. This is stored in the ‘\WorkerRole\app\spec’ folder, along with spec_helper.rb. Spec_helper.rb just has an include for the class, require_relative '../user' since the user.rb file is in the app folder.

The contents of the user_spec.rb file are:

 require 'spec_helper'

describe User do
    before :each do
        @user = User.new "Larry", "Franks", "larry.franks@somewhere.com"
    end
    describe "#new" do
        it "takes user's first name, last name, and e-mail, and then returns a User object" do
            @user.should be_an_instance_of User
        end
    end
    describe "#firstname" do
        it "returns the user's first name" do
            @user.firstname.should == "Larry"
        end
    end
    describe "#lastname" do
        it "returns the last user's last name" do
            @user.lastname.should == "Franks"
        end
    end
    describe "#email" do
        it "returns the user's email" do
            @user.email.should == "larry.franks@somewhere.com"
        end
    end
end

So really all this does is create an User and verify you can set a few properties. The user.rb file that contains the class being tested looks like this:

 class User
    attr_accessor :firstname, :lastname, :email
    def initialize firstname, lastname, email
        @firstname = firstname
        @lastname = lastname
        @email = email
    end
end

Running the Test

In order to run this test once the project is deployed to Windows Azure, I created a batch file, runTests.cmd, which is located in the application root: ‘\WorkerRole\app\runTests.cmd’. This file contains the following:

 REM Strip the trailing backslash (if present)
if %RUBY_PATH:~-1%==\ SET RUBY_PATH=%RUBY_PATH:~0,-1%

cd /d "%~dp0"

set PATH=%PATH%;%RUBY_PATH%\bin;%RUBY_PATH%\lib\ruby\gems\1.9.1\bin;

cd app

call rspec -fh -opublic\testresults.html

Note that this adds an additional \bin directory to the path; the one in \lib\ruby\gems\1.9.1\bin. For some reason, this is where the gem batch files/executables are installed when running on Windows Azure. Bundler ends up in the %RUBY_PATH%\bin, but everything installed by Bundler ends up in the 1.9.1\bin folder.

The runTests.cmd is also added as a

in the ServiceDefinition.csdef file. It should be added as the last task in the section so that it runs after Ruby, DevKit, and Bundler have be installed and processed gems in the Gemfile. The

entry I’m using is:

 <Task commandLine="runTests.cmd" executionContext="elevated">
                <Environment>
                    <Variable name="RUBY_PATH">
                        <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='ruby']/@path" />
                    </Variable>
                </Environment>
            </Task>

Going back to the runTests.cmd file, note that the last statement runs rspec, -fh instructs it to format the output as HTML, and -o instructs it to save the output in the public\testresults.html folder (under ’\WorkerRole\app). Since I’m using Thin as my web server, the public directory is the default for static files.

After deploying the project to Windows Azure and waiting for it to start, I can then browse to https://whateverurl/testresults.html to see the test results. Note that by default this is publicly viewable to anyone who knows the filename, so if you don't want anyone seeing your test output you'll need to secure it. Maybe set up a specific path in app.rb that returns the file only when a certain users logs in. Also, this isn't something you need to run all the time. Run your tests in staging until you're satisfied everything is working correctly, then remove the runTests.cmd and associated <Task> for subsequent deployments.

Summary

So running tests in Windows Azure turns out to not be that hard, it’s just a matter of packaging the tests as part of the application, then invoking the test package through a batch file as a startup task. In this example I save the output to a file that I can remotely open in the browser. But what about when my tests are failing and I want to actually play around with the code to fix them and not have to redeploy to the Azure platform to test my changes? I’ll cover that next time by showing you how to enable remote desktop for your deployment.