.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test::BDD::Cucumber::Manual::Tutorial 3pm" .TH Test::BDD::Cucumber::Manual::Tutorial 3pm "2016-05-03" "perl v5.22.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::BDD::Cucumber::Manual::Tutorial \- Quick Start Guide .SH "VERSION" .IX Header "VERSION" version 0.50 .SH "Introduction" .IX Header "Introduction" In this article we're going to jump straight in to using Test::BDD::Cucumber to build some simple tests for Digest, a core Perl module which provides message digests. .PP We'll create a \f(CW\*(C`features/\*(C'\fR directory, and put our first test case in it, \&\f(CW\*(C`features/basic.feature\*(C'\fR in it. The contents of it are, in their entirity: .PP .Vb 5 \& # Somehow I don\*(Aqt see this replacing the other tests this module has... \& Feature: Simple tests of Digest.pm \& As a developer planning to use Digest.pm \& I want to test the basic functionality of Digest.pm \& In order to have confidence in it \& \& Background: \& Given a usable Digest class \& \& Scenario: Check MD5 \& Given a Digest MD5 object \& When I\*(Aqve added "foo bar baz" to the object \& And I\*(Aqve added "bat ban shan" to the object \& Then the hex output is "bcb56b3dd4674d5d7459c95e4c8a41d5" \& Then the base64 output is "1B2M2Y8AsgTpgAmY7PhCfg" \& \& Scenario: Check SHA\-1 \& Given a Digest SHA\-1 object \& When I\*(Aqve added "" to the object \& Then the hex output is "" \& Examples: \& | data | output | \& | foo | 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 | \& | bar | 62cdb7020ff920e5aa642c3d4066950dd1f01f4d | \& | baz | bbe960a25ea311d21d40669e93df2003ba9b90a2 | \& \& Scenario: MD5 longer data \& Given a Digest MD5 object \& When I\*(Aqve added the following to the object \& """ \& Here is a chunk of text that works a bit like a HereDoc. We\*(Aqll split \& off indenting space from the lines in it up to the indentation of the \& first \e"\e"\e" \& """ \& Then the hex output is "75ad9f578e43b863590fae52d5d19ce6" .Ve .PP This is a complete test, and if you run pherkin against it, you will get sane output! It just doesn't do anything ... yet. .PP In the \f(CW\*(C`features/\*(C'\fR we'll add a \f(CW\*(C`step_definitions/\*(C'\fR directory, and add our first (and again, only) step definitions \&\f(CW\*(C`features/step_definitions/basic_steps.pl\*(C'\fR file in it: .PP .Vb 1 \& #!perl \& \& use strict; \& use warnings; \& \& use Test::More; \& use Test::BDD::Cucumber::StepFile; \& \& Given qr/a usable (\eS+) class/, sub { use_ok( $1 ); }; \& Given qr/a Digest (\eS+) object/, sub { \& my $object = Digest\->new($1); \& ok( $object, "Object created" ); \& S\->{\*(Aqobject\*(Aq} = $object; \& }; \& \& When qr/I\*(Aqve added "(.+)" to the object/, sub { \& S\->{\*(Aqobject\*(Aq}\->add( $1 ); \& }; \& \& When "I\*(Aqve added the following to the object", sub { \& S\->{\*(Aqobject\*(Aq}\->add( C\->data ); \& }; \& \& Then qr/the (.+) output is "(.+)"/, sub { \& my $method = {base64 => \*(Aqb64digest\*(Aq, \*(Aqhex\*(Aq => \*(Aqhexdigest\*(Aq }\->{ $1 } || \& do { fail("Unknown output type $1"); return }; \& is( S\->{\*(Aqobject\*(Aq}\->$method, $2 ); \& }; .Ve .PP When you run pherkin or the Test::Builder\-based test which does the same thing (900_run_features.t ), we look for a \f(CW\*(C`features/\*(C'\fR directory, and search for step definitions files (matched by \f(CW\*(C`*_steps.pl\*(C'\fR) and feature files (matched by \f(CW\*(C`*.feature\*(C'\fR). .PP The step matchers (the code that starts with \f(CW\*(C`Given\*(C'\fR, \f(CW\*(C`When\*(C'\fR and \f(CW\*(C`Then\*(C'\fR) are all loaded, and then we execute the feature files one by one. Let's step through the feature file, and look at how it matches up to the step definitions file. .SH "Name and conditions of satisfaction" .IX Header "Name and conditions of satisfaction" .Vb 4 \& Feature: Simple tests of Digest.pm \& As a developer planning to use Digest.pm \& I want to test the basic functionality of Digest.pm \& In order to have confidence in it .Ve .PP The first non-comment line of your feature file is a description of what you intend to do. You need to start the name itself with the string \f(CW\*(C`Feature:\*(C'\fR, and that should be the first line of your file, save comments (denoted by #). .PP Anything after that before the next new-line are your conditions of satisfaction. These aren't parsed, they're treated as human-readable text, and by convention, they're a user story . .SH "Background" .IX Header "Background" .Vb 2 \& Background: \& Given a usable Digest class .Ve .PP Next up, we have the Background section. The Background is a special kind of Scenario that doesn't have an explicit name, and should occur only once in your feature file. Its steps are run before the steps of every other scenario \- the harnesses distributed with this distro won't display the Background section separately, they'll just subsume the steps in to the other scenarios. .PP This is matched by: .PP .Vb 1 \& Given qr/a usable (\eS+) class/, sub { use_ok( $1 ); }; .Ve .PP \&\f(CW\*(C`Given()\*(C'\fR is a function exported by Test::BDD::Cucumber::StepFile that accepts two arguments: a regular expression (or a string when you don't need to do any smart matching) and a coderef. .PP If you're paying attention, you might notice that \f(CW\*(C`use_ok\*(C'\fR comes from Test::More. \fBEach step is run, from a\fR Test::Builder \fBperspective, as its own distinct test file\fR. This happens seamlessly, so you can use any Test::Builder\-based testing tools in your step definitions without really worrying about it. There's some more detail in Test::BDD::Cucumber::Manual::Steps. .SH "The First Scenario..." .IX Header "The First Scenario..." .Vb 6 \& Scenario: Check MD5 \& Given a Digest MD5 object \& When I\*(Aqve added "foo bar baz" to the object \& And I\*(Aqve added "bat ban shan" to the object \& Then the hex output is "bcb56b3dd4674d5d7459c95e4c8a41d5" \& Then the base64 output is "1B2M2Y8AsgTpgAmY7PhCfg" .Ve .PP The first scenario is delimited from the previous steps by a blank line, and it's called \fICheck \s-1MD5\s0\fR. Scenarios are marked out using the \f(CW\*(C`Scenario:\*(C'\fR keyword, and just like the Background section before, it's a series of steps. .PP These steps rely on the step before, which means we can examine the Test::BDD::Cucumber::StepContext object \f(CW$c\fR a little more closely. .PP .Vb 2 \& Given qr/a Digest (\eS+) object/, sub { \& my $c = shift; \& \& my $object = Digest\->new($1); \& ok( $object, "Object created" ); \& $c\->stash\->{\*(Aqscenario\*(Aq}\->{\*(Aqobject\*(Aq} = $object; \& }; .Ve .PP Creates a step definition. We create a new Digest object, and then use Test::More's \f(CW\*(C`ok()\*(C'\fR function to check that worked. We then put it in the \&\fIstash\fR for other steps to use. There are three stashes documented in Test::BDD::Cucumber::StepContext, \f(CW\*(C`feature\*(C'\fR, \f(CW\*(C`scenario\*(C'\fR and \f(CW\*(C`step\*(C'\fR. As you might expect, \f(CW\*(C`feature\*(C'\fR is available to all step definitions that are being executed as part of a feature, and \f(CW\*(C`scenario\*(C'\fR is available to all steps that are being executed as part of a scenario. .PP The context is the single argument that gets passed to each step, and it contains evertything that step should need to execute. We'll be looking at some of the methods you can call against it as we look at other steps, and you can find complete documentation for it here: Test::BDD::Cucumber::StepContext. .PP You'll note that the code above differs from the very first example, where we made use of \f(CW\*(C`C\*(C'\fR and \f(CW\*(C`S\*(C'\fR. \f(CW\*(C`C\*(C'\fR is a function which returns the current context, and \f(CW\*(C`S\*(C'\fR is a function which returns the scenario stash. So the above can be written: .PP .Vb 2 \& Given qr/a Digest (\eS+) object/, sub { \& my $c = shift; \& \& my $object = Digest\->new($1); \& ok( $object, "Object created" ); \& S\->{\*(Aqobject\*(Aq} = $object; \& }; .Ve .PP This scenario also introduce several ways of starting a step, \fIGiven\fR, \fIWhen\fR, and \fIThen\fR, as well as \fIAnd\fR. These are used to organize steps by type, with \&\fIGiven\fR tending to describe setup steps, \fIWhen\fR describing the key actions that you're testing, and \fIThen\fR describing the outputs you're looking for. You can find more on this here: . .PP A step definition you've declared with \fIGiven\fR \fBwill not\fR match a step starting with \fBThen\fR. You can use the keyword \fIStep\fR to declare general matching steps in your step definition files, although it's considered bad practice. .PP Finally, the keywords \fIAnd\fR and \fIBut\fR are simply replaced with the verb on the line before them. .SH "Scenario Outlines" .IX Header "Scenario Outlines" .Vb 9 \& Scenario: Check SHA\-1 \& Given a Digest SHA\-1 object \& When I\*(Aqve added "" to the object \& Then the hex output is "" \& Examples: \& | data | output | \& | foo | 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 | \& | bar | 62cdb7020ff920e5aa642c3d4066950dd1f01f4d | \& | baz | bbe960a25ea311d21d40669e93df2003ba9b90a2 | .Ve .PP The next scenario adds one of the three ways you can provide structured data to your steps, using placeholders and a table. This scenario is run three times, one for each table row, and with the \f(CW\*(C` being replaced by the appropriate row's column. These are called Scenario Outlines . .SH "Multiline Step Arguments" .IX Header "Multiline Step Arguments" .Vb 9 \& Scenario: MD5 longer data \& Given a Digest MD5 object \& When I\*(Aqve added the following to the object \& """ \& Here is a chunk of text that works a bit like a HereDoc. We\*(Aqll split \& off indenting space from the lines in it up to the indentation of the \& first \e"\e"\e" \& """ \& Then the hex output is "75ad9f578e43b863590fae52d5d19ce6" .Ve .PP While before we were looking at structured data on a Scenario level, we can also provide it on a Step level, in two ways. Firstly, we can provide multi-line strings, as above, using a feature that is syntactically similar to \&\f(CW\*(C`pystring\*(C'\fRs, and conceptually similar to HEREDOCs. The contents of the string will be available to the step definition via the \f(CW\*(C`data()\*(C'\fR method of the \&\fIcontext\fR: .PP .Vb 3 \& When "I\*(Aqve added the following to the object", sub { \& S\->{\*(Aqobject\*(Aq}\->add( C\->data ); \& }; .Ve .PP While we don't have an example of it here, you can also provide tables to your steps, which will also be available via \f(CW\*(C`data()\*(C'\fR: .PP .Vb 6 \& Scenario: Sort Employees \& Given a set of employees \& | name | wage | hair color | \& | Peter | 10,000 | brown | \& | John | 20,000 | blond | \& | Joan | 30,000 | green | .Ve .PP You can find out more about these features in the Cucumber documentation here: . .SH "Writing features and step files in languages other than English" .IX Header "Writing features and step files in languages other than English" By default, pherkin expects your features and step definitions to be written in English. Since feature files are mainly used for communication within your team, you might want to use your native language. To see a list of the languages you can used, ask pherkin what languages are supported: .PP .Vb 7 \& > pherkin \-\-i18n help \& | af | Afrikaans | Afrikaans | \& | ar | Arabic | العربية | \& | bg | Bulgarian | български | \& | bm | Malay | Bahasa Melayu | \& | ca | Catalan | català | \& ... .Ve .PP To see which keywords (and sub names) to use, ask pherkin about a specific language: .PP .Vb 10 \& > pherkin \-\-i18n de \& | feature | "Funktionalität" | \& | background | "Grundlage" | \& | scenario | "Szenario" | \& | scenario_outline | "Szenariogrundriss" | \& | examples | "Beispiele" | \& | given | "Angenommen", "Gegeben sei", "Gegeben seien" | \& | when | "Wenn" | \& | then | "Dann" | \& | and | "Und" | \& | but | "Aber" | \& | given (code) | "Angenommen", "Gegebensei", "Gegebenseien" | \& | when (code) | "Wenn" | \& | then (code) | "Dann" | .Ve .PP The last three lines of this list show you which sub names to use in your step file. Head over to the \fIi18n\fR directory for some examples. .SH "Next Steps..." .IX Header "Next Steps..." That's the tutorial done! You can find out more about writing steps in Test::BDD::Cucumber::Manual::Steps, the documentation for our simple command-line tool in App::pherkin, and how to integrate with Test::Builder in Test::BDD::Cucumber::Manual::Integration. .SH "AUTHOR" .IX Header "AUTHOR" Peter Sergeant \f(CW\*(C`pete@clueball.com\*(C'\fR .SH "LICENSE" .IX Header "LICENSE" Copyright 2011\-2016, Peter Sergeant; Licensed under the same terms as Perl