Reputation: 6299
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 ?
Upvotes: 4
Views: 1596
Reputation: 7482
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
Upvotes: 1
Reputation: 7482
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.
Upvotes: 0