I am presently writing REST API tests using REST Client and Spock. I want to be able to run my tests on the different test environments. My test data varies between test environment, so need to use and specify different input data per environment. For a sample test below
class MathSpec extends Specification {
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a | b || c
1 | 3 || 3
}
}
Am I able to specify different data tables for each environment?
class MathSpec extends Specification {
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
@TEST
a | b || c
1 | 3 || 3
@PROD
a | b || c
4 | 5 || 6
}
}
If not, what's the best way to approach this problem? Thanks.
The easiest way that comes to my mind to achieve your expectation is to prepare the data before running test methods based on some condition (e.g. execution environment) and then use this predefined data in your tests. Consider following example:
In this example we prepare shared
datathat holds values we will use in tests. Here we are expecting that the execution environment information will be passed asproperty (alternatively you can pass the same information using env variables). In this example we also use the default value if given property is missing -
testin this case (it will prevent test from failing if you forgot specifying your execution environment variable).Alternative:
@IgnoreIfconditional exectionSpock supports conditional execution. Take a look what the same test would look like if we use
@IgnoreIfapproach:Unfortunately, this approach requires a lot of duplication: you have to duplicate test methods and you can't re-use the same name (compiler does not allow to do that). It's pretty error-prone - you have to pay attention to put the same test body to all methods that should test the same stuff, but with different data. Another thing is that condition passed to
@IgnoreIfwill have to be modified if you introduce 3rd environment - in this case you will specify it something like that:I guess you see how much problematic it starts to be.