-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add @BeforeAll and @AfterAll hooks #515
Comments
Does that fire when the process dies, or just the current cucumber run? What if you wanted to do multiple runs without killing the process? (In an Aruba 5.2 stylee) |
It wouldn't be triggered by a VM shutdown. It would be part of the runner's life cycle and fire after the last scenario. For Java I think they would have to be defined as static methods as well. |
What would happen if you try to embed data into the report in this Furthermore, I supposed embed wouldn't work in the shutdown hook either in Cheers, -Tim On Thu, May 2, 2013 at 6:46 AM, Aslak Hellesøy notifications@github.comwrote:
|
From a If you attempt to do a |
We definitely have many uses for this. Currently we use a static It would be good to have a clear annotation such as @BeforeAll and On 2 May 2013 07:37, Aslak Hellesøy notifications@github.com wrote:
|
+1. I want having multiple BeforeAll and AfterAll supported (opposite to not like World) |
Any movement on this issue? I'd love to have it. Of course, I'm just one person. ;-) |
Those of you who need it - how about sending a pull request? |
+1 because the trick above won't work for AfterAll |
@quantoid It's not a trick, it's Java! Why do you say that it does not work for The Added this snippet to the Java Hello World Example: @Before
public void beforeAll() {
if(!dunit) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("snake!");
}
});
System.out.println("badger");
dunit = true;
}
} After changing the runner to use the progress formatter, when I run
|
Sorry, I can see how it works in Java, I should've added "...in Groovy" but could be wrong about that too. I'd want both 'all' hooks to be able to access variables that other glue can access, i.e. those in the Script scope. |
Same thing in the groovy-calculator example with class CustomWorld {
def value = null
def dunit = false
// ...
}
Before() {
if (!dunit) {
dunit = true
println "badger"
value = "snake!" // sets the value in the World
addShutdownHook {
println value // reads the value from the World
}
}
} Note that if you change the value variable in other steps, the shutdown hook is not going to print This change to a step will make the Then(~"the stored result should be (.*)") { double expected ->
value = "mushroom"
assert expected == result
} Of course it works even if you define the |
Awesome, thanks! Where does the addShutdownHook come from? There seem to be so many classes mixed in to the scope of these closures. |
@quantoid No magic there, it's part of the Groovy Object API |
Hmm, IMHO would be better to tear stuff down as part of the feature execution block rather than when the JVM shuts down. |
Hi, I'd like to create a test database then destroy it after all the tests are done. The static boolean works for the @before tag, creating my database only once. But how would I drop the database only after all my tests are done? |
@TarekSaid this is a bug tracker, not a support forum. Please ask on the cukes google group. See https://github.com/cucumber/cucumber-jvm/blob/master/CONTRIBUTING.md |
This will work OK for as completely global before/after, but it doesn't work if you want the behavior per feature file. For example, I may have 5 different feature files, each one having 10 different scenarios. Per feature, I may want a before/after. This workaround isn't per feature, but is per EVERYTHING. A per feature before/after would be nice. For example, JBehave has a @Before/AfterStories. The code discussed in this thread would be equivalent to that, so this annotation isn't really necessary. It also has @Before/AfterScenario, which is equivalent to Cucumber's @Before/After. However, JBehave also has a @Before/AfterStory (singular) which isn't really doable with Cucumber-JVM (that I can tell). This would be very useful. If you think about it, JUnit has it, too, with @Before/AfterClass. Maybe I'm blind and there is a way to do it in Cucumber... but I can't see it. |
@tschmal before/after feature hooks are out of the scope of this issue. They were discussed several times on The Cukes Google Group, so you might search there. In a recent thread I explained how to deal with feature tags. If you still think you need it, please write on the list first and explain your problem. |
You don't need to do this in cucumber. Use the @BeforeClass and @afterclass annotation from within the JUnit test used to run the cucumber tests. This has the benefit of running only for the features specified by the paths or tags options. @RunWith(Cucumber.class)
@Cucumber.Options(format = {"html:target/cucumber-html-report", "json-pretty:target/cucumber-json-report.json"})
public class RunCukesTest {
@BeforeClass
public static void setup() {
System.out.println("Ran the before");
}
@AfterClass
public static void teardown() {
System.out.println("Ran the after");
}
} |
@wjpowell there is nothing in the Cucumber runner that handles @BeforeClass/@afterclass. Have you actually tried this? |
@aslakhellesoy I've tried it, and it works. The Cucumber runner calls super.run which handles the @BeforeClass and @afterclass annotated methods, as well as any @ClassRule defined. |
Thanks for the clarification. I thought it might be something like that. |
Pull request created for this issue: #672 |
Would love to see this included in the latest version :) |
I just used the BeforeClass and AfterClass annotations today and they worked perfectly. I'm not sure why we need to add our own BeforeAll / AfterAll hooks. |
@mattwynne, I see a few reasons:
|
@mattwynne Because those annotations are from JUnit and they only work when you use the JUnit Cucumber runner. Many people use Cucumber-JVM with the command-line runner, without JUnit at all, and then this would not work. |
Hi there, It was working for me and I was very happy, but one I got requirement to use parallel execution. I implemented it using below tutorial https://johnfergusonsmart.com/running-parallel-tests-serenity-bdd-cucumber/ and now it's not working. Please advise. |
This solution worked for me as well. Can you suggest me if I want to use this AfterClass annotation then how I can able to use it? |
https://medium.com/@hemanthsridhar92/global-hooks-in-cucumber-jvm-afc1be13e487 |
Work in progress to fix #515. TODO: - [ ] How to deal with failure? - [ ] Test with JUnit4 - [ ] Test with JUnit5 - [ ] Test with TestNG - [ ] Test with CLI - [ ] Invoke around semantics? - [ ] How to report execution results? - [ ] TeamCity Plugin - [ ] Pretty formatter - [ ] Messages/Events
Can you please provide one for @afterall |
@RayEltaib Have you tried this yet https://terribletester.com/beforeall-and-afterall-workaround-for-cucumber-java? |
Nice job! |
Could you help me ? What should i do about it? I am stuck it a week and i dont have any ideal for it |
`BeforeAll` and `AfterAll` hooks are executed before all scenarios are executed and after all scenarios have been executed. A hook is declared by annotating a method. This methods must be static and do not take any arguments. Hooks are global, all hooks declared in any step definition class will be executed. The order in which hooks are executed is not defined. An explicit order can be provided by using the `order` property in the annotation. ```java package io.cucumber.example; import io.cucumber.java.AfterAll; import io.cucumber.java.BeforeAll; public class StepDefinitions { @BeforeAll public static void beforeAll() { // Runs before all scenarios } @afterall public static void afterAll() { // Runs after all scenarios } } ``` Notes: 1. When used in combination with Junit 5, Maven Surefire, and/or Failsafe use version `3.0.0-M5` or later. 2. When used in combination with Junit 5 and InteliJ IDEA failures in before all and after all hooks do not fail a test run. Fixes: #515
People keep asking for
BeforeAll
andAfterAll
hooks. I haven't had a big need for them myself, but it seems to me this would be sufficient:If this doesn't cut it for you, please explain in this ticket, and maybe we'll add special support for it.
The text was updated successfully, but these errors were encountered: