Using Cucumber and Perl to define test cases in natural language
Veggie Option
The Cucumber test framework helps developers and product departments jointly formulate test cases, not as program code, but in plain English. The initially skeptical Perlmeister has acquired a taste for this.
I'm a practical thinker and a realist – a natural combination, I find. In regard to developer frameworks for non-programmers, however, this means that when a new software product claims to support development on the basis of mouse clicks or descriptive text instead of code, I tend to steer clear. Experience has shown that, although simple solutions can be created with natural-language means, these projects reach their limits as tasks grow increasingly complex. When this occurs, the developer may have to ditch the project and re-implement everything from scratch in a proper programming language.
Perlmeister Stands Corrected
A few years ago when I first heard about the Cucumber project [1], which describes test cases for software projects in natural language, I immediately suspected a retake of the familiar "you-don't-need-programming-skills" mantra. My reflex was to turn my back on Cucumber and steer toward things with more substance. But, it seems I was wrong, because the project has gained much popularity among serious developers in the meantime.
In the end, I revised my own opinion. The decisive factor was reading the book about the Cucumber project [2]. The 300-page book explores the functions of the toolset in detail and shows step by step how a useful test suite is created during a real-life software project.
Here's how it works: In addition to the natural language description of a test case, say, Fetch the Facebook stock quote from the server
, Cucumber uses step definitions that use regular expressions to search the text and trigger actions that are encoded in a proper programming language. In this example, the step definition would jump to Fetch the
… stock quote
in the text, extract the Facebook
parameter, and get ready to retrieve the price of Facebook shares from the server with a function from a web library.
Most of Cucumber's step definitions are written in Ruby, but the toolset also supports Java or .NET. Thanks to the CPAN module Test::BDD::Cucumber, there is even a more or less satisfactory Perl port that you can experiment with.
Good Etiquette
Cucumber does not just wrap monotonous test code in naturally formulated scenarios but generally promotes Behavior-Driven Development (BDD). This not only helps the programmer writing the test of a function before the actual implementation (Test-Driven Development, TDD), but it also specifies that the test ensures a desired behavior of the software externally (i.e., something that the product manager also understands).
With its structure, Cucumber encourages programming of both code and test code to meet the requirements for easily maintainable software. After all, if you use a cowboy coding method for your test cases, you should not be surprised that changes in the application require more work to fix the test suite than to write the actual project code.
Re-using step definitions in several scenarios actively prevents code duplication and thus installs safeguards against future maintenance problems. Cucumber also separates test preparation ("scaffolding") from execution. During the test run, this approach causes the suite to tear down and rebuild the test framework before each test case, which reliably prevents "leaky scenarios" (i.e., scenarios in which one test case impairs the next).
Friendly Helper
Listing 1 [3] shows a complete test description of a module for retrieving stock quotes. The file describes a "feature" (i.e., a required function of the system), which the developer will check with multiple test cases (scenarios). For Cucumber to recognize them as such, the file must reside in a directory by the name of features
and have a suffix of .feature
.
Listing 1
basic.feature
Keywords ending with a colon (Feature:
, Background:
, Scenario:
) introduce the individual sections in the feature file; their content is indented two spaces. Cucumber checks the functions of a feature by running multiple independent scenarios.
In practice, each test case requires a few steps to initialize – typing this every time would break the rule "Do not repeat yourself!" The Background:
section therefore defines actions that should take place before the start of each scenario. In my example, the programmer checks whether or not the CPAN Finance::YahooQuote module used for retrieving the share prices from the Yahoo server is installed and the script can thus create an instance of the class.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.