Using Cucumber and Perl to define test cases in natural language
Scenarios Still Without Actions
Once Cucumber is installed (see the "Installation" section), impatient developers can launch the test suite on a trial flight. To do so, you need to call the cucumber
program in the directory above the features
directory, in which the basic.feature
file shown in Listing 1 resides.
Of course, Cucumber has at this time no idea what actions it needs to perform based on the feature description, because the step definitions are still missing. However, it tries to help out by sketching in some Ruby code, as shown in Figure 1.
Each text entry after Given
, When
, or Then
describes a scenario. Interestingly, Cucumber treats these keywords in the same way and only uses the assigned step definition. The different designations are only used to formulate the requirements understandably for the user.
Perl Without Gloss
The Perl implementation, with the aid of the CPAN Test::BDD::Cucumber [4] module, lacks a bit of the elegance of its role model but gets the job done. To start the test suite, the Perl programmer does not call cucumber
as in the original Ruby version; instead, it calls a script named pherkin
, which is included with the module and probably a synthesis of "Perl" and "gherkin." The output in Figure 2 uses gray text for nonfunctioning scenarios to make perfectly clear which step definitions are missing.
Listing 2 shows the step definition, which snaps up the text after the Given
expression in the background description. The file must have an extension of .pl
and reside in a directory called step_definitions
below features
.
Listing 2
basic_steps.pl – Definition 1
The regular expression squashed between Given qr/
and /
in line 6 searches for a string (a usable <[...]> class
) in all scenarios of the feature. In case of a match, the class name grouped by the brackets in (\S+)
lies in the $1
variable, and use_ok()
checks in line 7 whether the class can be loaded on the system using use Yahoo::FinanceQuote
.
The auxiliary function use_ok()
comes courtesy of the standard Test::More module. In typical Perl TAP format style, it outputs ok 1 if all goes well, and not ok 1 if an error occurs. The pherkin
tool understands this format and highlights the feature text in green or red accordingly.
Figure 3 shows that the background statement executed before the actual scenario is now highlighted in green, whereas the two scenarios that follow – When [...]
and Then [...]
– are still gray, or undefined.
Listing 3 delivers the rest of the step definitions in order to fully process the feature file in Listing 1. The pherkin
script imports Given
, Then
, And
, and other commands before calling the step definitions; the user does not need to include extra modules for this. Note that the two steps in the Retrieve Facebook
scenario depend on each other because the first step retrieves the share price from the server, and the second compares the numeric value with a preset in the scenario text.
Listing 3
basic_steps.pl – Definition 2
An Exchange with Context
The two steps communicate with one another via a context that pherkin
provides in the $c
variable if – as in line 5 of the steps definition in Listing 3 – the Method::Signatures module is loaded and the function to be called is declared using func($c) {}
(instead of sub{}
as shown in Listing 2). The method stash()
retrieves a reference to a memory area, which provides space for data generated in one step and needed in another under the scenario
key.
In Listing 3, the When
step stores the share price retrieved from the Yahoo server by the getonequote()
function of the Finance::YahooQuote module below the context's quote
key. The Then
step picks it up again from this location in line 24. The ok()
function from Test::More checks in line 15 whether a share price has arrived, and in line 24 whether it is greater than the value set in the text.
« Previous 1 2 3 Next »
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.