4

Method 1:-

test.rb

class Test < Test::Unit::TestCase
  def test_sample
    assert_true(test)
    assert_equal(a,b)
  end
end

Result:- Finished in 38.329532529 seconds.

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

Method 2:-

test.rb

class Test <  Test::Unit::TestCase
require 'helper'
include AssertionHelper
  def test_sample
    test_assertion
  end
end

helper.rb

include Test::Unit::Assertions
module AssertionHelper
  def test_assertion
    assert_true(test)
    assert_equal(a,b)
  end
end

Result:-

Finished in 38.329532529 seconds.

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

Method 3:-

test.rb

class Test <  Test::Unit::TestCase
require 'helper'
  def test_sample
    AssertionHelper.test_assertion()
  end
end

helper.rb

 include Test::Unit::Assertions
    module AssertionHelper
      def self.test_assertion
        assert_true(test)
        assert_equal(a,b)
      end
    end

Result:-

Finished in 38.329532529 seconds.

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

When using Method 3, I am getting assertion count as "0" instead of "2".

Is it possible for me to get assertion count as 2 using Method 2 ?

Galet
  • 5,853
  • 21
  • 82
  • 148
  • why to wrap assertion into a separate module? – Anatoly Sep 07 '15 at 06:54
  • @Anatoly I have common assertion statements to be verified for more test files. So i have put common assertions in one helper file and calling that from test files. – Galet Sep 07 '15 at 07:01
  • test_helper.rb is for common functions but **assertions** have to be within tests – Anatoly Sep 07 '15 at 07:03
  • @Anatoly Yes I understand. I don't want to repeat the assertion statements in all test fails. Is there way to get assertion count from helper files. – Galet Sep 07 '15 at 07:12
  • Great question. Would love to see good answer for it. – dimakura Sep 07 '15 at 07:27

2 Answers2

1

You can pass your current TestCase to your module, like this:

sample_test.rb:

require 'test-unit'
require 'helper'

def a; true ; end
def b; true ; end
def test; true ; end

class SampleTest < Test::Unit::TestCase
    def test_sample
        AssertionHelper.my_assertion(self)
    end
end

helper.rb:

module AssertionHelper   
    def self.my_assertion(test_case)
      test_case.instance_exec do
        assert_true(test)
        assert_equal(a, b)
      end
    end
end
Alexey Shein
  • 7,342
  • 1
  • 25
  • 38
0

Sorry, but I can't reproduce your situation, could you please provide Test::Unit version and your ruby version? Best of all would be your Gemfile with Gemfile.lock. The following setup works for me (I use ruby 2.2.0 and test-unit 3.0.8):

ruby-2.2.0 in ~/projects/test-unit ♥ tree
.
├── Gemfile
├── Gemfile.lock
└── test
    ├── helper.rb
    └── sample_test.rb

1 directory, 4 files

ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile
# A sample Gemfile
source "https://rubygems.org"

# gem "rails"
gem 'test-unit', '~> 3.0.8'
ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    power_assert (0.2.2)
    test-unit (3.0.8)
      power_assert

PLATFORMS
  ruby

DEPENDENCIES
  test-unit (~> 3.0.8)

sample_test.rb:

require 'test-unit'

def a; true ; end
def b; true ; end
def test; true ; end

class SampleTest < Test::Unit::TestCase
    require 'helper'
    include AssertionHelper
    def test_sample
        my_assertion
    end
end

helper.rb:

module AssertionHelper
    def my_assertion
        assert_true(test)
        assert_equal(a, b)
    end
end

Running testrb gives 2 assertions, as expected.

ruby-2.2.0 in ~/projects/test-unit ♥ testrb 
Loaded suite .
Started
.

Finished in 0.000828 seconds.

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

1207.73 tests/s, 2415.46 assertions/s
ruby-2.2.0 in ~/projects/test-unit ♥ 

UPDATE: This is actually strange that you don't get any error (on your method 3), because I get this: NoMethodError: undefined method 'assert_true' for AssertionHelper:Module and this is true, since AssertionHelper doesn't implement any other methods, you can't run any assert_* methods on it. Just use my code above (your method 2) and you should be fine. If you're still curious what can be done, have a look at Test::Unit::Assertions, there's also a lot of built-in assertions defined, maybe you find that useful.

Or, better, use MiniTest or RSpec, since Test::Unit is deprecated and is left in standard library only for legacy test suites.

Alexey Shein
  • 7,342
  • 1
  • 25
  • 38
  • Include "include Test::Unit::Assertions" in helper.rb. Your assert_true error will be solved. – Galet Sep 07 '15 at 09:53
  • You should `extend` it, rather than `include`. Yes, this fixes the error, but that doesn't fix your problem - assertions made in module are still not counted. This happens because `add_assertion` method is empty in `Test::Unit::Assertions`. To make it work you need to pass it an instance of your `Test::Unit::TestCase` so you can call its `add_assertion` method from your module's `add_assertion` to make it count. All in all, it's too cumbersome to implement it, so I prefer my solution above. Why don't you like it? – Alexey Shein Sep 07 '15 at 10:12
  • Oh, now I see. You included `Test::Unit::Assertions` **outside** of `module AssertionHelper`, don't ever do that in your code. All methods of `Test::Unit::Assertions` become included to `Object`, and now they exist in every object in your system, that's bad. You should've done `extend Test::Unit::Assertions` inside of your `AssertionHelper` module, although that doesn't solve problem with assertion counters as I stated above. – Alexey Shein Sep 07 '15 at 10:20
  • Thanks for your answer. What should i do now to get assertion count from helper. Can you give me sample code or edit my code and send it – Galet Sep 07 '15 at 10:52
  • Why don't you want to use my solution above? – Alexey Shein Sep 07 '15 at 11:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88997/discussion-between-shein-alexey-and-karan). – Alexey Shein Sep 07 '15 at 11:32