# Introducing SpecUnit

**URL:** <https://www.ruby-forum.com/t/introducing-specunit/72588>\
**Category:** Ruby\
**Created:** [October 18, 2006, 10:38am UTC](https://www.ruby-forum.com/t/introducing-specunit/72588 "2006-10-18T10:38:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trotter\_Cashion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@Trotter\_Cashion](https://www.ruby-forum.com/u/Trotter_Cashion)\
**Post date:** [October 18, 2006, 10:38am UTC](https://www.ruby-forum.com/t/introducing-specunit/72588/1 "2006-10-18T10:38:39Z")

</div>

Hello everyone,  
Two weeks ago at BarCamp NYC, I put together a little addition to  
Test::Unit that I’m tentatively calling SpecUnit. It clocks in at 61  
lines, is well tested, and adds nested contexts to Test::Unit.  
Install with ‘sudo gem install spec-unit’. Give it a look and let me  
know what you think. Below is the short readme I put together.

=Introduction  
SpecUnit is an extension to Test::Unit that allows tests to be grouped  
into contexts. The idea of contexts comes from rSpec, which is a BDD  
framework for Ruby. rSpec is a great testing framework, but I built  
SpecUnit out of the desire to make use of tools already in existence  
for Test::Unit and to provide a lightweight means of making use of  
contexts.

=Usage  
To use SpecUnit, simply require ‘spec-unit’

require ‘test/unit’  
require ‘spec-unit’

class TestBlob \< Test::Unit::TestCase  
include SpecUnit

```
 def setup
   @object = Blob.new
 end

 context 'when empty' do
   def specify_should_be_empty
     assert @object.empty?
   end
 end

 context 'when has one item' do
   def setup
     @object.add(2)
   end

   def specify_has_length_of_one
     assert_equal 1, @object.length
   end
 end

```

end

As you can see above, contexts can be nested. Setups are run outside  
in, while teardowns are run inside out. There should be no namespace  
conflicts as the library is fairly well tested, but let me know if you  
find any.

=Caveats  
I wrote this in a weekend, so there may be some things I didn’t think  
to test in my tests. Also, there are probably a number of cool  
features I could add. Let me know if there are any you want, and I’ll  
consider adding them. Submitting patches makes it much more likely  
that I’ll implement your feature.

=More Documentation  
Read the tests, let me know if they’re terribly unreadable. I’ll add  
some documentation to the SpecUnit module later. If you want to add  
documentation, please do. Send me a patch, and I will include it.

=About  
Author:: Trotter Cashion ([mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid))  
Copyright:: Copyright (c) 2006 Trotter Cashion  
License:: Distributed under the MIT License
