| 1 | package Test::Pcuke::Manual; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Test::Pcuke::Manual - is a proto manual for Test::Pcuke package. | ||
| 6 | |||
| 7 | =head1 CUCUMBER | ||
| 8 | |||
| 9 | Cucumber is a behaviour-driven development tool that allows you to define | ||
| 10 | and execute software acceptance tests. The tests themselves are written in | ||
| 11 | pretty plain (your) native language. This plain native language is called Gherkin. | ||
| 12 | |||
| 13 | For example, if your native language is German (aka Deutsch), then Gherkin | ||
| 14 | looks like this: | ||
| 15 | |||
| 16 | # language: de | ||
| 17 | Funktionalität: Addition | ||
| 18 | |||
| 19 | Um dumme Fehler zu vermeiden | ||
| 20 | möchte ich als Matheidiot | ||
| 21 | die Summe zweier Zahlen gesagt bekommen | ||
| 22 | |||
| 23 | Grundlage: der Taschenrechner | ||
| 24 | Angenommen der Taschenrechner instance | ||
| 25 | |||
| 26 | Szenariogrundriss: Zwei Zahlen hinzufügen | ||
| 27 | Angenommen ich habe <Eingabe_1> in den Taschenrechner eingegeben | ||
| 28 | Und ich habe <Eingabe_2> in den Taschenrechner eingegeben | ||
| 29 | Wenn ich <Knopf> drücke | ||
| 30 | Dann sollte das Ergebniss auf dem Bildschirm <Ausgabe> sein | ||
| 31 | |||
| 32 | Beispiele: | ||
| 33 | | Eingabe_1 | Eingabe_2 | Knopf | Ausgabe | | ||
| 34 | | 20 | 30 | add | 50 | | ||
| 35 | | 2 | 5 | add | 7 | | ||
| 36 | | 0 | 40 | add | 40 | | ||
| 37 | |||
| 38 | English speaker would prefer the following Gherkin: | ||
| 39 | |||
| 40 | # language: en | ||
| 41 | Feature: Addition | ||
| 42 | |||
| 43 | In order to avoid silly mistakes | ||
| 44 | As a math idiot | ||
| 45 | I want to be told the sum of two numbers | ||
| 46 | |||
| 47 | Background: calculator | ||
| 48 | Given a calculator instance | ||
| 49 | |||
| 50 | Scenario Outline: Add two numbers | ||
| 51 | Given I have entered <input_1> into the calculator | ||
| 52 | And I have entered <input_2> into the calculator | ||
| 53 | When I press <button> | ||
| 54 | Then the result should be <output> on the screen | ||
| 55 | |||
| 56 | Examples: | ||
| 57 | | input_1 | input_2 | button | output | | ||
| 58 | | 20 | 30 | add | 50 | | ||
| 59 | | 2 | 5 | add | 7 | | ||
| 60 | | 0 | 40 | add | 40 | | ||
| 61 | |||
| 62 | These examples express some expectations of the I<customer> on the software being developed | ||
| 63 | by the I<developer>. Both are expected to write these Gherkin texts together. Then the | ||
| 64 | developer provides I<definitions> which turn each such a text into a set of acceptance tests. | ||
| 65 | Then I<cucumber> tool reads these defenitions and executes Gherkin files one by one reporting | ||
| 66 | on failures and success. | ||
| 67 | |||
| 68 | Original cucumber tool is developed using Ruby language and does not support Perl. That's | ||
| 69 | why... | ||
| 70 | |||
| 71 | =head1 PCUKE | ||
| 72 | |||
| 73 | For example we develop a Local::Adder module that adds one number to the other. As a developers | ||
| 74 | we sit down with the customer and write down the texts shown above. Let's consider the english | ||
| 75 | example line by line. | ||
| 76 | |||
| 77 | =head2 Gherkin explained | ||
| 78 | |||
| 79 | # language: en | ||
| 80 | |||
| 81 | The first line is a pragma. Pragmas tell something important to the B<pcuke> parser. | ||
| 82 | Here we say that we use English for Gherkin statements. Note that English is a default | ||
| 83 | language and this line is not required. | ||
| 84 | |||
| 85 | Feature: Addition | ||
| 86 | |||
| 87 | This statement names the piece of functionality that is described in the file. "Feature:" | ||
| 88 | is a I<keyword>, "Addition" is a I<title> that best describes the functionality. There should | ||
| 89 | be exactly one I<Feature> statement per Gherkin file. | ||
| 90 | |||
| 91 | In order to avoid silly mistakes | ||
| 92 | As a math idiot | ||
| 93 | I want to be told the sum of two numbers | ||
| 94 | |||
| 95 | This is an optional narrative that describes in detail what feature title says in short. | ||
| 96 | It is for human consuption only. | ||
| 97 | |||
| 98 | Background: calculator | ||
| 99 | |||
| 100 | This keyword declares steps that should be executed before each scenario. | ||
| 101 | |||
| 102 | Given a calculator instance | ||
| 103 | |||
| 104 | This is a precondition common to all scenarios. | ||
| 105 | |||
| 106 | Scenario Outline: Add two numbers | ||
| 107 | |||
| 108 | This statement declares the template for scenarios. "Scenario Outline:" is a I<keyword> | ||
| 109 | while "Add two numbers" is a I<title>. Each scenario consists of steps that have names | ||
| 110 | "Given", "When" and "Then". These steps describe preconditions, events and | ||
| 111 | expected results respectively. There are steps with the special names "And" and | ||
| 112 | "But". These steps extend the previous "Given", "When", or "Then" step. | ||
| 113 | |||
| 114 | Given I have entered <input_1> into the calculator | ||
| 115 | |||
| 116 | The first step describes precondition: I enter a parameter, named "input_1" | ||
| 117 | into the calculator. | ||
| 118 | |||
| 119 | And I have entered <input_2> into the calculator | ||
| 120 | |||
| 121 | The second step adds another precondition, I enter a value of a parameter named "input_2". | ||
| 122 | |||
| 123 | When I press <button> | ||
| 124 | |||
| 125 | The third step describes an event, that I press something named <button> | ||
| 126 | |||
| 127 | Then the result should be <output> on the screen | ||
| 128 | |||
| 129 | The last step describes the expected behaviour: result should be the same as the value | ||
| 130 | of the parameter <output> | ||
| 131 | |||
| 132 | Examples: | ||
| 133 | |||
| 134 | This keyword declares a usage of the scenario template. It has no title. Below a table is given. | ||
| 135 | The first row contains headings of the columns that must correspond to the names of the parameters | ||
| 136 | in the step titles of the scenario outline. | ||
| 137 | |||
| 138 | The rows from second on describe one scenario each: | ||
| 139 | |||
| 140 | | input_1 | input_2 | button | output | | ||
| 141 | | 20 | 30 | add | 50 | | ||
| 142 | |||
| 143 | This row corresponds to the scenario with the following steps: | ||
| 144 | |||
| 145 | Given I have entered "20" into the calculator | ||
| 146 | And I have entered "30" into the calculator | ||
| 147 | When I press "add" | ||
| 148 | Then the result should be "50" on the screen | ||
| 149 | |||
| 150 | Each of the following rows describe two more scenarios: | ||
| 151 | |||
| 152 | | 2 | 5 | add | 7 | | ||
| 153 | | 0 | 40 | add | 40 | | ||
| 154 | |||
| 155 | |||
| 156 | Put this Gherkin text into <your calculator project root>/features/addition.feature file | ||
| 157 | (copy and paste from the CUCUMBER section above!). Now you can launch B<pcuke> from the | ||
| 158 | <your calculator project root>. The output should look like: | ||
| 159 | |||
| 160 | localhost:~/src/<your calculator project root>$ pcuke | ||
| 161 | Feature: Addition | ||
| 162 | |||
| 163 | In order to avoid silly mistakes | ||
| 164 | As a math idiot | ||
| 165 | I want to be told the sum of two numbers | ||
| 166 | |||
| 167 | Scenario Outline: Add two numbers | ||
| 168 | Given I have entered <input_1> into the calculator | ||
| 169 | And I have entered <input_2> into the calculator | ||
| 170 | When I press <button> | ||
| 171 | Then the result should be <output> on the screen | ||
| 172 | |||
| 173 | Examples: | ||
| 174 | | input_1 | input_2 | button | output | | ||
| 175 | | 20 | 30 | add | 50 | | ||
| 176 | | 2 | 5 | add | 7 | | ||
| 177 | | 0 | 40 | add | 40 | | ||
| 178 | |||
| 179 | |||
| 180 | 3 scenarios (3 undefined) | ||
| 181 | 15 steps (15 undefined) | ||
| 182 | |||
| 183 | localhost:~/src/<your calculator project root>$ | ||
| 184 | |||
| 185 | Pcuke says that it found 3 scenarios (one per each row in the table), 15 steps | ||
| 186 | (1 step in background + 4 steps in template) times 3 scenarios, and all steps and | ||
| 187 | scenarios are undefined. | ||
| 188 | |||
| 189 | =head2 Step Definitions | ||
| 190 | |||
| 191 | We should define steps. For that reason we create a file | ||
| 192 | <your calculator project root>/step_definitions/steps.pm with the | ||
| 193 | following content: | ||
| 194 | |||
| 195 | package steps; | ||
| 196 | use warnings; | ||
| 197 | use strict; | ||
| 198 | |||
| 199 | use Test::Pcuke::StepDefinition; | ||
| 200 | |||
| 201 | Given qr{^a calculator instance$} => sub { | ||
| 202 | # TODO | ||
| 203 | }; | ||
| 204 | |||
| 205 | Given qr{^I have entered "([^"]+)" into the calculator} => sub { | ||
| 206 | # TODO | ||
| 207 | }; | ||
| 208 | |||
| 209 | When qr{I press "([^"]+)"} => sub { | ||
| 210 | |||
| 211 | }; | ||
| 212 | |||
| 213 | Then qr{the result should be "([^"]+)" on the screen} => sub { | ||
| 214 | |||
| 215 | }; | ||
| 216 | |||
| 217 | 1; # Do not forget this number. pcuke uses require()! | ||
| 218 | |||
| 219 | Now launch B<pcuke> again to see (some output is skipped): | ||
| 220 | |||
| 221 | ............... | ||
| 222 | 3 scenarios | ||
| 223 | 15 steps | ||
| 224 | |||
| 225 | Everything is defined and pass. This is because we do not do anything in | ||
| 226 | our steps! Note however that we did not enter a definition like | ||
| 227 | |||
| 228 | And qr{^I have entered "([^"]+)" into the calculator} => sub { ... } | ||
| 229 | |||
| 230 | because it coincides with the corresponding Given definition. Simply speaking | ||
| 231 | the name of the step is not important at all. The rest of the title IS important. | ||
| 232 | |||
| 233 | Now let's do some actual work. in steps.pm edit a background step definition: | ||
| 234 | |||
| 235 | Given qr{^a calculator instance$} => sub { | ||
| 236 | my ($world, $text, $table) = @_; | ||
| 237 | $world->{_calculator} = Local::Adder->new; | ||
| 238 | }; | ||
| 239 | |||
| 240 | Launching the B<pcuke> we see that a background step is failed three times, before | ||
| 241 | execution of each of three scenarios. This is because we have not loaded Local::Adder! | ||
| 242 | |||
| 243 | Let's add two files. The first one is <your calculator project root>/lib/Local/Adder.pm: | ||
| 244 | |||
| 245 | package Local::Adder; | ||
| 246 | |||
| 247 | use warnings; | ||
| 248 | use strict; | ||
| 249 | |||
| 250 | sub new { | ||
| 251 | my ($class) = @_; | ||
| 252 | bless {}, $class; | ||
| 253 | } | ||
| 254 | |||
| 255 | 1; | ||
| 256 | |||
| 257 | The second is <your calculator project root>/features/support/env.pm | ||
| 258 | |||
| 259 | package Local::Adder::env; | ||
| 260 | |||
| 261 | use warnings; | ||
| 262 | use strict; | ||
| 263 | |||
| 264 | require 'lib/Local/Adder.pm'; | ||
| 265 | |||
| 266 | 1; # this number is important | ||
| 267 | |||
| 268 | Now B<pcuke> says that all steps are defined again. | ||
| 269 | |||
| 270 | Let's define the rest of the steps. Edit the <your calculator project root>/step_definitions/steps.pm | ||
| 271 | file: | ||
| 272 | |||
| 273 | Given qr{^I have entered "([^"]+)" into the calculator} => sub { | ||
| 274 | my ($world, $text, $table) = @_; | ||
| 275 | push @{ $world->{_arguments} }, $1; | ||
| 276 | }; | ||
| 277 | |||
| 278 | When qr{^I press "([^"]+)"$} => sub { | ||
| 279 | my ($world, $text, $table) = @_; | ||
| 280 | my ($a1, $a2) = @{ $world->{_arguments} }; | ||
| 281 | $world->{_result} = $world->{_calculator}->add( $a1, $a2 ); | ||
| 282 | }; | ||
| 283 | |||
| 284 | Then qr{^the result should be "([^"]+)" on the screen$} => sub { | ||
| 285 | my ($world, $text, $table) = @_; | ||
| 286 | expect( $world->{_result} )->equals($1); | ||
| 287 | }; | ||
| 288 | |||
| 289 | Now B<pcuke> says that "When" step fails. That's because there is no add method | ||
| 290 | in Adder. Define it in <your calculator project root>/lib/Local/Adder.pm: | ||
| 291 | |||
| 292 | sub add { $_[1] + $_[2]; } | ||
| 293 | |||
| 294 | Now all the steps pass which means that we have finished the feature. | ||
| 295 | |||
| 296 | =head1 BDD | ||
| 297 | |||
| 298 | See the RSpec book: | ||
| 299 | |||
| 300 | David Chelimsky, I<The RSpec Book. Behaviour-Driven Development with RSpec, Cucumber | ||
| 301 | and Friends>. - The Pragmatic Bookshelf, 2010. - (ISBN-10: 1-934356-37-9, | ||
| 302 | ISBN-13: 978-934356-37-1). | ||
| 303 | |||
| 304 | =head1 GHERKIN | ||
| 305 | |||
| 306 | See Cucumber Wiki: L<https://github.com/cucumber/cucumber/wiki/> | ||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | =head1 AUTHOR | ||
| 311 | |||
| 312 | Andrei V. Toutoukine, C<< <tut at isuct.ru> >> | ||
| 313 | |||
| 314 | =head1 BUGS | ||
| 315 | |||
| 316 | Please report any bugs or feature requests to C<bug-/home/tut/bin/src/test-pcuke at rt.cpan.org>, or through | ||
| 317 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=/home/tut/bin/src/Test-Pcuke>. I will be notified, and then you'll | ||
| 318 | automatically be notified of progress on your bug as I make changes. | ||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | =head1 SUPPORT | ||
| 324 | |||
| 325 | You can find documentation for this module with the perldoc command. | ||
| 326 | |||
| 327 | perldoc Test::Pcuke::Manual | ||
| 328 | |||
| 329 | |||
| 330 | You can also look for information at: | ||
| 331 | |||
| 332 | =over 4 | ||
| 333 | |||
| 334 | =item * RT: CPAN's request tracker | ||
| 335 | |||
| 336 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=/home/tut/bin/src/Test-Pcuke> | ||
| 337 | |||
| 338 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 339 | |||
| 340 | L<http://annocpan.org/dist//home/tut/bin/src/Test-Pcuke> | ||
| 341 | |||
| 342 | =item * CPAN Ratings | ||
| 343 | |||
| 344 | L<http://cpanratings.perl.org/d//home/tut/bin/src/Test-Pcuke> | ||
| 345 | |||
| 346 | =item * Search CPAN | ||
| 347 | |||
| 348 | L<http://search.cpan.org/dist//home/tut/bin/src/Test-Pcuke/> | ||
| 349 | |||
| 350 | =back | ||
| 351 | |||
| 352 | |||
| 353 | =head1 ACKNOWLEDGEMENTS | ||
| 354 | |||
| 355 | |||
| 356 | =head1 LICENSE AND COPYRIGHT | ||
| 357 | |||
| 358 | Copyright 2011 Andrei V. Toutoukine. | ||
| 359 | |||
| 360 | This program is released under the following license: artistic | ||
| 361 | |||
| 362 | |||
| 363 | =cut | ||
| 364 | |||
| 365 | 1; # End of Test::Pcuke::Manual |