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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.