簡體   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