Skip to content

[JUnit] Invoke (Before|After)Class and TestRules around Cucumber execution #1190

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

Merged
merged 1 commit into from
Aug 3, 2017

Conversation

mpkorstanje
Copy link
Contributor

@mpkorstanje mpkorstanje commented Aug 1, 2017

Junit invokes several framework methods around the test execution.
The cucumber runner should finish its whole execution inside these.

To be precises these two should be equivalent:

@BeforeClass
public static void before {
   // Do stuff
}

@Test
public void test(){
    cucumber.api.cli.Main.main(some arguments....)
}

@AfterClass
public static void after {
   // Do stuff
}

@RunWith(Cucumber.class)
public class Test {
    @BeforeClass
    public static void before {
       // Do stuff
    }

    @AfterClass
    public static void after {
     // Do stuff
    }
}

By firing the TestRunFinished event and printing the summary directly
after invoking the children rather then after the completion of the
whole test run these are made equivalent again.

Firing the TestRunFinished event before the JUnit execution
is done might look a bit odd but semantically this is a cucumber event
not a junit event and cucumber has finished its execution at this point.

Context

https://cucumberbdd.slack.com/archives/C590XDQQH/p1501620355467970

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • Awaiting confirmation from toothpicks131

I've manually placed breakpoints in the examples above to verify the methods are called in the correct order.

@mpkorstanje mpkorstanje force-pushed the complete-cucumber-in-junit-life-cycle branch 3 times, most recently from 80af564 to 64e163c Compare August 1, 2017 22:16
@mpkorstanje mpkorstanje added this to the 2.0.0 milestone Aug 1, 2017
@mpkorstanje mpkorstanje changed the title Wrap (Before|After)Class and TestRules around Cucumber execution [JUnit] Wrap (Before|After)Class and TestRules around Cucumber execution Aug 1, 2017
@mpkorstanje mpkorstanje changed the title [JUnit] Wrap (Before|After)Class and TestRules around Cucumber execution [JUnit] Invoke (Before|After)Class and TestRules around Cucumber execution Aug 1, 2017
Junit invokes several framework methods around the test execution.
The cucumber runner should finish its whole execution inside these.

To be precises these two should be equivalent:

```
@BeforeClass
public static void before {
   // Do stuff
}

@test
public void test(){
    cucumber.api.cli.Main.main(some arguments....)
}

@afterclass
public static void after {
   // Do stuff
}

```

```
@RunWith(Cucumber.class)
public class Test {
    @BeforeClass
    public static void before {
       // Do stuff
    }

    @afterclass
    public static void after {
     // Do stuff
    }
}
```

By firing the TestRunFinished event and printing the summary directly
after invoking the children rather then after the completion of the
whole test run these are made equivalent again.
@mpkorstanje mpkorstanje force-pushed the complete-cucumber-in-junit-life-cycle branch from 64e163c to 315e6db Compare August 1, 2017 22:21
@brasmusson
Copy link
Contributor

I can see how this makes sense. There are however some interesting differences in the fine details of execution depending on which runner that is used, for instance:

  • The JUnit and TestNG runners will establish the test count to be executed before any scenario is executed (because it needs to be communicated to the JUnit/TestNG infrastructure), but the CLI runner will compile and execute each feature file in sequence (so the test count from the later feature files are not known when the scenarios from the first feature file is executed).
  • The TestNG runner will finish the Cucumber execution (send the TestRunFinished event and print the summary) in a @AfterClass method, whereas the JUnit runner with this PR will have done that before any @AfterClass method has been executed.

From the Slack thread triggering this PR:

The challenge, though, is that the cucumber reports are not available when junit's AfterClass hook executes.

In this Slack thread the proposed @beforeall/@afterall are also discussed. However since @beforeall/@afterall (if implemented) will be part of the Cucumber execution, they would not be equivalent to JUnit @BeforeClass/@AfterClass methods. Specifically the Started/Finished events for these hooks should be available to the formatters and event listeners (which I have argued for Cucumber-Ruby, so the cucumber report cannot be available when a @afterall hook is executed.

@brasmusson brasmusson merged commit 315e6db into master Aug 3, 2017
brasmusson added a commit that referenced this pull request Aug 3, 2017
@mpkorstanje mpkorstanje deleted the complete-cucumber-in-junit-life-cycle branch August 3, 2017 17:25
@lock
Copy link

lock bot commented Oct 24, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 24, 2018
@mpkorstanje mpkorstanje restored the complete-cucumber-in-junit-life-cycle branch October 4, 2019 18:20
@mpkorstanje mpkorstanje deleted the complete-cucumber-in-junit-life-cycle branch October 4, 2019 18:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants