A glance at BDD, my Cucumber adventure

Squeeds Julkalender | 2023-12-11 | Jacob Bruér
Refactoring an old Javascript testbot, then Cucumber and Gherkin enters!
gherkins

Quickly after joining my team, I learned about the Javascript testbot that works but somebody left to slowly be forgotten. Once granted time, the team looked into using Cucumber as the new default tool to conduct and run integration testing with.

How can you use Cucumber?

Cucumber is a testing tool that supports behavior-driven development(BDD), enabling users to define and run automated tests based on Gherkin syntax. What is Gherkin you might wonder? It is a language which aim to describe the behavior of code in a way that is easy to understand(plain text) without the requirement of technical knowledge. My team uses Java but Cucumber can be used with various other programming languages, like Ruby or Javascript.

 

Tags

In Cucumber you can use @Tags to enable automated testing, associate certain scenarios and features which then can acts as triggers in scheduled test suites or during deployment processes. We use them both as execution triggers and configuration setup in conjunction with the "Before" and "After" step hooks.

cucumber-test-feature

This picture show both types of tags, @VehicleApp trigger the feature during the components build step and @SqueedHourly is a scheduled testing suite which runs every hour. The @Vehicle_GDPR_Report, and @European_Brand ones serves as identification for which configuration will take precedence during the given scenario. This allows you to easily reuse your step definitions and prevent step files from being bloated.

 

Background

Before both the scenarios in this feature you find a "Background"-paragraph. These are a great way to gather step definitions which are shared and they will run before every scenario since they are used by more than one.

 

Anti Patterns Try to refrain from creating these

  • Feature coupled or feature specific step definitions, they will cause code duplication
  • Conjunction steps, there is a reason they created "And" or "But". Can you find my blunder?
  • No clear separation between "Given", "When", "Then", describe the context, the action, and the outcome.

Scenario Outline

This particular feature runs API security checks on a longer interval based on the @SqueedDaily tag. This Scenario will run the same test case with different values. If you have worked with Java and JUnit you might recognize the similarities to the "Parameterized Tests", it is a great way to dynamically feed test data to a single test case. 

security-access-control

 

Cucumber test result

This is after one "Security Access Control" execution, here is where you can revisit all tests cases and find out their result, execution time, errors and report information.

cucumber-test-results

 

What I like about Cucumber

  • Run quick and scheduled automatic testing suites
  • Generate reports based on tested features and system behavior
  • Easily prepare systems and data for load test
  • Conduct initial troubleshooting without programming knowledge

 

Before leaving the team, one of my senior colleagues left me with a great gift and idea. Prior to decommissioning the old testbot, we used it for load testing but we had to keep track of a various tools, execute python scripts in sequence to update required data and align system processes. With Cucumber we can instead utilize Scenarios and Steps to easily prepare and configure the system from one place. Since our system is scheduled for load testing once every Program Increment, implementing a "one-click load test preparation" will be my next endeavor.

 

If you have any great ideas on how to use Cucumber, feel free to contact me - I would love to learn more!

 

 

buttons-to-press

 

 

Links

Cucumber documentation: https://cucumber.io/

Cucumber anti patterns: https://www.thinkcode.se/blog/2016/06/22/cucumber-antipatterns