Get HTML output from Test::Unit by using RSpec !?

Test::Unit‘s lack of pretty xml/html output led me to look around to see what was available. Turns out that there’s a very simple way to get html output… run your Test::Unit tests with rspec! Here’s a contrived and simple example of how it’s done.

We’ll start with a file called test_unit_to_rspec.rb containing only two tests; one that passes and one that fails. Here’s the code:

require "rubygems"
require "test/unit"
class SomeTests < Test::Unit::TestCase
  def test_that_passes
    assert(true)
  end
  def test_that_fails
    assert(false)
  end
end

To run this, we call ruby test_unit_to_rspec.rb and we get the following output:

Loaded suite /Users/nat/dev/test_unit_to_rspec
Started
F.
Finished in 0.011906 seconds.
1) Failure:
test_that_fails:8
is not true.
2 tests, 2 assertions, 1 failures, 0 errors

A ‘.’ and an ‘F’ in the result tells us that one test passed and one failed. Notice, no pretty html. No xml. This is a sad state of affairs and needs to be fixed. We want pretty. We want red. We want green. We want output that will be useful to people other than the test team. So… to fix this…

First, you’ll need to install the syntax gem (gem install syntax). This adds extra prettiness to the output – and that’s what we’re about at the moment! Then, in our test_unit_to_rspec.rb file…

…replace :

require "test/unit"

…with:

require "spec/test/unit"

The new code will look like this:

require "rubygems"
require "spec/test/unit"
class SomeTests < Test::Unit::TestCase
  def test_that_passes
    assert(true)
  end
  def test_that_fails
    assert(false)
  end
end

Next, run your tests using the following command (notice, no ruby in the command):

spec test_unit_to_rspec.rb -f h > results.html

When the run is finished, you’ll find pretty html output in the new file named results.html; here’s what it looks like:

rspec_output

Pretty! Red! Green! Summaries! The code that failed! A whole new world of beautiful (and useful)…

Note that this is only the first step in converting to rspec from test::unit. Doing the above will give you none of the power of rspec, only the pretty output. Also, I imagine that as I get around to converting bigger test::unit projects, conversion won’t be this easy…

This entry was posted in Example Code, Making Life Easier, RSpec, Ruby, Test::Unit and tagged , , , , , . Bookmark the permalink.

6 Responses to Get HTML output from Test::Unit by using RSpec !?

  1. Alister says:

    Thanks for the tip.
    Do you know how to get the output to show details of positive assertions too? Like it does for failed assertions.

  2. admin says:

    Not sure, but I’ll take a look. I don’t have high hopes though, since test::unit doesn’t give any details for tests that passed – other than the fact that they passed…

  3. admin says:

    Hmmm… can’t find anything… I had a look through the options that ‘spec -help’ prints out and couldn’t find anything.

    It brings up the question though; when would you want to know the details of assertions that passed?

  4. Alister says:

    I guess if you’re comparing Watir/Test::Unit to something like QuickTest Pro then QTP has a HTML-esque results page that shows every action and every assertion (I think they call it a pass/fail).

    I guess in Watir this would be a logging function, I know there are some Watir loggers available, but I haven’t found an awesome one yet.

  5. Nat says:

    Replicating QTP functionality with watir is the last thing I’d want to do ;)

    Looking at the details of a test that passed is not something I do often. If I’m getting false passes from a test, I generally look at it in isolation where logging isn’t important. When doing a full test run I’m generally uninterested in tests that pass – only in ones that fail, so I’ve never needed to log assertions that passed…

  6. jw says:

    nice. going to see if i can use this to prettify some new tests

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>