1

I have a difficulty with writing Cucumber tests. The re-usability of the Cucumber sentences is not consistent - I would like to avoid code duplication when creating sentences and methods glued to the sentences.

Using the Cucumber's built in tool does not work as it slows Eclipse so badly. How to reuse code in an efficient way?

For ex. I have the following Cucumber test in one suite:

Scenario:
  Given As a full right user
  When I open the webpage
  Then I see the edit button

And another one in another suite:

Scenario:
  Given As a full right user
  When I open the webpage
  Then I can remove a user

Not two, but having hundreds of tests, how can I handle the methods and cucumber scenarios of duplication? Duplication like I open the webpage and I load the webpage meaning the same, working in teams and because having hundreds of tests one may not notice that the method/sentence already exists.

3
  • I'm not sure what do you mean, can you add an example ?
    – Rsf
    Aug 16, 2019 at 12:53
  • Edited it with an example.
    – MMMM
    Aug 16, 2019 at 13:22
  • If I understand your question, you mean that the same wording performs different actions depending on the test, i.e. When I open the webpage Simply pass a parameter in the step, since the variable is the URL, so When I open the webpage "url" and write your step to act on the passed url. Aug 17, 2019 at 1:57

2 Answers 2

2

An autocomplete would come against Cucumber's goal of creating a shared vocabulary.

When the Three Amigos come to work on a new feature and its scenarios, they would develop this vocabulary, which mitigates the chances duplicated vocabulary. Keeping scenarios short and use proper mocking also helps.

If you need to send parameters to a step definition, you can use Expressions:

@Given("I have {int} cukes in my belly")
public void i_have_n_cukes_in_my_belly(int cukes) {
}

Given I have 0 cukes in my belly
Then I am hungry

Given I have 50 cukes in my belly
Then I am bloated

Given I have 5 cukes in my belly
Then I am satisfied

If you want to compose steps, you can simply call using Cuke4Cuke:

Given I eat a regular meal
Then I am satisfied

 @Given("I eat a regular meal")
 public void i_eat_regular_meal() {
   Given("I have 5 cukes in my belly");
   Given("I drink a cup of coffee");
 }
3
  • Thank you for your answer but It's not exactly when I was asking. The question is when you have many test cases with a lot of sentences (thousands for ex), and you would like to create a new test - how can you avoid code duplication and redundancy?
    – MMMM
    Aug 16, 2019 at 13:44
  • 1
    The last point touches this issue, by showing how to create steps on higher levels of abstractions, by reusing existing ones. Automatically, that's the basic tool - as mentioned, better conversations on when defining the steps mitigates the problem in a way the tool cannot do automatically. Aug 16, 2019 at 13:49
  • 1
    MMMM do you use linters / static code analysis tools on the automated tests code?
    – Prome
    Aug 19, 2019 at 5:36
1

I understand your point, at some point i had the same issue (i still have to some extent). I tried below counter measure.

  • Granulise you steps, ie 1 for click, 1 for getting text.
  • Tried separating your steps (methods for sentences) in different files based on use case. Steps specific to different pages should be kept in different files and and steps specific to browser action in separate files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.