This is a follow-up on the pull request to introduce BeforeFeature and
AfterFeature hooks:
https://github.com/cucumber/cucumber-jvm/pull/295
Maggie Zhou writes:
> Hi, I really think to get the AfterClass/AfterFeature and BeforeClass/BeforeFeature
> annotations is very useful for improving the efficiency of tests.
>
> For example:
> My test is search service content in different ways:
> Scenario: search file by id
> Scenario: search file by name
> Scenario: search file by metadata
> Scenario: search file by create_date
> Scenario: search file by last_modified_date
> ......
> To make sure the tests get something to return. I need pre upload some files
> and clean up those files after all the tests have been run.
There are several ways of solving this issue without using the
proposed hook, and the best one depends on several factors.
One is to write a step like "Given sample files exist" (or better list
what those files should be so that it's clearer why the searches work)
and put it in the feature Backgroud. If speed is a concern, the
implementation of that step in Java would be something like this...
public class FileSetupStepDef {
static boolean uploaded = false;
@Given("sample files exist")
public void sampleFilesExist() {
if (!uploaded) {
// TODO UPLOAD FILES
uploaded = true;
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// TODO DELETE FILES
}
});
}
}
}
It does not need hooks, but it needs a background. Of course the same
thing can be done with in a tagged before hook instead of in an
explicit setup step.
If the files need to be different from feature to feature, you can
achieve something similar saving in a static variable what is the
current setup and do both the deletion of old files and upload of new
ones only if it changes.
As I said, it can be implemented in several ways and in my opinion
there is no need for an explicit hook.
Maggie, does this solve your problem?
Paolo