Today I’m releasing test/spec 0.2, a library to do BDD with Test::Unit.
(See below for changes in version 0.2.)
What is test/spec?
test/spec layers an RSpec-inspired interface on top of Test::Unit, so you can mix TDD and BDD (Behavior-Driven Development).
test/spec is a clean-room implementation that maps most kinds of Test::Unit assertions to a ‘should’-like syntax.
Consider this Test::Unit test case:
class TestFoo < Test::Unit::TestCase def test_should_bar assert_equal 5, 2 + 3 end end
In test/spec, it looks like this:
require 'test/spec' context "Foo" do specify "should bar" do (2 + 3).should.equal 5 end end
test/spec has no dependencies outside Ruby 1.8.
Mixing test/spec and test/unit
test/spec and Test::Unit contexts/test cases can be intermixed freely, run in the same test and live in the same files. You can just add them to your Rake::TestTask, too. test/spec allows you to leverage your full existing Test::Unit infrastructure.
test/spec does not change Test::Unit with the exception of monkey-patching Test::Unit::TestSuite to order the test cases before running them. (This should not do any harm, but if you know a way around it, please tell me.)
These assertions are not included in Test::Unit, but have been added to test/spec for convenience:
a.should.be operator(where operator is
should.output, to check what is printed
SpecDox and RDox
test/spec adds two additional test runners to Test::Unit, based on the console runner but with a different output format.
SpecDox, run with
like RSpec’s output:
spec.output - works for print - works for puts - works with readline
RDox, run with
-rr) can be
included for RDoc documentation (e.g. see SPECS):
== spec.output * works for print * works for puts * works with readline
SpecDox and RDox work for Test::Unit too:
$ ruby -r test/spec test/testunit/test_testresult.rb -rs Test::Unit::TC_TestResult - fault notification - passed? - result changed notification Finished in 0.106647 seconds. 3 specifications (30 requirements), 0 failures
Since version 0.2, test/spec features a standalone test runner called specrb. specrb is like an extended version of testrb, Test::Unit’s test runner, but has additional options. It can be used for plain Test::Unit suites, too.
$ specrb -a -s -n should.output should.output - works for print - works for puts - works with readline Finished in 0.162571 seconds. 3 specifications (6 requirements), 0 failures
specrb --help for the usage.
Changes in version 0.2
- Better, module-based implementation
- Official support for FlexMock and Mocha
- More robust
- Nested contexts
- Standalone test/spec runner, specrb
- -w warning free
Version 0.3 (November 2006): deprecate underscore forms.
Version 1.0 (Late November 2006): first stable release.
Please mail bugs, suggestions and patches to firstname.lastname@example.org.
Darcs repository (“darcs send” is welcome for patches):
- Eero Saynatkari for writing should.output.
- Thomas Fuchs for script.aculo.us BDD testing which convinced me.
- Dave Astels for BDD.
- The RSpec team for API inspiration.
- Nathaniel Talbott for Test::Unit.
Copyright (C) 2006 Christian Neukirchen
test/spec is licensed under the same terms as Ruby itself.
Where can I get it?
You can download test/spec 0.2 at:
Alternatively, you can checkout from the development repository with:
darcs get http://chneukirchen.org/repos/testspec
(Patches using “darcs send” are most welcome.)
Happy hacking and have a nice day, Christian Neukirchen
NP: David Gilmour—Mihalis