NAME

Test::Cucumber::Tiny - Cucumber-style testing in perl

DESCRIPTION

This Testing module provides a simple and less dependancy to run cucumber tests.

Cucumber is a tool that executes plain-text functional descriptions as automated tests. The language that Cucumber understands is called Gherkin.

We only need 2 things to build Cucumber test, a list a scenarios and to define the functions for the scenarios. Example in synopsis.

While Cucumber can be thought of as a "testing" tool, the intent of the tool is to support BDD. This means that the "tests" are typically written before anything else and verified by business analysts, domain experts, etc. non technical stakeholders. The production code is then written outside-in, to make the stories pass.

SYNOPSIS

If you need to shared the scenarios with the business analysts.

Use yml format to write the scenarios and then using YAML module to decode it into arrayref for constructing a cucumber.

Here is an example using arrayref:

use Test::More tests => 1;
use Test::Cucumber::Tiny;

subtest "Feature Test - Calculator" => sub {
   ## In order to avoid silly mistake
   ## As a math idiot
   ## I want to be told a sum of 2 numbers

   my $cucumber = Test::Cucumber::Tiny->new(
       scenarios => [
           {
               Scenario => "Add 2 numbers",
               Given    => [
                   "first, I entered 50 into the calculator",
                   "second, I entered 70 into the calculator",
               ],
               When => [ "I press add", ],
               Then => [ "The result should be 120 on the screen", ]
           },
           {
               Scenario => "Add numbers in examples",
               Given    => [
                   "first, I entered <1st> into the calculator",
                   "second, I entered <2nd> into the calculator",
               ],
               When     => [ "I press add", ],
               Then     => [ "The result should be <answer> on the screen", ],
               Examples => [
                   {
                       '1st'  => 5,
                       '2nd'  => 6,
                       answer => 11,
                   },
                   {
                       '1st'  => 100,
                       '2nd'  => 200,
                       answer => 300,
                   }
               ],
           },
           {
               Scenario => "Add numbers using data",
               Given    => [
                   {
                       condition => "first, I entered number of",
                       data      => 45,
                   },
                   {
                       condition => "second, I entered number of",
                       data      => 77,
                   }
               ],
               When => [ "I press add", ],
               Then => [
                   {
                       condition => "The result is",
                       data      => 122,
                   }
               ],
           }
       ]
   );
   $cucumber->Given(
       qr/^(.+),.+entered (\d+)/,
       sub {
           my $c = shift;
           diag shift;
           $c->{$1} = $2;
       }
   );
   $cucumber->Given(
       qr/^(.+),.+entered number of/,
       sub {
           my $c = shift;
           diag shift;
           $c->{$1} = $c->{data};
       }
   );
   $cucumber->When(
       qr/press add/,
       sub {
           my $c = shift;
           diag shift;
           $c->{answer} = $c->{first} + $c->{second};
       }
   );
   $cucumber->When(
       qr/press subtract/,
       sub {
           my $c = shift;
           diag shift;
           $c->{answer} = $c->{first} - $c->{second};
       }
   );
   $cucumber->Then(
       qr/result.+should be (\d+)/,
       sub {
           my $c = shift;
           is $1, $c->{answer}, shift;
       }
   );
   $cucumber->Then(
       qr/result is/,
       sub {
           my $c = shift;
           is $c->{data}, $c->{answer}, shift;
       }
   );
   $cucumber->Test;
};

METHODS

new

Create a cucumber for test

my $cuc = Test::Cucumber::Tiny->new(
   scenarios => [
       {
           ....
       }
   ]
);

$cuc->Given(...);
...
$cuc->Then(...);

$cuc->Test;

Scenarios

Create a cucumber with a plain array list of scenarios

my $cuc = Test::Cucumber::Tiny->Scenarios(
   {
       ....
   }
);

$cuc->Given(...);
...
$cuc->Then(...);

$cuc->Test;

ScenariosFromYML

Create a cucumber from a YAML file.

YMAL Example:

- Scenario: Add 2 numbers
  Given:
    - first, I entered 50 into the calculator
    - second, I entered 70 into the calculator
  When: I press add
  Then: The result should be 120 on the screen

- Scenario: Add 3 numbers
  Given:
    - first, I entered 50 into the calculator
    - second, I entered 70 into the calculator
    - third, I entered 10 into the calculator
  When: I press add
  Then: The result should be 130 on the screen

In Code:

my $cuc = Test::Cucumber::Tiny->ScenariosFromYML( "scenarios.yml" );
$cuc->Given(...);
...
$cuc->Then(...);
$cuc->Test;

Before

@param regexp

@code ref

Given

@param regexp

@param code ref

When

@param regexp / hashref { regexp, data }

@param code ref

Then

@param regexp / hashref { regexp, data }

@param code ref

Any

Use any to set all 3 like below

$cuc->Any( qr/.+/ => sub { return 1 } );

Same as

$cuc->Given( qr/.+/ => sub { return 1 } );
$cuc->When( qr/.+/ => sub { return 1 } );
$cuc->Then( qr/.+/ => sub { return 1 } );

After

@param regexp

@code ref

NextStep

When you are the functions of Given

Call NextStep will jump to When

When you are the functions of When

Call NextStep will jump to Then

When you are the functions of Then

Call NextStep will finish the current scenario.

NextExample

When you are the functions of Given, When or Then

Call NextExample will finish the current cycle and use the next example data in the current scenario.

NextScenario

Just jump to the next scenario.

Test

Start Cucumber to run through the scenario.

SEE ALSO

http://cukes.info/

https://github.com/cucumber/cucumber/wiki/Scenario-outlines