We are considering to use Cucumber on our project for acceptance testing.
When we write a scenario
in a Cucumber feature
, we write a list of Given
, When
and Then
statements.
As we use cucumber-jvm project, the Given
, When
and Then
statement are related to Java methods in (JUnit) classes.
I want to know what is the best organization for the code related to Given
/ When
/ Then
in the project structure. My main concern is the maintenance of the cucumber tests on a big project, where the number of scenario is quite important, and especially regarding the items that are shared between features.
I can see at least 2 main approaches:
Each feature is related to it's own JUnit class. So if I have a
foo/bar/baz.feature
cucumber file, I will find the releatedfoo.bar.Baz
JUnit class with the adequate@Given
,@When
and@Then
annotated methods.Separate
@Given
,@When
and@Then
methods into "thematic" classes and packages. For example, if in my cucumber scenario I have a statementGiven user "foo" is logged
, then the@Given("^user \"([^\"]*)\" is logged$")
annotated method will be located in thefoo.user.User
class method, but potentially, the@When
method used later in the same cucumber scenario will be in a different Java class and package (let sayfoo.car.RentCar
).
For me, the first approach seems good in the way that I can easily do the relation between my cucumber features and my Java code. But the drawback is that I can have a lot of redundancies or code duplication. Also, it may be hard to find a possible existing @Given
method, to avoid to recreate it (the IDE can help, but here we are using Eclipse, and it does not seem to give a list of existing Given
statement?).
The other approach seems better essentially when you have Given
conditions shared among several cucumber feature, and thus I want to avoid code duplication. The drawback here is that it can be hard to make the link between the @Given
Java method and the Given
cucumber statement (maybe, again, the IDE can help?).
I'm quite new to cucumber, so maybe that my question is not a good question, and with time and experience, the structure will be self-evident, but I want to get good feedbacks on its usage...
Thanks.