cucumber / cucumber-jvm

Cucumber for the JVM
https://cucumber.io
MIT License
2.69k stars 2.02k forks source link

Please provide a @BeforeFeature and @AfterFeature hooks #2883

Closed zephirus closed 1 month ago

zephirus commented 1 month ago

🤔 What's the problem you're trying to solve?

I think this is something that would give extra power to Cucumber. In my personal case, I need to run some code only after each feature ends all scenarios, both passed or failed.

✨ What's your proposed solution?

My proposal is to have these two hooks in alignment with what already exists for the Before and After and BeforeStep and AfterStep.

⛏ Have you considered any alternatives or workarounds?

I tried to get the number of scenarios in my feature file at the beginning and then use that information to use the already available @After and then run my code but that is also that I can't do. I didn't found any way to get the number of scenarios in a feature file.

📚 Any additional context?

No response

mpkorstanje commented 1 month ago

What sort of code would you need to run? If it is for reporting purposes you might be interested in using the plugin system.

zephirus commented 1 month ago

I have N scenarios in my feature files. Currently, I have an additional scenario at the end of each feature that is all the same in every feature (most of them) and must run ONLY at the end of the feature file.

mpkorstanje commented 1 month ago

It sounds like your scenarios are not independent. Cucumber is designed with the assumption that all scenarios are independent. This is generally a good practice when testing. So making it easier to work with dependent scenarios is not something we would like spend time on supporting.

But you can turn your dependent scenarios into a single scenario and then use the regular after scenario hook. There are a few ways to do that

Note: I'm making some assumptions about your situation here. But dependent scenarios and low level steps are frequently used together.

zephirus commented 1 month ago

Putting all scenarios in a single one is not a good solution. It makes organization worst. It seems to me that @AfterFeature is really super usefull.

mpkorstanje commented 1 month ago

A single hig level scenario doesn't really affect organisation. Assuming you organize the code that executes the high level steps cleanly.

But this is going a bit beyond a feature request.

zephirus commented 1 month ago

Wait. My scenarios can run independently. But I don't want to run a certain code after each one, only after the last one. Why do I need to put it there explicitly when I have hundreds of features and that code is the same? That is the purpose of After and Before right?

mpkorstanje commented 1 month ago

But I don't want to run a certain code after each one, only after the last one.

Why not? And why specifically run at the end of feature file? And not say after every 10 scenarios from any feature? It shouldn't really matter if the scenarios are independent right?

From an implementation perspective, Cucumber doesn't execute features. Only pickles (scenario's and examples) are executed. Additionally it wouldn't quite work when executing in parallel. Feature hooks for different features would interleave in the same context.

zephirus commented 1 month ago

Ok I understand that. The purpose is to all be independent. I don't want to change that.