如何在测试单元中计算断言计数

How assertion counts are calculated in test unit

方法一:-

test.rb

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

结果:- 在 38.329532529 秒内完成。

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

方法二:-

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

结果:-

Finished in 38.329532529 seconds.

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

方法三:-

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

结果:-

Finished in 38.329532529 seconds.

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

使用方法 3 时,我得到的断言计数为“0”而不是“2”。

我可以使用方法 2 将断言计数设为 2 吗?

抱歉,我无法重现您的情况,能否请您提供 Test::Unit 版本和您的 ruby 版本?最好的是带有 Gemfile.lock 的 Gemfile。以下设置适用于我(我使用 ruby 2.2.0 和测试单元 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

运行 testrb 给出了 2 个断言,正如预期的那样。

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:这实际上很奇怪你没有得到任何错误(在你的方法 3 上),因为我得到这个:NoMethodError: undefined method 'assert_true' for AssertionHelper:Module 这是真的,因为 AssertionHelper 没有实现任何其他方法,所以你不能 运行 任何 assert_* 方法。只需使用我上面的代码(你的方法 2),你应该没问题。如果您仍然对可以做什么感到好奇,请查看 Test::Unit::Assertions,其中还定义了很多内置断言,也许您会觉得有用。

或者,更好的是,使用 MiniTest 或 RSpec,因为 Test::Unit is deprecated 并且仅保留在标准库中用于遗留测试套件。

您可以将当前 TestCase 传递给您的模块,如下所示:

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