Cucumber is a Behaviour-Driven-Development (BDD) tool. You can use to automate the acceptance tests for your software systems. A real-world example of its use in a large application is the MediaWiki QA process.
This post will show you the steps to get it up-and-running to test a trivial app on a Unix (Mac, Linux) computer. I’ll assume that you already have Ruby installed.
Installation
First up, install Cucumber’s Ruby implementation:<
|
|
If you’re on Debian, you can also achieve this with apt-get
:
|
|
The versions that I got from the install were-
|
|
Demo project
Make a “hello world” project:
|
|
One of the under-celebrated features of Cucumber is the quality of its error messages. Simply attempt to run it and you will be told what to do next:
|
|
Go ahead and run cucumber --init
as the program suggests:
|
|
This has set up your project. In the same vein as Test-Driven Development (TDD), the usual practice is to write the tests before you code.
Most programmers will be familiar with the expected output of a “Hello World” program. In Cucumber, this ‘feature’ would be represented in Cucumber as:
|
|
Save this definition to features/hello.feature
in the project directory and attempt to run Cucumber again:
|
|
The output helpfully contains the skeleton code for writing the test steps in Ruby. Using this as a base, I implemented a test using the rspec expectations syntax, to check if the string starts with “Hello World”.
|
|
Save these steps to features/step_definitions/hello_steps.rb
Finally, its time to code. Write up an implementation of “Hello World” in basically any programming language, and save the output to an executable file called hello-world
:
Using bash:
|
|
|
|
Manually testing:
|
|
Automatically testing with Cucumber confirms that the test criteria are met:
|
|
Now that you’ve completed these steps, you should have this project structure:
|
|
Next steps
Our scenario for the Hello World program has users interfacing with the system entirely with non-interactive shell commands. Cucumber needs to be able to observe the external behaviour of your application, so your steps will end up including some powerful test libraries. Some that are used often are:
- Command-line: Aruba
- Web-based: Capybara (back-ends include Selenium WebDriver, tutorial here
- Mobile: iOS & Android - Calabash