What is the real difference between @Before hook in Cucumber and Background keyword in the feature file? I can think of only one difference, which is Background keyword is specific to only one file, whereas @Before is for the whole Cucumber framework. Are there any other differences?
Quoting the docs on hooks:
Think twice before you use
Before
Whatever happens in a
Before
hook is invisible to people who only read the features. You should consider using a background as a more explicit alternative, especially if the setup should be readable by non-technical people. Only use aBefore
hook for low-level logic such as starting a browser or deleting data from a database.
While Before
and Background
can be used to implement the same type of pre-test logic, it mainly comes down to whether or not the setup you are trying to perform is at-all relevant to the test. For example, if you are testing a web application, is there any explanatory power or benefit in starting each test with...
Background:
Given I have opened my browser
# ^ internally starts Selenium or a similar web-driver
If not, perhaps it should be a Before
hook.