[JVM] BeforeFeature/AfterFeature hooks

3,587 views
Skip to first unread message

Paolo Ambrosio

unread,
Jul 6, 2013, 5:57:35 AM7/6/13
to cu...@googlegroups.com
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

Maggie Zhou

unread,
Jul 8, 2013, 2:42:40 PM7/8/13
to cu...@googlegroups.com
Hi, Paolo,

Could you point me when the following part will be trigged. Will it be trigged at the end of each feature file? or after all feature files. I need something run once after the specific feature file (grouped the test scenarios).

.....
 Runtime.getRuntime().addShutdownHook(new Thread() { 
          @Override 
          public void run() { 
            // TODO DELETE FILES 
          } 
        }); 
.....

Thanks

Maggie ZHou

Paolo Ambrosio

unread,
Jul 8, 2013, 5:50:08 PM7/8/13
to cu...@googlegroups.com
Please follow rule 2 of this list and reply inline.

On Mon, Jul 8, 2013 at 7:42 PM, Maggie Zhou <maggie.t...@gmail.com> wrote:

> Could you point me when the following part will be trigged. Will it be
> trigged at the end of each feature file? or after all feature files. I need
> something run once after the specific feature file (grouped the test
> scenarios).

I don't know why you cannot download those files at the end of the
cucumber tests, and I still think that it is a bad idea to use a hook
for this setup, as it hides what it is done. You haven't provided any
concrete example, so I cannot be more specific.

Anyway, to answer your question, this is a gist with a hook that
uploads the files before the first scenario tagged with @mysetup and
cleans up before a scenario not tagged with @mysetup is executed or
at the end (whatever comes first). If only scenarios without @mysetup
are executed, it does not upload any file.

https://gist.github.com/paoloambrosio/5952737


Paolo

Maggie Zhou

unread,
Jul 8, 2013, 6:09:47 PM7/8/13
to cu...@googlegroups.com
I see the code you wrote for check the uploaded status, Thanks. For one time clean up, your code need use tag to separate the last scenario from others. I am looking for some way to avoid it.

My tests are:
 1.  upload certain files (could use the code hook @Before and upload files just one time)
 2.  In a feature file, I have some tests to against the uploaded files.
       search file by id;
       search file by name;
       search file by content;
       .......
  3. After all the test, my code remove the test files from service/database (After the last scenario)

For step 3, I am looking for a better way to identity the end of the feature file, and execute "delete file" at the really end (after the last scenario). Now I am putting a tag at the last scenario and hook @After to the tag to run the delete file method.

Maggie Zhou

Pranas Baliuka

unread,
Jul 8, 2013, 8:31:41 PM7/8/13
to cu...@googlegroups.com
I guess you should forget about it. I don't see public static getRuntime() in the cucumber.runtime.Runtime - https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/runtime/Runtime.java

aslak hellesoy

unread,
Jul 8, 2013, 9:09:13 PM7/8/13
to Cucumber Users
On Mon, Jul 8, 2013 at 7:31 PM, Pranas Baliuka <pra...@orangecap.net> wrote:
I guess you should forget about it. I don't see public static getRuntime() in the cucumber.runtime.Runtime - https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/runtime/Runtime.java



Pranas,

This thread is about java.lang.Runtime.

Aslak

P.S. please learn the rules on this mailing list.

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Paolo Ambrosio

unread,
Jul 8, 2013, 11:31:13 PM7/8/13
to cu...@googlegroups.com
Again, Maggie, please read the rules at the bottom of each message or
on the web and use interleaved answers:
http://en.wikipedia.org/wiki/Posting_style#Interleaved_style

On Mon, Jul 8, 2013 at 11:09 PM, Maggie Zhou <maggie.t...@gmail.com> wrote:

> On Monday, July 8, 2013 2:50:08 PM UTC-7, Paolo Ambrosio wrote:
>>
>> https://gist.github.com/paoloambrosio/5952737

I've updated the Gist as it didn't need to be that complex (revision 2
works with cucumber 1.1.3, not 1.1.4-SNAPSHOT).

> I see the code you wrote for check the uploaded status, Thanks. For one time
> clean up, your code need use tag to separate the last scenario from others.
> I am looking for some way to avoid it.

No, my code does not need any separate tag in the last scenario. That
is exactly the point. If you put it in a project with the two feature
files provided in the same gist you will see. All you have to do is to
tag the feature (or even single scenarios!) that you want to use that
setup and it should work. The feature files in the gist DO NOT have
tags other than the @mysetup in the feature.

> My tests are:
> 1. upload certain files (could use the code hook @Before and upload files
> just one time)
> 2. In a feature file, I have some tests to against the uploaded files.
> search file by id;
> search file by name;
> search file by content;
> .......
> 3. After all the test, my code remove the test files from service/database
> (After the last scenario)

Yes, and that is exactly what my code does. It uploads the files
before it finds a scenario that is tagged with @mysetup (the first in
the feature if you tag the whole feature) and it removes them when it
finds a scenario not tagged with @mysetup (in your case before it
executes the next feature file or before it shuts down if it's the
last feature file to be executed).

> For step 3, I am looking for a better way to identity the end of the feature
> file, and execute "delete file" at the really end (after the last scenario).
> Now I am putting a tag at the last scenario and hook @After to the tag to
> run the delete file method.

The end of a scenario comes right before the beginning of the next one
(that is where my cleanup takes place), so it should work unless you
do something weird in between with other hooks.


Paolo

Tommy Schmal

unread,
Aug 2, 2013, 4:15:29 PM8/2/13
to cu...@googlegroups.com
Yes, and that is exactly what my code does. It uploads the files 
before it finds a scenario that is tagged with @mysetup (the first in
the feature if you tag the whole feature) and it removes them when it
finds a scenario not tagged with @mysetup (in your case before it
executes the next feature file or before it shuts down if it's the
last feature file to be executed).

I like the idea of a @setup tag, but there's certainly a use case that it requires it to be a little deterministic. What if you have 5 different features and each one has a unique setup that's necessary before all scenarios of the feature run. While you can use an @setup tag on the feature, it doesn't know which feature's setup needs to run. I don't see any capability in cucumber that would allow it to know that information.

The behavior might be possible using the Background and a Given step in it (Given the setup for the x module), but then you can't easily do a cleanup per feature (because often, when there's a setup, there's a cleanup). Perhaps a combination of tags and Background would be necessary. For example:

@cleanup
Feature: blah blah
  Background:
    Given setup for the x module
  ... steps...

The Background/Given step would run the setup, and then you could have an @After("@cleanup") to clean up the mess.

I feel like there should be a better way. Yeah, you can probably eventually get all of this to work without an @AfterFeature or @BeforeFeature hook, but it sure seems like it'd make everyone's life easier if they were there.
Reply all
Reply to author
Forward
0 new messages