O'Reilly logo

The Cucumber Book, 2nd Edition by Steve Tooke, Aslak Hellesoy, Matt Wynne

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Hooks

Cucumber supports hooks, which are blocks of code that run before or after each scenario. You can define them anywhere in your support or step definition layers, using the methods Before and After.

To test them, take any Cucumber project and try adding a file features/support/hooks.rb that looks like this:

 Before ​do
  puts ​"Go!"
 end
 
 After ​do
  puts ​"Stop!"
 end

If you run cucumber, you’ll see the two messages printed in the output at the beginning and end of the scenario. Cucumber’s Before and After hooks are a lot like the setup and teardown methods in the xUnit family of testing tools.

The most common use of Before and After hooks is to clear up any residual state left in external systems like databases so ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required