Using Cucumber and Perl to define test cases in natural language
Expandable Scenarios
Using the step definitions from Listing 3, all of the previously defined scenarios work perfectly – as you can see from Figure 4. Because Cucumber separates the feature description from the step definition, the latter is also suitable for other scenarios. To retrieve additional securities, besides the price of Facebook shares for the test, Listing 4 introduces another feature file, also located in the features
directory.
Listing 4
table.feature
The listing shows how the rows of an ASCII table written with pipe symbols following the Examples:
keyword each define a new test case. Thus, the Facebook share price must be greater than US$ 10 and the Amazon security greater than US$ 400. A glance at the stock market pages shows that the Internet retailer just missed this target when this issue went to press with a price of around US$ 397. The test case is obviously incorrectly defined; a more generous margin – for example, "greater than 1" – would be useful to increase the likelihood that the script still works in a couple of years.
In Figure 5, the failed scenario is highlighted in red. As you can tell from the grayed subsequent scenario, cucumber
stops testing a feature if a single part fails. If the reviewer corrects the expected Amazon price from US$ 400 to 300, all the tests run perfectly, as the green output in Figure 6 reveals.
When defining tables, the CPAN module, incidentally, differs from the original Cucumber tool – probably unintentionally: cucumber
rejects Examples:
in a Scenario:
and only accepts them in a section that starts with Scenario Outline:
.
Installation
You can install the Cucumber CPAN module presented here by typing:
cpan Test::BDD::Cucumber
Additionally, Test::BDD::Cucumber::Manual::Tutorial provides a short tutorial. The module runs independently of the original code by the Cucumber project, which is available as a Ruby gem. To load the latter onto a system with an installed Ruby interpreter, including the ruby-dev
package installed, just run two commands:
gem install gherkin gem install cucumber
In contrast to the Perl module, the original Cucumber Code also allows describing the features in languages other than English. Typing cucumber --language
reveals support for more than 40 languages.
As stated on the website, the Cucumber advantage is not primarily in writing test cases in a natural language. Instead, it opens the opportunity for developers and product management to collaborate on the feature document and test the results at the same time. The Cucumber framework promotes a dialog between the two parties during development, helping avoid expensive product defects caused by misunderstandings.
Infos
- Cucumber: http://cukes.info
- Ian Dees, Matt Wynne, and Aslak Hellesoy, The Cucumber Book: Behaviour-Driven Development for Testers and Developers. Pragmatic Programmers, 2012.
- Listings for this article: ftp://www.linux-magazin.de/pub/listings/magazine/161/Perl
- Test::BDD::Cucumber: http://search.cpan.org/~bdr/Test-BDD-Cucumber/
« Previous 1 2 3
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.