Rails tests showing 0% passed when all pass
Asked Answered
M

3

8

My Rails app passes its test but then says 0% passed. Its a Rails 3 app converted to 3.1.

5 tests, 11 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

If I generate a new Rails 3.1 app it doesnt have this "passed" line.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Both are running under 1.9.2 p290.

I tried removing test-unit but that did not seem to make a difference and nothing else stands out to me that would cause it. I guess something is triggering the use of a different test runner.

These are the gems installed for my app:

actionmailer (3.1.1)
actionpack (3.1.1)
activemodel (3.1.1)
activerecord (3.1.1)
activeresource (3.1.1)
activesupport (3.1.1)
ansi (1.3.0)
archive-tar-minitar (0.5.2)
arel (2.2.1)
bcrypt-ruby (3.0.1)
bootstrap-sass (1.3.0)
builder (3.0.0)
bundler (1.0.21)
coderay (0.9.8)
coffee-rails (3.1.1)
coffee-script (2.2.0)
coffee-script-source (1.1.2)
columnize (0.3.4)
devise (1.4.7)
devise_rpx_connectable (0.2.2)
enumerated_attribute (0.2.16)
erubis (2.7.0)
execjs (1.2.9)
haml (3.1.3)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.14)
json (1.6.1)
json_pure (1.6.1)
linecache19 (0.5.12)
mail (2.3.0)
meta_programming (0.2.2)
method_source (0.6.6)
mime-types (1.16)
multi_json (1.0.3)
orm_adapter (0.0.5)
polyglot (0.3.2)
pry (0.9.6.2)
pry-doc (0.3.0)
rack (1.3.4)
rack-cache (1.1)
rack-mount (0.8.3)
rack-ssl (1.3.2)
rack-test (0.6.1)
rack-webconsole (0.1.2)
rails (3.1.1)
railties (3.1.1)
rake (0.9.2)
rdoc (3.10)
rpx_now (0.6.24)
ruby-debug-base19 (0.11.25)
ruby-debug19 (0.11.6)
ruby_core_source (0.1.5)
ruby_parser (2.0.6)
sass (3.1.10)
sass-rails (3.1.4)
sexp_processor (3.0.7)
slop (2.1.0)
sprockets (2.0.2)
sqlite3 (1.3.4)
test-unit (2.4.0)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
turn (0.8.2)
tzinfo (0.3.30)
uglifier (1.0.3)
warden (1.0.6)
yard (0.7.2)

And this is what I get with the default rails new'd app

actionmailer (3.1.1)
actionpack (3.1.1)
activemodel (3.1.1)
activerecord (3.1.1)
activeresource (3.1.1)
activesupport (3.1.1)
ansi (1.3.0)
arel (2.2.1)
builder (3.0.0)
bundler (1.0.21)
coffee-rails (3.1.1)
coffee-script (2.2.0)
coffee-script-source (1.1.2)
erubis (2.7.0)
execjs (1.2.9)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.14)
json (1.6.1)
mail (2.3.0)
mime-types (1.16)
multi_json (1.0.3)
polyglot (0.3.2)
rack (1.3.4)
rack-cache (1.1)
rack-mount (0.8.3)
rack-ssl (1.3.2)
rack-test (0.6.1)
rails (3.1.1)
railties (3.1.1)
rake (0.9.2)
rdoc (3.10)
sass (3.1.10)
sass-rails (3.1.4)
sprockets (2.0.2)
sqlite3 (1.3.4)
test-unit (2.4.0)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
turn (0.8.2)
tzinfo (0.3.30)
uglifier (1.0.3)

EDIT

I have cut out everything from the app and pushed it to github - https://github.com/kimptoc/zero-percent-bugette When I just run "rake", I still get 0% passed:

$ rake
Loaded suite /Users/kimptoc/.rvm/gems/ruby-1.9.2-p290@p-viadropbox31/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started

Finished in 0.000897 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

1114.83 tests/s, 1114.83 assertions/s
Coverage report generated for /Users/kimptoc/.rvm/gems/ruby-1.9.2-p290@p-viadropbox31/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb  to    /Users/kimptoc/dev/ruby/fran/zero-percent/coverage. 4 / 4 LOC (100.0%) covered.
Melbourne answered 9/10, 2011 at 20:54 Comment(3)
Do you've any complete project one can take a look at?Requiescat
Hi Oleander - let me see what I can knock upMelbourne
Push it to Github when you're done.Requiescat
B
10

If you are using test-unit with Rails, then you cannot do anything. I believe it is a bug in activesupport gem that comes with Rails. In more detail:

Normally, test-unit has a method add_pass that increases the count of tests that pass. This is called inside run(...) method (testcase.rb file). However, activesupport hides/overrides this method with its own implementation. It is method run(...) inside file setup_and_teardown.rb (The full name of method is ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit#run().

I have modified this method as follows:

    def run(result)
      return if @method_name.to_s == "default_test"

      mocha_counter = retrieve_mocha_counter(result)
      yield(Test::Unit::TestCase::STARTED, name)
      @_result = result

      begin
        begin
          _run_setup_callbacks do
            setup
            __send__(@method_name)
            mocha_verify(mocha_counter) if mocha_counter
          end
        result.add_pass # XXXXXXXXXXXX My Addition to count passed tests XXXXXXXXXX
        rescue Mocha::ExpectationError => e
          add_failure(e.message, e.backtrace)
        rescue Test::Unit::AssertionFailedError => e
          add_failure(e.message, e.backtrace)
        rescue Exception => e
          raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
          add_error(e)
        ensure
          begin
            teardown
            _run_teardown_callbacks
          rescue Test::Unit::AssertionFailedError => e
            add_failure(e.message, e.backtrace)
          rescue Exception => e
            raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
            add_error(e)
          end
        end
      ensure
        mocha_teardown if mocha_counter
      end

      result.add_run
      yield(Test::Unit::TestCase::FINISHED, name)
    end

I have added result.add_pass which was not there, and now percentages should be ok.

UPDATE: To help you out integrating this change and test/see for yourself, you can clone a gist that I have created using the following command:

git clone git://gist.github.com/1324512.git gist-1324512

This will create a file with name : active_support_test_unit_pass_count.rb

Move that to your test folder where test_helper.rb file is and require it in your test_helper.rb file, exactly before the definition of the ActiveSupport::TestCase class:

require 'active_support_test_unit_pass_count'

class ActiveSupport::TestCase

....

Then run your tests and see the percentage result. To me...it worked ok.

Belligerent answered 29/10, 2011 at 14:29 Comment(5)
Thanks - sounds good, although it doesnt seem to have helped... trying a few tweaks to see if I can get it going but no joy - github.com/kimptoc/zero-percent-bugetteMelbourne
@ChrisKimpton My friend, I should have said exactly before the definition of the ActiveSupport::TestCase class. Move the line require 'active_support_test_unit_pass_count' exactly before. I cloned your repository. I saw that it didn't work (as you said), then moved the line exactly before the definition of the class, and it ran, this time, with success. Try for yourself and you will see. (I have also edited my answer to reflect thi)Belligerent
That did it - thanks. Seems like a Rails/activesupport bugette - will raise it on the rails issue list.Melbourne
FWIW - Oh- it seems its been raised already - github.com/rails/rails/pull/2472Melbourne
Thanks! Also, if you're having problems including the file use this line: require File.expand_path('../../test/active_support_test_unit_pass_count', FILE)Astereognosis
M
2

I ran across this on an old Rails 3.2.22 app. I found an easier solution than above (above gave me other errors as it makes assumptions about versions etc). I added test-unit-rails gem to my gemfile, for me I had to lock it to version 1.0.4 as it supported the version of test-unit I was previously using.

In my test_helper.rb file I added:

require "test/unit/active_support"
require 'active_support'

Here's the docs I found it from. Specifically, it's an active-support plugin for test-unit.

Marlie answered 10/11, 2017 at 21:11 Comment(0)
E
1

Try removing test unit and installing mini-test. In your Gemfile:

gem "minitest"
Enantiomorph answered 25/10, 2011 at 18:32 Comment(5)
Thanks - but no difference, still says 0% passed, yet all green. I thought minitest was in 1.9, but I guess there is more to is...Melbourne
Have you tried it without ruby-debug19 in your gemfile? Just stabbing in the dark, at this point.Enantiomorph
LOL - same here, just tried uninstalling that too- no better :( .Melbourne
minitest solved this for me, but only after I removed test-unit.Meisel
For me the problem was that I was using guard-test instead of guard-minitest. The guard-test gem was dependent on the old test-unit. Along these lines, you might just want to check your Gemfile.lock to see if Bundler is requiring a different version of test/unit because of one of your gems. Another way to test your gem dependencies is to run this: ruby -rbundler/setup -rtest/unit -e "puts ::Test::Unit::TEST_UNIT_IMPLEMENTATION". If it says uninitialized constant Test::Unit::TEST_UNIT_IMPLEMENTATION (NameError), then you're not using minitest.Laceration

© 2022 - 2024 — McMap. All rights reserved.