我有两个单元测试,它们应该共享许多常见的测试,但设置方法略有不同。如果我写类似的东西
class Abstract < Test::Unit::TestCase
def setup
@field = create
end
def test_1
...
end
end
class Concrete1 < Abstract
def create
SomeClass1.new
end
end
class Concrete2 < Abstract
def create
SomeClass2.new
end
end
那么Concrete1似乎没有继承Abstract的测试。或者至少我无法让它们在日食中运行。如果我为包含 Concrete1 的文件选择“运行所有测试用例”,则即使我不希望它运行,也会运行 Abstract。如果我指定 Concrete1 那么它根本不运行任何测试!如果我在 Concrete1 中指定 test_1 ,那么它会抱怨找不到它(“uncaught throw :invalid_test (ArgumentError)”)。
我是红宝石新手。我在这里缺少什么?
问题是,据我所知,Test::Unit
跟踪继承自哪些类Test::Unit::TestCase
,结果将only从类中运行测试directly继承它。
解决这个问题的方法是创建一个包含所需测试的模块,然后include派生类中的该模块Test::Unit::TestCase
.
require 'test/unit'
module TestsToInclude
def test_name
assert(self.class.name.start_with?("Concrete"))
end
end
class Concrete1 < Test::Unit::TestCase
include TestsToInclude
def test_something_bad
assert(false)
end
end
class Concrete2 < Test::Unit::TestCase
include TestsToInclude
def test_something_good
assert(true)
end
end
Output:
Loaded suite a
Started
.F..
Finished in 0.027873 seconds.
1) Failure:
test_something_bad(Concrete1) [a.rb:13]:
<false> is not true.
4 tests, 4 assertions, 1 failures, 0 errors
shell returned 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)