假设我在一个抽象类中有几个测试,这些测试打算在一个特征的多个实现上运行:
trait Foo {
def method1
def method2
// etc.
}
class Foo1 extends Foo { ... }
class Foo2 extends Foo { ... }
class Foo3 extends Foo { ... }
abstract class FooTest extends FunSuite with Matchers {
val foo: Foo
test("method1") { foo.method1 should equal(...) }
test("method2") { ... }
}
class Foo1Tests extends FooTests { val foo = new Foo1 }
...
不幸的是,有一些测试没有通过 Foo2
,还有一些测试没有通过 Foo3
。将它们视为 Foo
的规范,其中不完整的实现可能已经有用。我想要一种方法将一些继承的测试标记为待处理(或完全隐藏它们)。
根据子类测试的作用将 FooTests
分解为特征并不是解决方案:将来可能会添加新的子类,包括在外部项目中。
使用 JUnitSuite
会有所帮助,因为测试只是方法,可以正常覆盖,但这样做可能为时已晚。
最佳答案
您放入抽象类主体中的任何代码都会被执行,就是这样。如果您希望能够排除某些测试,则需要自定义解决方案。其中之一是根据名称排除测试:
abstract class FooTest extends FunSuite with Matchers {
def excluded: Seq[String] = Seq.empty
override def test(testName: String, testTags: Tag*)(testFun: => Unit) =
if (excluded.contains(testName)) ()
else super.test(testName, testTags: _*)(testFun)
test("method1") { ... }
test("method2") { ... }
然后在子类中你可以排除一些测试如下:
class Foo1Test extends FooTest {
override def excluded = Seq("method2")
}
您当然可以想出更多的语法糖,但我认为如果它只是为了隐藏测试直到它们被实现,那么我认为不值得花更多的时间。
另请注意,使用这种方法可以完全隐藏测试,但您也可以标记它们、标记它们,或者在调用 test
之前做任何您想做的事情。
关于scala - 覆盖或隐藏从父套件继承的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32846389/