Question

I'm writing a gem and I'd like to write lots of tests for it, as is the norm. I've followed a couple of guides and arrived at the following:

Rakefile

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |t|
  t.libs << 'test'
end

desc "Run tests"
task :default => :test

test/settings_test.rb

require "test/unit"
require "cohabit"

class SettingsTest < Test::Unit::TestCase

  def test_thing
    assert(false)
  end

end

With the following project structure:

- cohabit
  - lib
    - cohabit
      - ...
    cohabit.rb
  - test
    settings_test.rb

I get the following when I run rake test --trace:

mbp4:cohabit mike.campbell$ rake test --trace
** Invoke test (first_time)
** Execute test
mbp4:cohabit mike.campbell$

and rake -T:

mbp4:cohabit mike.campbell$ rake -T
rake build    # Build cohabit-0.0.1.gem into the pkg directory.
rake default  # Run tests
rake install  # Build and install cohabit-0.0.1.gem into system gems.
rake release  # Create tag v0.0.1 and build and push cohabit-0.0.1.gem to R...
rake test     # Run tests

Any ideas why my test doesn't seem to be getting run? I'm sure I'm probably missing something stupid.

Edit - changed Rakefile and output is now:

mbp4:cohabit mike.campbell$ rake test --trace
** Invoke test (first_time)
** Execute test
/Users/mike.campbell/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -I"lib:test" -I"/Users/mike.campbell/.rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib" "/Users/mike.campbell/.rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/rake_test_loader.rb" 
Was it helpful?

Solution

try this out:-

in your test Rakefile update your code to this

require 'rake/testtask'

Rake::TestTask.new do |t|
  t.libs << "test"
  t.test_files = FileList['test/*_test.rb']
  t.verbose = true
end

desc "Run tests"
task :default => :test

i have myself implemented and tested this in my own gem

https://github.com/sachin87/week-of-month/blob/master/Rakefile

for adding more parameters to rake task, read it

http://rake.rubyforge.org/classes/Rake/TestTask.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top