【发布时间】:2015-09-07 06:47:57
【问题描述】:
方法一:-
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 吗?
【问题讨论】:
-
为什么要将断言包装到一个单独的模块中?
-
@Anatoly 我有共同的断言语句要验证更多的测试文件。所以我把常见的断言放在一个帮助文件中,并从测试文件中调用它。
-
test_helper.rb 用于常用功能,但 assertions 必须在测试中
-
@Anatoly 是的,我明白了。我不想在所有测试失败中重复断言语句。有没有办法从帮助文件中获取断言计数。
-
好问题。很想看到它的好答案。
标签: ruby assertions testunit