Reusing test classes in other tests
I want to do some tests that have a certain dependency on each other. Because of this, I have one "main" test that should call other tests. Here are two example classes:
@Stepwise
public class TestClass extends GebReportingSpec{
NotAutomaticExecutedIT test = new NotAutomaticExecutedIT();
def "anderen Test aufrufen"() {
given:
test."test"()
when:
def wert = true
then:
wert == true
}
}
and
@Ignore
public class NotAutomaticExecutedIT extends GebReportingSpec {
def "test"() {
given:
def trueness = true;
when:
def argument = true;
then:
argument != trueness;
}
}
If I run the test, I get the following exception:
groovy.lang.MissingFieldException: No such field: $ spock_sharedField__browser for class: org.codehaus.groovy.runtime.NullObject at geb.spock.GebSpec.getBrowser (GebSpec.groovy: 40) at geb.spock.GebingSpec.methodS groovy: 54) at org.gkl.kms.webapp.tests.BestellungenIT.anden Test aufrufen (TestClass.groovy: 16)
Can't you do it?
source to share
The error is because you are calling a field test
that is not static and is not annotated with @Shared
. I'm not 100% sure what you are trying to do will work even if you add the annotation @Shared
.
What I would do is translate the general test logic into helper functions. These are functions that do not spock when / then blocks, but instead just use assert
. Put these helper functions in a superclass and any specifications that will use it extend that class. Then you can do something like:
public class AutomaticSpec extends BaseSpec{
def "This is a test"(){
when:
def x = some value
then:
super.helperFunction(x) //super not needed here, just for clarity
}
}
and basic specification:
public class BaseSpec extends GebReportingSpec{
def helperFunction(x){
assert x == some condition
true
}
}
With this setup, you will be able to use common logic across multiple tests.
EDIT: helpers should use assert instead of returning false for failure, in order to keep the error reported with <
...
source to share