Reputation: 31
I need to skip some functions in the program, but it should depend on a variable defined in the same program. How can I do it?
def skip = true
@IgnoreIf({ skip })
def "some function" () {
..
}
Upvotes: 2
Views: 1977
Reputation: 25200
Another way is:
def skip = true
@IgnoreIf({ instance.skip })
def "some function" () {
..
}
instance
The specification instance, if instance fields, shared fields, or instance methods are needed. If this property is used, the whole annotated element cannot be skipped up-front without executing fixtures, data providers and similar. Instead, the whole workflow is followed up to the feature method invocation, where then the closure is checked, and it is decided whether to abort the specific iteration or not.
Then you could do something like:
abstract class BaseTest extends Specification {
boolean skip = false
@IgnoreIf({ instance.skip })
def "some function" () { .. }
}
SubSpecification:
class GoodTest extends BaseTest {
}
class SkipTest extends BaseTest {
boolean skip = true
}
In this case, skip
is a property, but it can be a method.
Upvotes: 0
Reputation: 11
If the variable needs to be computed first and then the decision for test ignoring needs to be taken on the basis of computation, we can use static block and static variable
import spock.lang.IgnoreIf
import spock.lang.Specification
class IgnoreIfSpec extends Specification {
static final boolean skip
static {
//some code for computation
if("some condition")
skip = true
else
skip = false
}
@IgnoreIf({ IgnoreIfSpec.skip })
def "should not execute this test if `IgnoreIfSepc.skip` is set to TRUE"() {
when:
def res = 1 + 1
then:
res == 2
}
def "should execute this test every time"() {
when:
def res = 1 + 1
then:
res == 2
}
}
Upvotes: 1
Reputation: 1685
Another way to do this is by using spock's configuration file to include/exclude tests or base classes.
First you create your own annotations and place on your tests.
You then write a Spock configuration file.
When you run your tests you can set the spock.configuration
property to the configuration file of your choice.
This way you can include/exclude the tests and base classes you want.
Example of spock configuration file:
runner {
println "Run only tests"
exclude envA
}
Your test:
@envA
def "some function" () {
..
}
You can then run :
./gradlew test -Pspock.configuration=mySpockConfig.groovy
Here is a github example
Upvotes: 4
Reputation: 42174
You can do it by accessing skip
field in a static context:
import spock.lang.IgnoreIf
import spock.lang.Specification
class IgnoreIfSpec extends Specification {
static boolean skip = true
@IgnoreIf({ IgnoreIfSpec.skip })
def "should not execute this test if `IgnoreIfSepc.skip` is set to TRUE"() {
when:
def res = 1 + 1
then:
res == 2
}
def "should execute this test every time"() {
when:
def res = 1 + 1
then:
res == 2
}
}
Otherwise closure passed to @IgnoreIf()
tries to find skip
field inside the closure and it fails.
Upvotes: 3