Test::Unit compatibility for Rspec 2.
Just add this to your code:
and then you can write test classes like this:
class FooTest < Rspec::Unit::TestCase def test_foo assert_equal 3, Foo::major_version end end
test_info method, you can attach metadata to the next
defined test (this works much the same way Rake's
attaches a description string to the next defined task):
test_info :speed => 'slow', :run => 'nightly' def test_tarantula_multipass # ... end
You can also attach metadata to the entire class with the
class BarTest < Rspec::Unit::TestCase test_case_info :integration => true # ... end
Each instance of
Rspec::Unit::TestCase is equivalent to an
describe block, so it can also include
after blocks, and nested
describe blocks. Test
example blocks can contain either assertions or
test blocks (as found in Rails 2.x) also work.
Additionally, assertions can be used inside ordinary Rspec examples.
This gem is the rough equivalent, for Rspec 2, of the test/unit compatibility that was a part of the core Rspec gem in Rspec 1. The new Rspec runner design makes it quite easy to implement this functionality as a separate gem, which seems like a better choice in many ways.
Currently, test/unit compatibility is much more limited than in Rspec 1. The goal is not to make Rspec 2 a drop-in replacement for test/unit; rather, we have two more limited goals:
- to allow Rspec 2 examples to easily make use of test/unit assertions in cases where those assertions are valuable, or where assertions might be the best way to express particular expectations.
- to make it easy for a project to switch an existing test/unit suite over to run under Rspec, as the start of a gradual, piecemeal conversion to Rspec.
As such, there are some things we don''t support:
- The top-level module name is different. For example, one requires
test/unit, and extends
- TestSuite is not supported. The Rspec 2 metadata features are far more flexible than test/unit-style suites.
- Because of the very different implementation, many test/unit extensions will not run properly.
- All test output and summaries are in Rspec style; test/unit-compatible output is not supported.
We will certainly consider supporting those things if there is demand.
I originally wrote this test/unit compatibility gem for Micronaut, a lightweight Rspec clone by Chad Humphries. Micronaut has been rolled into Rspec as the core of Rspec 2, and I was able to move the test/unit compatibility over with minimal changes.
The point of this gem is not that I think test/unit is a better way to write tests than the Rspec style. I admit that I'm a TDD oldtimer who sees Rspec as mostly a cosmetic (rather than fundamental) change, but that doesn't mean it's not an important change. My curmudgeonly nature has its limits, and I do find specs a big improvement.
So why rspec-unit? Three reasons:
- I wanted to show off the generality of Micronaut's (and now Rspec's) architecture. I hope rspec-unit can serve as an example for anyone who wants to experiment with new ways of expressing tests and specs on top of Rspec.
- Many projects with existing test/unit test suites might want to benefit from the metadata goodness in Rspec 2, or begin a gradual, piecemeal change to an Rspec style. That's pretty easy to do with rspec-unit.
- Even when writing specs and examples, I frequently encounter
cases where an assertion is more expressive than a
shouldexpression. It's nice just to have assertions supported within Rspec examples.
It would be nice to try using the assertion code from minitest, which is much more compact and seems less coupled than that from test/unit.
Copyright (c) 2009, 2010 Glenn Vanderburg. See LICENSE for details.