cucumber / cucumber-jvm

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

Handler for TestRunStarted event gets triggered after all the tests finish in case of parallel execution of tests #2914

Closed MazurK239 closed 2 months ago

MazurK239 commented 2 months ago

πŸ‘“ What did you see?

I registered a plugin that catches TestRunStarted and TestRunFinished events to do some set-up / tear-down logic. This plugin extends EventListener and overrides method setEventPublisher.

When the tests are run sequentially (both via CLI and JUnit runners), the TestRunStarted handler is invoked, then tests are executed, then TestRunFinished handler is invoked, everything is fine CLI runner executed in IDEA

CLI runner executed in IDEA

JUnit runner executed via Gradle

JUnit runner executed via Gradle

But when I run the tests in parallel, the handler of TestRunStarted gets invoked right after all tests have been executed. Even though the actual event time stored in event object is correct (see the timestamps in log on the screenshots) CLI runner executed in IDEA

CLI runner executed in IDEA

JUnit runner executed via Gradle

JUnit runner executed via Gradle

In case of CLI runner I specified --threads 2 in the program args, in case of JUnit runner I specified cucumber.execution.parallel.enabled=true inside the junit-platform.properties file. The threads count doesn't affect anything in the latter case, it is reproduced even with one thread****

βœ… What did you expect to see?

I expected that during the parallel test execution the TestRunStarted handler will be executed once the event happens, before the tests are started. So that I could run some set-up logic inside it. Currently It seems to not be practical

πŸ“¦ Which tool/library version are you using?

org.junit:junit-bom:5.11.0 io.cucumber:cucumber-bom:7.18.1

πŸ”¬ How could we reproduce it?

Minimal reproducible example forked from the cucumber-java skeleton: cucumber-java-issue-events

Run feature file via IntelliJ IDEA (CLI runner will be used) or via Gradle

πŸ“š Any additional context?

No response

mpkorstanje commented 2 months ago

That is intentional. Most plugins can't handle parallel execution. You'll want to use the ConcurrentEventListener.

https://github.com/cucumber/cucumber-jvm/blob/0cdf507e357eda48c7496e63de9588b75134aa72/cucumber-plugin/src/main/java/io/cucumber/plugin/EventListener.java#L9-L15

mpkorstanje commented 2 months ago

Btw, for setup you might want to look at the @BeforeAll and @AfterAll hooks from Cucumber or depending on what you need, those recently added to the JUnit 5 Suite Engine.

MazurK239 commented 2 months ago

Thank you @mpkorstanje. Works like a charm! sorry that I missed this in the documentation. Didn't know about @BeforeAll and @AfterAll either, will definitely give them a try

MazurK239 commented 2 months ago

@mpkorstanje, since there is yet no documentation regarding the use of BeforeAll/AfterAll could you, please, advise, how to correctly use it in Kotlin? As I see, internally, there is a check that this method is static, but with Kotlin it seems impossible to make this check pass

mpkorstanje commented 2 months ago

Docs are here:

https://github.com/cucumber/cucumber-jvm/tree/main/cucumber-java#beforeall--afterall

And for Java to Kotlin interop you'll want to read:

https://kotlinlang.org/docs/java-to-kotlin-interop.html#static-methods

MazurK239 commented 2 months ago

Many thanks!

ejbartelds commented 2 weeks ago

I saw @MazurK239 's question and @mpkorstanje 's answer today, concerning the use of BeforeAll/AfterAll with Cucumber in Kotlin, but I ran into a problem when trying to use those. See screenshots.

It appears Cucumber's method scanning in io.cucumber.java.MethodScanner::scan sees both the original companion object method (which is not static) AND the synthesized one (due to @JvmStatic) (which has been made static). I am using cucumber-java 7.20.1

As a result, I cannot get this to work in Kotlin...

Any thoughts?

image image image

mpkorstanje commented 2 weeks ago

@ejbartelds for bugs it's usually better to start a new issue.

ejbartelds commented 2 weeks ago

See https://github.com/cucumber/cucumber-jvm/issues/2934 @mpkorstanje