Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanup steps for Cucumber scenarios

Tags:

cucumber

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?

like image 988
TheDude Avatar asked Feb 22 '13 18:02

TheDude


People also ask

What can be used to perform a task after each scenario in Cucumber?

Cucumber supports hooks, which are blocks of code that run before or after each scenario. You can define them anywhere in your project or step definition layers, using the methods @Before and @After. Cucumber Hooks allows us to better manage the code workflow and helps us to reduce the code redundancy.

How do you fail a step in Cucumber scenario?

To fail a scenario you just need an assertion to fail, no need to set the status of the scenario. Cucumber will take care of that if an assertion fails. For testng you can use the SoftAssert class - http://testng.org/javadocs/org/testng/asserts/SoftAssert.html You will get plenty tutorials for this.

What are before after BeforeStep and AfterStep in Cucumber?

Looking at the result of a test run in the IntelliJ IDE, we can see the execution order: First, our two @Before hooks execute. Then before and after every step, the @BeforeStep and @AfterStep hooks run, respectively. Finally, the @After hook runs.


2 Answers

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

like image 163
prabhu Avatar answered Sep 17 '22 13:09

prabhu


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.

like image 35
orde Avatar answered Sep 16 '22 13:09

orde