TestBench is a no-nonsense testing framework for Ruby aiming to offer precisely what is needed to test well-designed code effectively and easily. In stark contrast to most other testing frameworks, test files are written using a procedural API, not a declarative one, which brings a score of substantial benefits in exchange for largely immaterial drawbacks. As a result, TestBench can be learned very quickly. However, users should not expect it to offer many palliative features that ease the difficulties inherent to working with vast, long-running, or brittle test suites. With that in mind, even those situations can usually be addressed with a little improvising.
To install TestBench with Rubygems:
> gem install test_bench
Or, to add it to a project’s Gemfile
if using Bundler:
gem 'test_bench'
Next, write a test loader, e.g. test/test_init.rb
:
# Begin test/test_init.rb
# Load the code to be tested
require_relative '../lib/my/code.rb'
# Load Test Bench and then activate it
require 'test_bench'; TestBench.activate
# End test/test_init.rb
Then add test files that require the test loader via require_relative
. The typical directory to store test files is test/automated
. For example:
# Begin test/automated/example.rb
require_relative './automated_init'
context "Example Context" do
test "Pass" do
assert(true)
end
test "Assertion failure" do
assert(false)
end
test "Error" do
fail "Some error"
end
end
# End test/automated/example.rb
The above test file can be run directly by ruby:
To run all the tests within e.g. the test/automated
directory, the TestBench runner can be used. Invoke the runner from a script within the project (commonly test/automated.rb
):
# Begin test/automated.rb
require_relative './test_init'
TestBench::Run.('test/automated')
# End test/automated.rb
Now invoke the runner:
After that, the project should be up and running.
Important
|
Documentation for the latest release of TestBench (TestBench 2) is still in progress. For a list of changes from the previous generation (TestBench 1), see Changes From TestBench 1 To TestBench 2. The previous documentation and project can still be found here. |
For a comprehensive list of changes, see Changes
-
Test files and subdirectories found within directories are sorted by the CLI before being loaded.
-
The CLI accepts all of the settings as keyword arguments. Settings supplied to the CLI in this manner will be displayed as the default values by the CLI help text. This allows the CLI settings to be customized on a per-project basis, similar to a per-project configuration file.
-
The CLI::Run class accepts the
exclude_file_pattern
setting as a keyword argument. -
The output methods
enter_assert_block
andexit_assert_block
are now supplied the caller location of the corresponding assertion. -
A bug was fixed in the output implementation that would fail to print outer assertion failures from a block-form assert when the abort on error setting is active.
The changes from TestBench 1 are too numerous to enumerate. See Changes From TestBench 1 To TestBench 2 for an overview of the most significant changes.
Test Bench is licensed under the MIT license.
Copyright © Nathan Ladd