run Ruby Unit Tests with Buildr

97 Views Asked by At

I am using buildr for some time now, but today I came over a little problem in connection to unit testing of ruby code.

So in my buildfile I have these lines:

define "ruby-project" do

    project.version = VERSION_NUMBER
    project.group = GROUP

    Rake::TestTask.new(:test_rb) do |t|
        t.warning = true
        t.verbose = true
        t.test_files = FileList['test/*.rb']
    end
    task test => [:test_rb]

end

running buildr test actually runs the tests, what is nice. The test is actually just that:

require 'test/unit'

class TestFileParse < Test::Unit::TestCase
    def test_fail
        assert(false, 'test to fail')
     end
end

As expected it fails, BUT what is strange for me is that buildr quits the build with that message:

sh: 2: Syntax error: Unterminated quoted string
Buildr aborted!
RuntimeError : Command failed with status (1): [/usr/bin/ruby1.9.1 -w -I"lib" -I"/var/lib/...]

Running ruby file-with-failing-test-from-above.rp does not throw a runtime error, instead it prints the test report on screen, what is what is what I want.

Question

How can I make Buildr run the unit tests without quitting with an RuntimeError if a test fails?

Greetings Philipp

1

There are 1 best solutions below

0
On

Since I am not too familiar with Ruby development, what involves Rake, I was looking for the wrong question. Instead of looking for: »how to run unit test with buildr«, the question should have been »how to run unit tests with rake«, because buildr is a kind of extended Rake (similar to the »maven-ant-relationship«). So everything one can do in Rake, one can do in buildr, too. Therefore on good SO answer to run ruby unit tests in buildr is here.

Additionally it is possible to run RSpec's with buildr, therefore one has two options:

  1. set project.test.using :rspec, what involves the use of JRuby, so one has to set JRUBY_HOME (in my case ~/.rvm/rubies/jruby-1.7.9/), or run buildr within jruby. In my case this slowed test execution down, because each time a jvm needed to be started.
  2. Or one can use rspec's rake task in this manner. I choose this method since my tests run much faster without the jvm overhead.

n.b. as the answer implies, I switched over to rspec.

Happy Testing/Speccing!