TestBelt¶ ↑
Description¶ ↑
This is my Ruby testing “tool belt”. It packages up the most common testing tools and paradigms I use. It is opinionated and custom to how I like to test.
Installation¶ ↑
gem install test-belt
TestBelt only impacts your testing environment or setup and is designed only for testing scenarios. Here are a few ways of bringing it in to your tool:
-
As a development dependency for a gem:
Gem::Specification.new do |s| # your spec stuff ... s.add_development_dependency("test-belt") end
-
As a Gemfile test dependency:
source 'http://rubygems.org' # other gem dependencies ... group :test do gem 'test-belt' # other testing gem dependencies ... end
Test Helpers¶ ↑
Requiring test belt loads in the testing libraries I like and use. First make sure all your tests are in a ‘test’ directory. Then in your test files require in test belt:
require 'test_belt'
This does a few things for you:
-
adds your ‘lib’ and ‘test’ dirs to the $LOAD_PATH
-
requires in a ‘test/helper’ file if one exists
In addition, it gives you the following tools. I use most of these helpers in testing this gem. To see examples of these in action, peruse this gem’s test files (github.com/kelredd/test-belt/tree/master/test) and run the test suite with:
$ rake test
Test::Unit¶ ↑
just write Test::Unit tests with assertions
Leftright¶ ↑
Inheritence based contexts¶ ↑
-
naming
-
subjects
-
callbacks (test, case, and suite)
Custom matchers for repeatable macro-tests¶ ↑
-
have_class_methods
-
have_instance_methods
-
have_readers
-
have_writers
-
have_accessors
-
have_files
-
base class for writing your own
Test::Unit ‘skip’ directive¶ ↑
Test::Unit already provides a ‘flunk’ directive that will fail the current test. I’ve added a ‘skip’ directive that will skip the test when running the suite. I use it to ignore certain tests while I focus on others. Skipping while using LeftRight allows me to ignore the test but not forget about it when looking at my test results.
class TestCaseTest < Test::Unit::TestCase include TestBelt context "Something" should "be something awesome" do skip # anything after the 'skip' directive will not be executed # => use skip(false) to not halt execution assert something_is_in_fact_awesome end end
Generated Rake tasks¶ ↑
For running tests¶ ↑
TestBelt can provide an automatic set of rake tasks for testing subsets of your code base. These tasks are defined based on the structure of your test files. To use this first add this to your Rakefile:
require 'rubygems' require 'test_belt/rake_tasks' TestBelt::RakeTasks.for :test # this assumes your test files are located in /test
To see what this gives you:
$ rake -T
IRB with your environment loaded¶ ↑
Many times in developing code, I need to quickly load up IRB with my environment. TestBelt will give you a rake task for doing that. Simply create an irb.rb file in your test file directory and have it require ‘test_belt’. See github.com/kelredd/test-belt/tree/master/test/irb.rb and demo for this gem with:
$ rake irb > TestBelt => TestBelt >