繁体   English   中英

在其他测试中重用测试类

[英]Reuse of test-Classes in other tests

我想做一些对彼此具有特定依赖性的测试。 因此,我有一个“主要” - 测试,应该调用其他测试。 这是两个示例类:

@Stepwise
public class TestClass extends GebReportingSpec{
NotAutomaticExecutedIT test = new NotAutomaticExecutedIT();

 def "anderen Test aufrufen"() {
    given:
        test."test"()
    when:
        def wert = true
    then:
        wert == true

 }

}

@Ignore
public class NotAutomaticExecutedIT extends GebReportingSpec {

 def "test"() {
    given:
        def trueness = true;
    when:
        def argument = true;
    then:
        argument != trueness;
 }
}

如果我运行测试,我会得到以下异常:

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)

这样做不可能吗?

该错误是因为您正在调用非静态的字段test ,并且未使用@Shared注释。 即使您添加了@Shared注释,我也不能100%确定您尝试做的事情会有效。

我要做的是将常用测试逻辑移动到辅助函数。 这些函数在/然后阻塞时不使用spock,而只是使用assert 将这些辅助函数放入超类中,并使用它的所有规范扩展该类。 然后你可以这样做:

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
  }
}

和基本规格:

public class BaseSpec extends GebReportingSpec{
    def helperFunction(x){
      assert x == some condition
      true
    }
}

通过此设置,您应该能够在多个测试中使用通用逻辑。

编辑:助手应该使用assert而不是为失败返回false,以便保持spock的花哨错误报告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM