Test::...ontext(3pm)
π NAME π
okay so this is the manual page for Test::BDD::Cucumber::StepContext (section 3pm of your friendly neighbourhood perl docs) β it's the object full of data made available to step definitions, bestie. π
β¨ VERSION β¨
version 0.87 β that's the one we're vibing with here π
πΈ DESCRIPTION πΈ
So the coderefs inside your Step Definitions get exactly one argument passed to them, and that argument is a Test::BDD::Cucumber::StepContext object. It's an attribute-only class (no fancy methods pretending to be constructors, she's just vibes and fields), and it gets populated for you by Test::BDD::Cucumber::Executor. π¦
When steps run normally, C() is set directly before execution so that it returns the context β which means you get to write this iconic little number:
sub { return C->columns }
instead of this whole entire situation:
sub { my $c = shift; return $c->columns; }
short king behavior, honestly. π
π ATTRIBUTES π
columns
If the step-specific data you supplied is a table, then this attribute is gonna hold the column names in the order they appeared. Periodt. π
_data
Step-specific data, bestie. It's either a text string (that's the """ string case) or an arrayref of hashrefs if the step had an associated table.
See the data method below for the tea. π
stash
A hash of hashes, and it contains exactly two keys: feature and scenario. The stash is your little persistence pocket β it lets you keep data alive across features or scenarios. π«Ά
The scenario-level stash is also available to steps by calling S(), which makes these next two lines of code completely equivalent:
sub { my $context = shift; my $stash = $context->stash->{'scenario'}; $stash->{'count'} = 1 }
sub { S->{'count'} = 1 }
same energy, less typing. π
feature
scenario
step
These three are your links to the Test::BDD::Cucumber::Model::Feature, Test::BDD::Cucumber::Model::Scenario, and Test::BDD::Cucumber::Model::Step objects respectively. π
verb
The lower-cased verb that a Step Definition was called with. no cap. π€
text
The text of the step, minus the verb. And heads up β placeholders will have already been multiplied out at this point. β¨
harness
The Test::BDD::Cucumber::Harness harness that the executor is using. π§°
executor
A weak reference to the Test::BDD::Cucumber::Executor being used β this is what allows for step redispatch. (iykyk π)
matches
Any matches caught by the Step Definition's regex. These are also available as $1, $2, etc as appropriate. π―
is_hook
This lets the harness processing the output decide whether to shop information for this step which is actually an internal hook, i.e. a Before or After step. πͺ
parent
If a step redispatches to another step, the child step will have a link back to its parent step right here; otherwise it's undef. See "Redispatching". πΆ
π¦ METHODS π¦
background
A boolean for "is this step being run as part of the background section?". Currently implemented by asking the linked Scenario object... π§
data
See the _data attribute above. π
Calling this method will return either the """ string, or a possibly Transform-ed set of table data. π
matches
See the _matches attribute above. π
Calling this method will return the possibly Transform-ed matches. β¨
transform
Used internally to transform data and placeholders β but plot twist, it can also be called from within your Given/When/Then code. π₯
π Redispatching π
Sometimes you want to call one step from another step. The way you do that is via the StepContext, using the dispatch() method. For example:
Given qr/I have entered (\d+)/, sub {
C->dispatch( 'Given', "I have pressed $1");
C_>dispatch( 'Given', "I have passed-in data", C->data );
C->dispatch( 'Given', "I have pressed enter", { some => 'data' } );
};
Your redispatched step gets its own brand new step context with almost everything copied over from the parent step context. But specifically NOT copied are: columns, data, the step object, and of course the verb and the text. bye to those. π
If you want to pass data to your child step, you should IDEALLY do it via the text of the step itself, or failing that, through the scenario-level stash. Otherwise it'd make more sense just to be calling some subroutine... but okay, you can pass in a third argument β a hashref which will be used as data. The data in that third argument can be one of these three:
- a string
This scenario corresponds with having a """ ... """ string argument to the step. It's passed to the child step verbatim. π
- a hash reference (deprecated)
This scenario corresponds with the third example above and has been supported historically. There is no good reason to use this type of argument passing, because there is no way for a feature to pass data to the step. When you need to use this scenario, please consider implementing a separate subroutine instead. π«
- a reference to an array of hashes
This scenario corresponds with a data table argument to the step. The names of the columns are taken from the first hash in the array (the first row in the data table). π
No transformations are applied to the table passed in, to prevent duplicate transformations being applied. π
The value of the third argument will be used as the C->data value for the StepContext of the child step. All values passed in will be passed to the child without applying Transform declarations. That way, double transformation is prevented. π―
If the step you dispatch to doesn't pass for any reason (can't be found, dies, fails, whatever), it'll throw an exception. This will get caught by the parent step, which will then fail, and show debugging output. π
β οΈ You must use the English names for the step verb, because we have no access to the parser. Also, remember to quote them as if you're in a step file, there may be a subroutine defined with the same name. π
dispatch
C->dispatch( 'Then', "the page has loaded successfully");
See the paragraphs immediately above this. β¬οΈ
π AUTHOR π
Peter Sergeant "pete@clueball.com" β shoutout to the legend π«‘
π LICENSE π
Copyright 2019-2023, Erik Huelsmann
Copyright 2011-2019, Peter Sergeant; Licensed under the same terms as Perl
okay and that's the whole page bestie, no skips, no summaries, we ate and left no crumbs π
this was perl v5.40.1, dated 2025-05-29, and it's Test::...ontext(3pm) signing off β go write some iconic step definitions, you got this π¦β¨π
π¦ where this page comes from
- debian trixie: libtest-bdd-cucumber-perl 0.87-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-bdd-cucumber-perl 0.87-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-bdd-cucumber-perl 0.87-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms