Differences between revisions 12 and 14 (spanning 2 versions)
Revision 12 as of 2013-03-13 16:37:00
Size: 6546
Editor: ?markus tornow
Comment: adding BR between Case [number]: ; to make it more clear
Revision 14 as of 2013-03-18 11:37:11
Size: 6455
Editor: Praveen A
Comment: request upstream to include tests in gem itself
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
Note: temporary content, copied from piratepad (to have the page here and make sure it won't be overlooked) If it is good it will stay.
Line 12: Line 11:
Note: Probably a better place for this is Ruby/Packaging/Tests as it is common for any ruby gem.
Line 150: Line 148:
Note: Request upstream to include tests in future version of the gem itself. See https://github.com/maxwell/rack-piwik/issues/6 for an example.

Translation(s): none

ruby tests

Introduction to software testing

Most of the ruby libraries contain test code which uses the functionality provided by the library and compare results with known inputs. Commonly used testing framework is rspec. Look in your gem to see if there is a 'spec' directory. Other test frameworks may use a 'test' directory.

Running tests

If the gem has tests defined (see if it has 'test or 'spec' directories), make sure you run them successfully. By default gem2deb creates 'debian/ruby-tests.yaml', but it may not work always. If your test or spec directory contains files with extentions other than .rb, this is most certainly going to fail. You can try removing non-ruby files from ruby-test-files.yaml and rebuild it. If it still does not work, try the other two methods described below.

  • You can try ruby-tests.rb, see ruby-warden for a sample file http://ur1.ca/ao6o8 It allows you to add custom directories to ruby library load path and you may need it if your tests fail to find files already present in 'lib' or 'test' directories.

Note: If you don't find ruby-tests.rb in debian directory, you have to create it from the sample file given above. You have to remove ruby-test-files.yaml too.

Uncomment this line in ruby-tests.rb to add custom library paths.

$: << 'lib' << '.'

If it could be done with not much effort, consider instead patching the tests not to override default $LOADPATH (aka $:) variable.

  • You may also try ruby-tests.rake if the gem uses rspec to run the tests (in this case you have a spec/ directory with *_spec.rb files). See ruby-dataobjects-sqlite3 for an example http://ur1.ca/ao6ok


1. Gem provide a test runner (ruby-vegas) - It has test_vegas_runner.rb, so just use it from ruby-tests.rb

 require 'test/test_vegas_runner.rb'

fixing errors

Tips to fixing test errors:

  • Check for build-depencies on rubygems.org: they are listed as development (see the commented line in debian/control produced by gem2deb). You may not need all the build dependencies listed there, so the only way to be sure is to see the build errors. Normally, you need also the dependencies during the build, if you run the tests (since your library will be used).

Case one: a required library is missing

  • If you see a file is missing in the error message, it may be provided by a gem not yet installed in your system. For example if you see

no such file to load -- shoulda (LoadError), 

search for shoulda ie,

$ apt-cache search shoulda

If you find it is already packaged, install it with

# apt-get install ruby-shoulda

Note: Remember to add the build dependency you just found to Build-Depends: in debian/control

  • If you cannot find the package via apt, try if it is a gem file not yet packaged, then run

gem list -r shoulda

If you find it in the remote gem list, then you need to package it first :) So start from step 1 for this gem and come back to your current gem once your package enters debian!

Case two: lib or testor spec is not in library load path

  • If you see spec_helper or test_helper is missing, you may need to add spec or test directory to library load path.

  in `require': no such file to load -- test_helper (LoadError)
  • Uncomment the following line in ruby-tests.rb and modify it accordingly.

$: << 'lib' << '.' 


$: << 'test'  << '.' 

This adds test directory to load path and your tests will be able to find test_helper.

Case three: Argument Error

Example error statement : 'ensure_shared_example_group_name_not_taken'

This happens due to some namespacing error. Refer to this stackoverflow thread for more details.

Change this:

    Dir['{spec}/**/*.rb'].each { |f| require f }


    Dir['{spec}/**/*_spec.rb'].each { |f| require f }

Case four: missing test folder from rubygems org

Error: gem2deb couldn't create a spec or test folder, as it is missing in the gem. At upstream, usually github, it is existent.

Solution: create a directory with the ruby-gem name and make it an empty git repo

mkdir ruby-subexec
cd !$
git init

Download the upstream tarball from github. Add tags to the homepage URL, so you will get a tar.gz file: https://github.com/nulayer/subexec/tags You can download it like usual from there (or wget if you prefer)

import the upstream version to git with

git-import-orig --pristine-tar ~/Downloads/subexec-0.2.2.tar.gz 

You can now copy the complete debian directory gem2deb created for you and then go on like usual:

cp -a path/to/gem2deb_build/ruby-subexec/debian path/to/actual_build/ruby-subexec 

Note: Request upstream to include tests in future version of the gem itself. See https://github.com/maxwell/rack-piwik/issues/6 for an example.

Case five: Tests failure due to interactions

Sometimes, The tests may pass perfectly when they are run separately and fail when we are building it because then the tests are running together and they may interact with each other. To see if the tests run okay individually run the following commands inside the test directory.

RUBY=ruby1.8 ./run_tests.sh
RUBY=ruby1.9.1 ./run_tests.sh

If the tests are passing now, you may want to figure out a way and edit the debian/ruby-tests.rb file by which the tests can run without interacting with each other. One such fix made to the ruby-tests.rb file is as follows.

Dir['test/*_test.rb'].each do |f|
  puts f        
  system("#{ENV['RUBY_TEST_BIN']} -Idebian/ruby-timecop/usr/lib/ruby/vendor_ruby:./test  #{f}") or raise        

The new ruby-tests.rb has a loop: for every test file, call ruby with this test file, and raise an exception if the test fails this way, all the tests are run separately.