Cleanup steps for Cucumber scenarios
Asked Answered
A

3

22

Is there a way to define the cleanup steps for all of the scenarios for a feature in Cucumber? I know that Background is used to define the setup steps for each scenario that follows it, but is there a way to define something like that to happen at the end of each scenario?

Appolonia answered 22/2, 2013 at 18:46 Comment(0)
D
14

You can use an After hook that will run after each scenario:

After do
  ## teardown code
end

There's also a Before hook that will allow you to set up state and/or test data prior to the scenario:

Before do
  ## setup code
end

The Before and After hooks provide the functionality of setup and teardown from Test::Unit, and they are generally located in hooks.rb in the features/support directory.

Dayfly answered 22/2, 2013 at 19:14 Comment(1)
Updating hooks doc URL: hereAntinomian
U
18

should also notice that 'Before' and 'After' is global hooks i.e those hooks are run for every scenario in your features file

If you want the setup and teardown to be run for just few testcases ( grouped by tags) then you need to use taggedHooks, where the syntax is

Before('@cucumis, @sativus') do
# This will only run before scenarios tagged
# with @cucumis OR @sativus.
end


AfterStep('@cucumis', '@sativus') do
# This will only run after steps within scenarios tagged
# with @cucumis AND @sativus.
end

For more info : https://github.com/cucumber/cucumber/wiki/Hooks

Unheardof answered 27/8, 2013 at 15:30 Comment(0)
D
14

You can use an After hook that will run after each scenario:

After do
  ## teardown code
end

There's also a Before hook that will allow you to set up state and/or test data prior to the scenario:

Before do
  ## setup code
end

The Before and After hooks provide the functionality of setup and teardown from Test::Unit, and they are generally located in hooks.rb in the features/support directory.

Dayfly answered 22/2, 2013 at 19:14 Comment(1)
Updating hooks doc URL: hereAntinomian
S
0

If you want to do a clean up before or after all the scenarios in a feature (i.e not after every scenario in a feature) , you can work around it by defining a scenario at the start of your feature file that does the clean up.

    @clean_database
  Scenario: Clean the database before running any tests.
    Given Clean the db first

In your stepDef for "Clean the db first" you can do a clean up of your db and will be run only once for the whole feature file instead of after every scenario.

Using @Before or @After will only clean up after every scenario and that may not be something you want.

Saltern answered 9/8, 2023 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.