[英]Reuse of test-Classes in other tests
I want to make a few Tests which have a specific dependecy on each other. 我想做一些对彼此具有特定依赖性的测试。 Because of this, I have one "main"-Test, which should call the 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.GebSpec.methodMissing(GebSpec.groovy:54) at org.gkl.kms.webapp.tests.BestellungenIT.anderen Test aufrufen(TestClass.groovy:16)
groovy.lang.MissingFieldException:没有这样的字段:$ spock_sharedField__browser for class:org.codehaus.groovy.runtime.NullObject at geb.spock.GebSpec.getBrowser(GebSpec.groovy:40)at geb.spock.GebSpec.methodMissing(GebSpec。 groovy:54)at org.gkl.kms.webapp.tests.BestellungenIT.anderen Test aufrufen(TestClass.groovy:16)
Isn't it possible to do this? 这样做不可能吗?
The error is because you are calling a field test
which is not static, and is not annotated with @Shared
. 该错误是因为您正在调用非静态的字段
test
,并且未使用@Shared
注释。 I'm not 100% sure what you're trying to do will work even if you add the @Shared
annotation. 即使您添加了
@Shared
注释,我也不能100%确定您尝试做的事情会有效。
What I would do is move the common test logic to helper functions. 我要做的是将常用测试逻辑移动到辅助函数。 These are functions which do not use the spock when/then blocks, but instead simply use
assert
. 这些函数在/然后阻塞时不使用spock,而只是使用
assert
。 Put these helper functions into a super class and have all Specifications which 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 the base spec: 和基本规格:
public class BaseSpec extends GebReportingSpec{
def helperFunction(x){
assert x == some condition
true
}
}
With this setup you should be able to use common logic in multiple tests. 通过此设置,您应该能够在多个测试中使用通用逻辑。
EDIT: The helpers should use assert instead of returning false for failure, in order to keep spock's fancy error reporting. 编辑:助手应该使用assert而不是为失败返回false,以便保持spock的花哨错误报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.