# Test/spec 0.2, a BDD interface for Test::Unit

**URL:** <https://www.ruby-forum.com/t/test-spec-0-2-a-bdd-interface-for-test-unit/72761>\
**Category:** Ruby\
**Created:** [October 18, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/test-spec-0-2-a-bdd-interface-for-test-unit/72761 "2006-10-18T18:27:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cneuhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/34f0e0/32.png) [@cneuhaus](https://www.ruby-forum.com/u/cneuhaus)\
**Post date:** [October 18, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/test-spec-0-2-a-bdd-interface-for-test-unit/72761/1 "2006-10-18T18:27:42Z")

</div>

Hello,

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 does not include a mocking/stubbing-framework; use whichever  
you like to—FlexMock and Mocha have been tested.

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.)

== Wrapped assertions

assert\_equal: should.equal, should ==  
assert\_not\_equal: should.not.equal, should.not ==  
assert\_same: should.be  
assert\_not\_same: should.not.be  
assert\_nil: should.be.nil  
assert\_not\_nil: should.not.be.nil

assert\_in\_delta: should.be.close  
assert\_match: should.match, should =~  
assert\_no\_match: should.not.match, should.not =~

assert\_instance\_of: should.be.an.instance\_of  
assert\_kind\_of: should.be.a.kind\_of  
assert\_respond\_to: should.respond\_to

assert\_raise: should.raise  
assert\_nothing\_raised: should.not.raise  
assert\_throws: should.throw  
assert\_nothing\_thrown: should.not.throw  
assert\_block: should.satisfy

== Additional assertions

These assertions are not included in Test::Unit, but have been added  
to test/spec for convenience:

- should.not.satisfy
- a.should. (works like assert a.?)
- a.should.be (where is \<, \<=, \>, \>=, or ===)
- 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 –runner=specdox (or -rs) looks  
like RSpec’s output:

```
spec.output
- works for print
- works for puts
- works with readline

```

RDox, run with –runner=rdox (or -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

```

== specrb

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

```

See specrb --help for the usage.

== Changes in version 0.2

- Better, module-based implementation
- Official support for FlexMock and Mocha
- More robust Should#output
- Should#_operator_
- Nested contexts
- Standalone test/spec runner, specrb
- -w warning free

== Roadmap

Version 0.3 (November 2006):: deprecate underscore forms.

Version 1.0 (Late November 2006):: first stable release.

== Contact

Please mail bugs, suggestions and patches to  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).

Darcs repository (“darcs send” is welcome for patches):  
[http://chneukirchen.org/repos/testspec](http://chneukirchen.org/repos/testspec)

== Thanks to

- Eero S. for writing should.output.
- Thomas F. for [script.aculo.us](http://script.aculo.us) BDD testing which convinced me.
- Dave A. for BDD.
- The RSpec team for API inspiration.
- Nathaniel T. for Test::Unit.

== Copying

Copyright (C) 2006 Christian N.  
[http://purl.org/net/chneukirchen](http://purl.org/net/chneukirchen)  
test/spec is licensed under the same terms as Ruby itself.

== Where can I get it?

You can download test/spec 0.2 at

```
    http://chneukirchen.org/releases/testspec-0.2.0.tar.gz

```

Alternatively, you can checkout from the development repository with:

```
       darcs get http://chneukirchen.org/repos/testspec

```

(Patches using “darcs send” are most welcome.)

== Links

Behavior-Driven Development:: [http://behaviour-driven.org/](http://behaviour-driven.org/)  
RSpec:: [http://rspec.rubyforge.org/](http://rspec.rubyforge.org/)  
[script.aculo.us](http://script.aculo.us) testing::  
[http://mir.aculo.us/articles/2006/08/29/bdd-style-javascript-testing](http://mir.aculo.us/articles/2006/08/29/bdd-style-javascript-testing)  
FlexMock:: [http://onestepback.org/software/flexmock/](http://onestepback.org/software/flexmock/)  
Mocha:: [http://mocha.rubyforge.org/](http://mocha.rubyforge.org/)

Happy hacking and have a nice day,  
Christian N.

d59a0412f8930bf8354d1fe2e089a49d testspec-0.2.0.tar.gz

---

<div class="post-metadata">

**Author:** ![cneuhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/34f0e0/32.png) [@cneuhaus](https://www.ruby-forum.com/u/cneuhaus)\
**Post date:** [October 19, 2006, 8:09am UTC](https://www.ruby-forum.com/t/test-spec-0-2-a-bdd-interface-for-test-unit/72761/2 "2006-10-19T08:09:54Z")

</div>

On 10/18/06, Christian N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> == Roadmap
> 
> Version 0.3 (November 2006):: deprecate underscore forms.

Hi Christian

This all looks very neat, I’d like to give it a try. Two quick  
questions:

- have you heard any feedback on how well it works w/i rails,  
especially the multiple aliases of setup/teardown and similiar hackery  
that goes on?

- why are.method.calls done with periods instead\_of nice underscores?

- Rob

---

<div class="post-metadata">

**Author:** ![cneuhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/34f0e0/32.png) [@cneuhaus](https://www.ruby-forum.com/u/cneuhaus)\
**Post date:** [October 19, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/test-spec-0-2-a-bdd-interface-for-test-unit/72761/3 "2006-10-19T19:09:33Z")

</div>

“Rob S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Hi Christian
> 
> This all looks very neat, I’d like to give it a try. Two quick questions:
> 
> - have you heard any feedback on how well it works w/i rails,  
> especially the multiple aliases of setup/teardown and similiar hackery  
> that goes on?

I didn’t try it with rails yet, but it uses the same mechanisms as the  
mocking libraries, so it should work. Feedback is welcome.

> - why are.method.calls done with periods instead\_of nice underscores?

Because that’s far easier to implement and extend (and I actually  
think it looks better).
