[Rspec-devel] Rspec/Test::Unit Integration

Nick Sieger nicksieger at gmail.com
Wed Jul 5 23:53:53 EDT 2006

On 7/4/06, Steven R. Baker <srbaker at pobox.com> wrote:
> > I had a conversation with David at the Rails Conference about
> > integrating Rspec into Test::Unit
> [snip]
> > If Rspec where to user Test::Unit::TestCase to make its assertions,
> > it would be easier to transition into using Rspec from Test::Unit.
> This is a very interesting point; David and I discussed this at
> RailsConf as well.  I would have liked to meet you at RailsConf, but
> I wasn't aware of your attendance.  (If we did cross paths in the
> confusion, accept my apologies.)  Be sure to track me down at the
> next one!

I half-participated/eavesdropped in a conversation with Aslak and another
fellow (sorry didn't catch your name!) after Aslak's lightning talk on this
subject also -- don't know if it was the same conversation or not.

First a disclaimer -- I have not used Rspec regularly yet, but have known
about it, and being a developer-testing advocate for a while, having used
and promoted the ideas espoused by tools like agiledox, I feel like I *got*
it pretty quickly after seeing Aslak's demo.

My idea for Test::Unit integration is this:  don't touch the spec DSL
itself, instead enable a mode (perhaps the default?) where executing a spec
generates and runs a subclass of Test::Unit instead.  This in theory would
allow you to mix "should" behavior specs with assertions inside of a
"specify" block.  The code to do this is pretty simple:

| require 'spec/version'
| require 'spec/api'
| require 'spec/runner'
| require 'test/unit'
| module Spec
|   module Runner
|     TEST_CLASSES = []
|     class TestunitContext < Context
|       def initialize(name, &context_block)
|         @test_class = Class.new(Test::Unit::TestCase)
|         super
|         TEST_CLASSES << @test_class
|       end
|       def setup(&block)
|         @test_class.send(:define_method, :setup, &block)
|       end
|       def teardown(&block)
|         @test_class.send(:define_method, :teardown, &block)
|       end
|       def specify(spec_name, &block)
|         @test_class.send(:define_method, "test #{spec_name}".to_sym,
|       end
|     end
|   end
| end
| module Kernel
|   def context(name, &block)
|     Spec::Runner::TestunitContext.new(name, &block)
|   end
| end

By putting this code in a file called 'specunit.rb' (also attached) in your
load path and requiring it instead of 'spec' in the file containing your
specs, I get Test::Unit execution:

ruby stack_spec.rb
Loaded suite stack_spec
Finished in 0.006394 seconds.

18 tests, 0 assertions, 0 failures, 0 errors

Again, I'm new to the list, so apologies if this sort of thing has been
proposed before.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20060705/89a9291f/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: specunit.rb
Type: application/octet-stream
Size: 828 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-devel/attachments/20060705/89a9291f/attachment.obj 

More information about the Rspec-devel mailing list