Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

java - Cucumber hooks execute after all feature scenarios ended

I try to execute a script to clean up db after features executions in cucumber. I use the before and after hook in the following fashion:

    private static boolean skipActions = false;
    @Before("@initData")
    public void setupData() throws Exception {
     if (!skipActions) {
         initData();
         skipActions = true;
      }
    }

for the before I can avoid the script to be called before each scenario using a static variable. But didn't figure out how to do it for the after hook:

  @After("@clearData")
   public void tearDown() throws Exception {
      clearData();
  }

is there a way to capture if the last scenario has executed and trigger clearData() only if that condition is fulfilled ? Is there a more elegant way of doing it ?

question from:https://stackoverflow.com/questions/65846858/cucumber-hooks-execute-after-all-feature-scenarios-ended

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So in ruby there is an at_exit hook. but this isn't cucumber, it's programmatically related. I would look inside your language to see if this is possible.

Failing that, you can store a class variable during your run of the number of failures. then if all of those have passed then you can do something.

Another way you could tackle it would be to wrap your execution job in something like jenkins, jenkins has nice simple out of the box methods for cleaning things up.

In Jenkinsfile's both declarative and scripted pipelines allow the calling of cleanWs() as a groovy statement which then wipes the entire jenkins node.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...