Note
|
This is an archived project. For the up-to-date version of test bench, see link:https://github.com/test-bench/test-bench. |
Test Bench is a test framework for ruby designed to offer the minimum set of features necessary to test well designed code effectively. There are no hooks for test setup, teardown, defining variables, sharing tests, custom reporters, or plugins. There is no mocking library. There is but one output format, inspired by RSpec’s original output. There is no DSL in play that resembles human language. There are no matchers or a large bank of esoteric assertions you will probably never use. In fact, there are only five methods: assert
, refute
, test
, context
, and comment
.
For more information about why Test Bench was created and the reasoning behind it’s design, see Rationale.
Before getting started, I recommend skimming through the documentation referenced after this guide. Afterwards, to get started with Test Bench, either install the test_bench_legacy
gem on your local system or add it to a project you’d like to use Test Bench with.
To install the gem locally:
# gem install test_bench_legacy
Or, to add it to the project via Bundler:
gem 'test_bench_legacy'
Second, write a test loader, e.g. tests/test_helper
:
# Begin tests/test_helper.rb
# Load Test Bench and then activate it
require 'test_bench_legacy/activate'
# Load the code under test
require_relative '../lib/my/code.rb'
# End tests/test_helper.rb
Then add ruby files to your test directory that require the test loader via require_relative
:
# Begin tests/some_test_file.rb
require_relative './test_helper'
context "Some subject" do
test "Some test" do
assert true
end
context "Some nuance" do
test "Some other test" do
assert false # Will fail!
end
end
end
# End tests/some_test_file.rb
Now, if you want to run that test file, just load it through the ruby executable, e.g. ruby tests/some_test_file.rb
. If you want to run all the tests in the tests/
directory, the bench
executable that ships with the test_bench_legacy
gem can do just that:
# bench tests/
That will get you started, though it’s a good idea to read through all the documentation. Also, that bench
executable offers a few useful options, so be sure to check out the help via bench --help
. Finally, Test Bench’s test suite itself serves as an example for reference.