还有一种替代方案(也许以前不存在):
在@Requires
或@IgnoreIf
中使用instance
:
以下是继承示例,但不是必需的:
abstract class BaseTest extends Specification {
abstract boolean serviceIsOnline()
@Requires({ instance.serviceIsOnline() })
def "some test" () { .. }
}
子规范:
class OnlineTest extends BaseTest {
boolean serviceIsOnline() {
// Test connection, etc.
return true
}
}
class SkipTest extends BaseTest {
boolean serviceIsOnline() {
return false
}
}
实例
如果需要实例字段、共享字段或实例方法,则使用规范实例。如果使用此属性,则无法直接跳过整个注释元素而不执行夹具、数据提供程序等。相反,整个工作流程将一直持续到特征方法调用,在那里检查闭包,并决定是否中止特定迭代。
作为额外的方式,您可以使用 where
标签以编程方式跳过测试:
class MyTest extends Specification {
List getAvailableServices() {
// You can test connections here or your conditions
// to enable testing or not.
return available
}
@Unroll
def "Testing something"() {
setup:
URL url = serviceUrl.toURL()
expect:
assert url.text.contains("Hello")
where:
serviceUrl << availableServices
}
}
@IgnoreIf
中运行任意代码。你试过吗?我知道这是一个旧问题,但是你到底有什么问题?如果你已经了解自己的Spock内置功能可以实现相同的功能,那么我认为没有必要使用Assume
。 - kriegaex@Stepwise
,以便使Spock自动跳过该规范中剩余的方法。 另外,如果您一定要这样做,@Requires
或@IgnoreIf
可以轻松调用测试中的静态方法,检查或记录到远程系统中。 - kriegaex