On 2/15/07, Daniel Berger <djberg96 / gmail.com> wrote:
> Hi all,
>
> What's the general approach folks use for skipping tests? Sometimes I
> have some tests that I want to skip based on platform (usually MS
> Windows). I saw the 'flunk' method, but that's considered a failed
> test. I'm looking for something that doesn't treat it as success or
> failure.
>
> I guess I'd like something like this:
>
> class TC_Foo < Test::Unit::TestCase
>    def test_bar
>       if RUBY_PLATFORM.match('mswin')
>           skip('There's no Foo::Bar on MS Windows - skipped')
>       else
>           assert_equal(42, Foo::Bar)
>       end
>    end
> end
>
> And then output that looked like this:
>
> 1 tests, 0 assertions, 0 failures, 0 errors, 1 skipped
>
> If there isn't anything like this currently, would patches to add this
> be accepted? I'd be happy to work on it.
>
> Thanks,
>
> Dan
>
> PS - Yes, I realize I could wrap the whole test_bar method in an 'if'
> clause. I don't want to do that. I want to be explicit.
>

Here's a little something I've used...the reason was we had shared
tests across projects where certain test cases shouldn't be run for
one project or another.  Using this was easier then ripping everything
out of svn:externals and syncing manually...

This was used w/i rails, but should work fine with normal Test::Unit.
Just "include Bypass" in any test case you want to be skipped.  The
code could probably be cleaned up - I hacked around with different
methods of doing this as some ways would work within autotest but not
rake, other ways vice versa.  This works in both IIRC.

- Rob


  module Bypass

    def self.logger; RAILS_DEFAULT_LOGGER; end

    def self.included(mod)
      mod.extend ClassMethods
      mod.class_eval do
        define_method(:setup) {}
        define_method(:teardown) {}
        class << self
          define_method(:fixtures) {}
        end
      end
    end

    # provide an empty test to avoid annoying errors
    def test_dummy
      assert true
    end

    module ClassMethods
      # callback when methods are added
      def method_added(meth)
        undef_test_methods(meth)
        remove_setup_and_teardown(meth)
      end

      # undefine any test methods
      def undef_test_methods(meth)
        if meth.to_s.starts_with?("test")
          undef_method meth
        end
      end

      # remove setup and teardown
      def remove_setup_and_teardown(meth)
        if meth == :setup || meth == :teardown
          remove_method meth
        end
      end
    end

  end