I was wondering if there is a good argument for or against using backgrounds in cucumber when compared to using tags and hooks.
Having a logged in user before the start of a test could go either like this:
Background:
Given that I am logged in
Scenario: Lorem ipsum sit amet dolor
[...]
or like this:
@login
Scenario: Lorem ipsum sit amet dolor
[...]
+
before(@login) do
visit('/admin/login/testuser')
end
Any idea when to favor one over the other?
Backgroundis useful when you provide common customer-readable (non technical) background for your scenarious. It is worth using it if you want to be explicit about this initialization in the text of your Feature.But sometimes teardown (and setup) logic is an implementation details and is implemented in
Before,AfterorAroundhooks (because reader of your spec won’t need to know about these technical things).Summary: use Background if you want to inform reader of your spec of the background and use hooks when background is an implementation details.
In you example Background is the best choice.