Spectrum is a library of test structures: the words that organize tests into cases, specifications or features. Pick the one you like to write in; each is loaded on its own.
| Require | Structure |
|---|---|
spectrum/testcase |
TestCase "..." do, test "..." do, setup, teardown |
spectrum/describe |
describe "..." do, it "..." do, before, after, let |
spectrum/feature |
Feature "..." do, Scenario "..." do, Given, When, Then |
Spectrum does two things and leaves the rest to others:
- Running is done by Rubytest. Every structure builds test objects that Rubytest understands, so its command line, report formats and filters work with all of them.
- Asserting is left to an assertion library. Spectrum is the counterpart of Assay, which offers a choice of assertion notations in the same way, but anything that raises an assertion Rubytest recognizes will do.
So a project chooses one of each:
# test/helper.rb
require 'spectrum/describe' # how tests are organized
require 'assay/ae/should' # how they assert$ rubytest -Ilib -r ./test/helper test/*_spec.rb
The examples below use Assay's AE notation.
require 'spectrum/testcase'
TestCase Array do
setup do
@list = [1, 2, 3]
end
test "has a size" do
@list.size.assert == 3
end
context "#join" do
test "joins the elements" do
@list.join.assert == "123"
end
end
endTestCaseis also spelledtest_caseortestcase, andtest,context,setup,teardownandskipmay be capitalized.- Each test runs in an instance of its own, after
setupand beforeteardown. A context inherits its parent's. - Methods defined in a test case are helpers for its tests.
- A test without a block is reported as still to be done.
A test whose block takes arguments is parameterized, and each ok
after it makes a test of it:
test "is even" do |x|
x.assert.even?
end
ok 2
ok 4To skip tests, with a reason:
skip "waiting on the new parser" do
test "parses the new syntax" do
...
end
endrequire 'spectrum/describe'
describe Array do
let(:list) { [1, 2, 3] }
before :each do
@calls = 0
end
it "has a size" do
list.size.should == 3
end
describe "#join" do
it "joins the elements" do
list.join.should == "123"
end
end
it "is something to get to"
enddescribeis alsocontextwithin a specification, anditis alsoexample.beforeandaftertake:each(the default) or:all, and for:eachmay be limited to examples matching a label, pattern or tag:before :each, /database/ do.letandlet!define values,subjectthe subject, anditsan example about a method of the subject.shared_examples_for "..."defines examples thatit_behaves_like "..."brings in.pending "reason"inside an example, or an example without a block, reports it as still to be done.skip "reason", /pattern/skips the examples of the context that match, andskip "reason"all of them.omit /pattern/leaves them out altogether.
When a specification describes a class, a method the example doesn't have is sent to a new instance of that class:
describe Array do
it "starts out empty" do
size.should == 0
end
endrequire 'spectrum/feature'
Feature "Addition" do
To "avoid silly mistakes"
As "a math idiot"
I "need to calculate the sum of numbers"
Scenario "Add two numbers" do
Given "I have entered 50 into the calculator"
And "I have entered 70 into the calculator"
When "I press add"
Then "the result should be 120 on the screen"
end
Given 'I have entered (\d+) into the calculator' do |n|
@calculator ||= Calculator.new
@calculator.push n.to_i
end
When 'I press add' do
@result = @calculator.add
end
Then 'the result should be (\d+) on the screen' do |n|
@result.assert == n.to_i
end
endA scenario lists its steps; the feature defines them. A step is run by the first definition of its kind whose description matches the whole step, ignoring case and the amount of whitespace. Text in parentheses is a regular expression, and what it matches is passed to the block.
- Each step is reported as a test. If a step fails, the ones after it in the scenario are not run.
- A step with no definition is reported as still to be done.
- Each scenario runs in a scope of its own.
skip "reason"skips the scenario that follows.
Step definitions can be shared between features by putting them in a
module that includes Spectrum::Featurette:
module CalculatorSteps
include Spectrum::Featurette
When 'I press add' do
@result = @calculator.add
end
end
Feature "Addition" do
include CalculatorSteps
...
endFiles in a featurettes directory next to a feature are loaded
automatically.
A method defined in Spectrum::World is available inside every test,
example and step, whichever structure it is written in.
module Spectrum::World
def fixture(name)
File.read("test/fixtures/#{name}")
end
endSpectrum combines three earlier gems, which it replaces:
| Before | Now |
|---|---|
gem citron, require 'citron' |
require 'spectrum/testcase' |
gem spectroscope, require 'spectroscope' |
require 'spectrum/describe' |
gem lime, require 'lime' |
require 'spectrum/feature' |
The words are the same. What changed is listed in HISTORY.md; in short, several things that silently did the wrong thing now don't.
Lemon, the strict unit test framework, is a separate gem and also runs on Rubytest.
$ gem install spectrum
Copyright (c) 2011 Rubyworks
Spectrum is distributed under the terms of the BSD-2-Clause license. See LICENSE.txt for details.