如何在geb-spock规范文件

时间:2015-06-25 13:09:37

标签: spock

问题陈述:如何在geb-spock规范文件中的测试方法开始和结束之前自动导入代码。

Geb-spock没有与BeforeMethod或AfterMethod相关的东西,它在测试方法的开始/结束之前执行。此功能由testNG工具提供。因为,Geb-spock在内部使用junit而junit也没有这个功能,Geb-spock也没有这个功能。

但是,我需要使用geb-spock解决问题,我需要在测试方法开始之前和测试方法完成之后导入代码。

如何解决我的问题。

谢谢, Debasish

1 个答案:

答案 0 :(得分:0)

请按照以下代码了解:

import spock.lang.Specification
class SampleSpockSpec extends Specification {

   def setupSpec(){
          println "inside setupSpec method"
   }
   def setup(){
          println "inside setUP method"
   }
   def "playActivity1"(){
          println "Iam activity 1"
          given:
          println "Executing Given Stmt"

          when:
          println "Executing when Stmt"
          then:
          println "Executing Then Stmt"


   }

   def "playActivity2"(){
          println "Iam activity 2"
          given:
          println "Executing Given Stmt"

          when:
          println "Executing when Stmt"
          then:
          println "Executing Then Stmt"
   }
   def "playActivity3"(){
          println "Iam activity 3"
          given:
          println "Executing Given Stmt"

          when:
          println "Executing when Stmt"
          then:
          println "Executing Then Stmt"
   }
   def cleanup(){
          println "inside Clean up method"
   }
   def cleanupSpec(){
          println "inside cleanupSpec method"
   }

}

**Result:**

inside setupSpec method
inside setUP method
Iam activity 1
Executing Given Stmt
Executing when Stmt
Executing Then Stmt
inside Clean up method
inside setUP method
Iam activity 2
Executing Given Stmt
Executing when Stmt
Executing Then Stmt
inside Clean up method
inside setUP method
Iam activity 3
Executing Given Stmt
Executing when Stmt
Executing Then Stmt
inside Clean up method
inside cleanupSpec method