3

One of my tests waits until an event happens in the Then step. If the test works fine there is no issue but if the test is failing (i.e. no event is triggered) then it just hangs.

How can I set a timeout in Cucumber?

I know JUnit has a timeout parameter you can use in the @Test annotation, is there something similar for Cucumber?

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114

1 Answers1

5

Cucumber has followed the JUnit pattern and offers a timeout parameter in its steps annotations. This takes a long value specifying the number of milliseconds after which the step is failed if it doesn't finish execution.

You can use it as follows:

@Then(value = "^verify (\\d+) events sent$", timeout = 5000)

This also works on the other step types (e.g. Given, When).

Don't forget to add value = before the steps definition string.

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
  • does this work? I tried a simple example and it doesn't seem to fail the test – jcalloway Jan 23 '18 at 16:52
  • @jcalloway it does for me. Why not ask a stackoverflow question with your code example, link it here and we can have a look? – Vlad Schnakovszki Jan 24 '18 at 10:41
  • 3
    Note that since Cucumber-JVM 5.x this is no longer available - see "Removing timeout" section of https://cucumber.io/blog/open-source/announcing-cucumber-jvm-v5-0-0-rc1/ – Olaf May 12 '20 at 08:28