John Duff
John Duff

Reputation: 38588

Rails test hanging - how can I print the test name before execution?

I'm having a test hang in our rails app can't figure out which one (since it hangs and doesn't get to the failure report). I found this blog post http://bmorearty.wordpress.com/2008/06/18/find-tests-more-easily-in-your-testlog/ which adds a setup hook to print the test name but when I try to do the same thing it gives me an error saying wrong number of arguments for setup (1 for 0). Any help at all would be appreciated.

Upvotes: 34

Views: 8409

Answers (6)

oscarw
oscarw

Reputation: 1

In case anyone else has the same problem but with integration tests and a headless web server, the following ruby code will print out a test filename before running it

Dir["test/integration/**/*.rb"].each do |filename|
  if filename.include?("_test.rb")
    p filename
    system "xvfb-run rake test TEST=#{filename}"
  end
end

Upvotes: 0

allenwei
allenwei

Reputation: 4137

If you run test using rake it will work:

rake test:units TESTOPTS="-v" 

Upvotes: 48

Ben Scofield
Ben Scofield

Reputation: 6418

This is what I use, in test_helper.rb

class Test::Unit::TestCase
  # ...

  def setup_with_naming 
    unless @@named[self.class.name]
      puts "\n#{self.class.name} "
      @@named[self.class.name] = true
    end
    setup_without_naming
  end
  alias_method_chain :setup, :naming unless defined? @@aliased
  @@aliased = true  
end

The @@aliased variable keeps it from being re-aliased when you run it on multiple files at once; @@named keeps the name from being displayed before every test (just before the first to run).

Upvotes: 4

Jean
Jean

Reputation: 21605

The definition in the blog post is shoulda specific (the setup(&block) method is defined in the module Thoughtbot::Shoulda in context.rb. shoulda.rb then has TestCase extend that module).

The definition for pure test::unit is

# File test/unit/testcase.rb, line 100
      def setup
      end

what you could do is

def setup
  log_test
end  

private   

def log_test   
  if Rails::logger  
    # When I run tests in rake or autotest I see the same log message multiple times per test for some reason.   
    # This guard prevents that.   
  unless @already_logged_this_test  
    Rails::logger.info "\n\nStarting #{@method_name}\n#{'-' * (9 + @method_name.length)}\n"  
  end  
  @already_logged_this_test = true  
end 

edit

if you really don't want to edit your files you can risk reopenning test case and extend run instead :

class Test::Unit::TestCase   
  alias :old_run :run
  def run 
    log_test
    old_run
  end
end

this should work (I don't have ruby around to test though)


I give up ! (in frustration)

I checked the code and rails actually does magic behind the scene which is probably why redefining run doesn't work.

The thing is : part of the magic is including ActiveSupport::CallBack and creating callbacks for setup and teardown.

which means

class Test::Unit::TestCase
  setup :log_test  

  private   

  def log_test   
    if Rails::logger  
      # When I run tests in rake or autotest I see the same log message multiple times per test for some reason.   
      # This guard prevents that.   
      unless @already_logged_this_test  
        Rails::logger.info "\n\nStarting #{@method_name}\n#{'-' * (9 + @method_name.length)}\n"  
      end  
      @already_logged_this_test = true  
    end  
  end  
end

should definitely work

and actually running tests with it does work. where I am confused is that this is the first thing I tried and it failed with the same error you got

Upvotes: 3

Aaron Hinni
Aaron Hinni

Reputation: 14726

The printing of the test name is the responsibility of the TestRunner. If you are running your tests from the command line you can specify the -v option, to print out the test case names.

example:

ruby test_Foo.rb -v
Loaded suite test_Foo
Started
test_blah(TestFoo): .
test_blee(TestFoo): .

Finished in 0.007 seconds.

2 tests, 15 assertions, 0 failures, 0 errors

Upvotes: 6

mwilliams
mwilliams

Reputation: 9978

What test framework are you using? Test/Unit?

I would take a look at RSpec which would provide a little more context for your tests. You can also get a nice HTML report that looks like this:

RSpec Result http://myskitch.com/robbyrussell/rspec_results-20070801-233809.jpg

Had you had any failing tests, the specific test would be red and it would expand to the specific lines where the test failed to provide you more visibility on why the test failed and where you should look to address the issue.

Upvotes: -2

Related Questions