How to invoke a custom method/cleanUp after all tests? (Geb and Spock)










1















To set up the environment before running tests I use the GebConfig.groovy class which is triggered as first component when runnig tests. But I also need to restore the environment to the initial state after all tests are finished.



I tried to overwtire the method cleanUpSpec() in class which extended GebReportingSpec class, but it is invoked after each test.



Is there a way to invoke a method after all tests are completed to clean up the environment to the initial state?



I am using maven in this project.










share|improve this question


























    1















    To set up the environment before running tests I use the GebConfig.groovy class which is triggered as first component when runnig tests. But I also need to restore the environment to the initial state after all tests are finished.



    I tried to overwtire the method cleanUpSpec() in class which extended GebReportingSpec class, but it is invoked after each test.



    Is there a way to invoke a method after all tests are completed to clean up the environment to the initial state?



    I am using maven in this project.










    share|improve this question
























      1












      1








      1








      To set up the environment before running tests I use the GebConfig.groovy class which is triggered as first component when runnig tests. But I also need to restore the environment to the initial state after all tests are finished.



      I tried to overwtire the method cleanUpSpec() in class which extended GebReportingSpec class, but it is invoked after each test.



      Is there a way to invoke a method after all tests are completed to clean up the environment to the initial state?



      I am using maven in this project.










      share|improve this question














      To set up the environment before running tests I use the GebConfig.groovy class which is triggered as first component when runnig tests. But I also need to restore the environment to the initial state after all tests are finished.



      I tried to overwtire the method cleanUpSpec() in class which extended GebReportingSpec class, but it is invoked after each test.



      Is there a way to invoke a method after all tests are completed to clean up the environment to the initial state?



      I am using maven in this project.







      maven testing automated-tests spock geb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 14:16









      JeyKeyJeyKey

      10218




      10218






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I don't know any mechanism in Geb or Spock. Generally my experience is that you will be in a better place when every test sets the stage before it starts instead of relying on some cleanup mechanism, which might not have been executed.



          However, if you really need this, I'd suggest to use something like Maven Exec Plugin with execution phase post-(integration-)test:



          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>
          <executions>
          <execution>
          <id>cleanup</id>
          <phase>post-test</phase>
          <goals>
          <goal>exec</goal>
          </goals>
          </execution>
          </executions>
          <configuration>
          <executable>cleanup.groovy</executable>
          </configuration>
          </plugin>


          You can use any executable file instead of cleanup.groovy but keep in mind that is has to be executable (chmod +x ...).






          share|improve this answer

























          • Thanks for answer. I have some questions: 1. Where this file 'cleanup.groovy' should be stored? On the same level as pom.xml? 2. Should this code you have provided in the answer be placed next to maven-surefire-plugin? 3. I created a file 'cleanup.groovy' with content 'println("All tests have been executed!")' and run one test class, but that line has not been executed. I don't know if I understood you correctly. Will be grateful for help.

            – JeyKey
            Nov 14 '18 at 14:18












          • In this example the cleanup.groovy should indeed be next to pom.xml. However you can configure any path you like. There is no need to put it next to surefire. The connection is made by the configured <phase>. Using <phase>post-test</phase> makes Maven execute right after test phase. See maven.apache.org/guides/introduction/… for more options for phases.

            – Michael
            Nov 14 '18 at 14:20












          • Note that I updated the source. I used the wrong phase clean which will execute during mvn clean.

            – Michael
            Nov 14 '18 at 14:25











          • The problem is that in this project I already had maven-failsafe-plugin and it has <phase>verify</phase>. Here you can see whole plugin ibb.co/e5O7Pf I will try to change it to 'post-test' and see what will happen. Cause I guess it was set to verify for some reason

            – JeyKey
            Nov 14 '18 at 14:27











          • If you want the script be executed after failsafe, you should use <phase>post-verify</phase>.

            – Michael
            Nov 14 '18 at 14:29










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283012%2fhow-to-invoke-a-custom-method-cleanup-after-all-tests-geb-and-spock%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I don't know any mechanism in Geb or Spock. Generally my experience is that you will be in a better place when every test sets the stage before it starts instead of relying on some cleanup mechanism, which might not have been executed.



          However, if you really need this, I'd suggest to use something like Maven Exec Plugin with execution phase post-(integration-)test:



          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>
          <executions>
          <execution>
          <id>cleanup</id>
          <phase>post-test</phase>
          <goals>
          <goal>exec</goal>
          </goals>
          </execution>
          </executions>
          <configuration>
          <executable>cleanup.groovy</executable>
          </configuration>
          </plugin>


          You can use any executable file instead of cleanup.groovy but keep in mind that is has to be executable (chmod +x ...).






          share|improve this answer

























          • Thanks for answer. I have some questions: 1. Where this file 'cleanup.groovy' should be stored? On the same level as pom.xml? 2. Should this code you have provided in the answer be placed next to maven-surefire-plugin? 3. I created a file 'cleanup.groovy' with content 'println("All tests have been executed!")' and run one test class, but that line has not been executed. I don't know if I understood you correctly. Will be grateful for help.

            – JeyKey
            Nov 14 '18 at 14:18












          • In this example the cleanup.groovy should indeed be next to pom.xml. However you can configure any path you like. There is no need to put it next to surefire. The connection is made by the configured <phase>. Using <phase>post-test</phase> makes Maven execute right after test phase. See maven.apache.org/guides/introduction/… for more options for phases.

            – Michael
            Nov 14 '18 at 14:20












          • Note that I updated the source. I used the wrong phase clean which will execute during mvn clean.

            – Michael
            Nov 14 '18 at 14:25











          • The problem is that in this project I already had maven-failsafe-plugin and it has <phase>verify</phase>. Here you can see whole plugin ibb.co/e5O7Pf I will try to change it to 'post-test' and see what will happen. Cause I guess it was set to verify for some reason

            – JeyKey
            Nov 14 '18 at 14:27











          • If you want the script be executed after failsafe, you should use <phase>post-verify</phase>.

            – Michael
            Nov 14 '18 at 14:29















          0














          I don't know any mechanism in Geb or Spock. Generally my experience is that you will be in a better place when every test sets the stage before it starts instead of relying on some cleanup mechanism, which might not have been executed.



          However, if you really need this, I'd suggest to use something like Maven Exec Plugin with execution phase post-(integration-)test:



          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>
          <executions>
          <execution>
          <id>cleanup</id>
          <phase>post-test</phase>
          <goals>
          <goal>exec</goal>
          </goals>
          </execution>
          </executions>
          <configuration>
          <executable>cleanup.groovy</executable>
          </configuration>
          </plugin>


          You can use any executable file instead of cleanup.groovy but keep in mind that is has to be executable (chmod +x ...).






          share|improve this answer

























          • Thanks for answer. I have some questions: 1. Where this file 'cleanup.groovy' should be stored? On the same level as pom.xml? 2. Should this code you have provided in the answer be placed next to maven-surefire-plugin? 3. I created a file 'cleanup.groovy' with content 'println("All tests have been executed!")' and run one test class, but that line has not been executed. I don't know if I understood you correctly. Will be grateful for help.

            – JeyKey
            Nov 14 '18 at 14:18












          • In this example the cleanup.groovy should indeed be next to pom.xml. However you can configure any path you like. There is no need to put it next to surefire. The connection is made by the configured <phase>. Using <phase>post-test</phase> makes Maven execute right after test phase. See maven.apache.org/guides/introduction/… for more options for phases.

            – Michael
            Nov 14 '18 at 14:20












          • Note that I updated the source. I used the wrong phase clean which will execute during mvn clean.

            – Michael
            Nov 14 '18 at 14:25











          • The problem is that in this project I already had maven-failsafe-plugin and it has <phase>verify</phase>. Here you can see whole plugin ibb.co/e5O7Pf I will try to change it to 'post-test' and see what will happen. Cause I guess it was set to verify for some reason

            – JeyKey
            Nov 14 '18 at 14:27











          • If you want the script be executed after failsafe, you should use <phase>post-verify</phase>.

            – Michael
            Nov 14 '18 at 14:29













          0












          0








          0







          I don't know any mechanism in Geb or Spock. Generally my experience is that you will be in a better place when every test sets the stage before it starts instead of relying on some cleanup mechanism, which might not have been executed.



          However, if you really need this, I'd suggest to use something like Maven Exec Plugin with execution phase post-(integration-)test:



          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>
          <executions>
          <execution>
          <id>cleanup</id>
          <phase>post-test</phase>
          <goals>
          <goal>exec</goal>
          </goals>
          </execution>
          </executions>
          <configuration>
          <executable>cleanup.groovy</executable>
          </configuration>
          </plugin>


          You can use any executable file instead of cleanup.groovy but keep in mind that is has to be executable (chmod +x ...).






          share|improve this answer















          I don't know any mechanism in Geb or Spock. Generally my experience is that you will be in a better place when every test sets the stage before it starts instead of relying on some cleanup mechanism, which might not have been executed.



          However, if you really need this, I'd suggest to use something like Maven Exec Plugin with execution phase post-(integration-)test:



          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.2.1</version>
          <executions>
          <execution>
          <id>cleanup</id>
          <phase>post-test</phase>
          <goals>
          <goal>exec</goal>
          </goals>
          </execution>
          </executions>
          <configuration>
          <executable>cleanup.groovy</executable>
          </configuration>
          </plugin>


          You can use any executable file instead of cleanup.groovy but keep in mind that is has to be executable (chmod +x ...).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 14:23

























          answered Nov 13 '18 at 18:33









          MichaelMichael

          1,2261912




          1,2261912












          • Thanks for answer. I have some questions: 1. Where this file 'cleanup.groovy' should be stored? On the same level as pom.xml? 2. Should this code you have provided in the answer be placed next to maven-surefire-plugin? 3. I created a file 'cleanup.groovy' with content 'println("All tests have been executed!")' and run one test class, but that line has not been executed. I don't know if I understood you correctly. Will be grateful for help.

            – JeyKey
            Nov 14 '18 at 14:18












          • In this example the cleanup.groovy should indeed be next to pom.xml. However you can configure any path you like. There is no need to put it next to surefire. The connection is made by the configured <phase>. Using <phase>post-test</phase> makes Maven execute right after test phase. See maven.apache.org/guides/introduction/… for more options for phases.

            – Michael
            Nov 14 '18 at 14:20












          • Note that I updated the source. I used the wrong phase clean which will execute during mvn clean.

            – Michael
            Nov 14 '18 at 14:25











          • The problem is that in this project I already had maven-failsafe-plugin and it has <phase>verify</phase>. Here you can see whole plugin ibb.co/e5O7Pf I will try to change it to 'post-test' and see what will happen. Cause I guess it was set to verify for some reason

            – JeyKey
            Nov 14 '18 at 14:27











          • If you want the script be executed after failsafe, you should use <phase>post-verify</phase>.

            – Michael
            Nov 14 '18 at 14:29

















          • Thanks for answer. I have some questions: 1. Where this file 'cleanup.groovy' should be stored? On the same level as pom.xml? 2. Should this code you have provided in the answer be placed next to maven-surefire-plugin? 3. I created a file 'cleanup.groovy' with content 'println("All tests have been executed!")' and run one test class, but that line has not been executed. I don't know if I understood you correctly. Will be grateful for help.

            – JeyKey
            Nov 14 '18 at 14:18












          • In this example the cleanup.groovy should indeed be next to pom.xml. However you can configure any path you like. There is no need to put it next to surefire. The connection is made by the configured <phase>. Using <phase>post-test</phase> makes Maven execute right after test phase. See maven.apache.org/guides/introduction/… for more options for phases.

            – Michael
            Nov 14 '18 at 14:20












          • Note that I updated the source. I used the wrong phase clean which will execute during mvn clean.

            – Michael
            Nov 14 '18 at 14:25











          • The problem is that in this project I already had maven-failsafe-plugin and it has <phase>verify</phase>. Here you can see whole plugin ibb.co/e5O7Pf I will try to change it to 'post-test' and see what will happen. Cause I guess it was set to verify for some reason

            – JeyKey
            Nov 14 '18 at 14:27











          • If you want the script be executed after failsafe, you should use <phase>post-verify</phase>.

            – Michael
            Nov 14 '18 at 14:29
















          Thanks for answer. I have some questions: 1. Where this file 'cleanup.groovy' should be stored? On the same level as pom.xml? 2. Should this code you have provided in the answer be placed next to maven-surefire-plugin? 3. I created a file 'cleanup.groovy' with content 'println("All tests have been executed!")' and run one test class, but that line has not been executed. I don't know if I understood you correctly. Will be grateful for help.

          – JeyKey
          Nov 14 '18 at 14:18






          Thanks for answer. I have some questions: 1. Where this file 'cleanup.groovy' should be stored? On the same level as pom.xml? 2. Should this code you have provided in the answer be placed next to maven-surefire-plugin? 3. I created a file 'cleanup.groovy' with content 'println("All tests have been executed!")' and run one test class, but that line has not been executed. I don't know if I understood you correctly. Will be grateful for help.

          – JeyKey
          Nov 14 '18 at 14:18














          In this example the cleanup.groovy should indeed be next to pom.xml. However you can configure any path you like. There is no need to put it next to surefire. The connection is made by the configured <phase>. Using <phase>post-test</phase> makes Maven execute right after test phase. See maven.apache.org/guides/introduction/… for more options for phases.

          – Michael
          Nov 14 '18 at 14:20






          In this example the cleanup.groovy should indeed be next to pom.xml. However you can configure any path you like. There is no need to put it next to surefire. The connection is made by the configured <phase>. Using <phase>post-test</phase> makes Maven execute right after test phase. See maven.apache.org/guides/introduction/… for more options for phases.

          – Michael
          Nov 14 '18 at 14:20














          Note that I updated the source. I used the wrong phase clean which will execute during mvn clean.

          – Michael
          Nov 14 '18 at 14:25





          Note that I updated the source. I used the wrong phase clean which will execute during mvn clean.

          – Michael
          Nov 14 '18 at 14:25













          The problem is that in this project I already had maven-failsafe-plugin and it has <phase>verify</phase>. Here you can see whole plugin ibb.co/e5O7Pf I will try to change it to 'post-test' and see what will happen. Cause I guess it was set to verify for some reason

          – JeyKey
          Nov 14 '18 at 14:27





          The problem is that in this project I already had maven-failsafe-plugin and it has <phase>verify</phase>. Here you can see whole plugin ibb.co/e5O7Pf I will try to change it to 'post-test' and see what will happen. Cause I guess it was set to verify for some reason

          – JeyKey
          Nov 14 '18 at 14:27













          If you want the script be executed after failsafe, you should use <phase>post-verify</phase>.

          – Michael
          Nov 14 '18 at 14:29





          If you want the script be executed after failsafe, you should use <phase>post-verify</phase>.

          – Michael
          Nov 14 '18 at 14:29



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283012%2fhow-to-invoke-a-custom-method-cleanup-after-all-tests-geb-and-spock%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20