Is it possible to override step definitions in a behat context?
Asked Answered
R

3

12

Is it possible to have have a subcontext class extend another subcontext and override functions?

At present I have

class TestContext extends BehatContext {
    /**
     * @Given /^a testScenarioExists$/
     */
    public function aTestscenarioexists() {
        echo "I am a generic test scenario\n";
    }
}

and

class SpecialTestContext extends TestContext {
    /**
     * @Given /^a testScenarioExists$/
     */
    public function aTestscenarioexists() {
       echo "I am a special test scenario\n";
    }
}

In feature context I tell it us the SpecialTestContext as a subcontext.

When I run the test behat complains with

[Behat\Behat\Exception\RedundantException]
Step "/^a testScenarioExists$/" is already defined in SpecialTestContext::aTestscenarioexists()

Can somebody please point in me the right direction with this ?

To give some further info as to why I'm trying to achieve this what I am trying to achieve is the ability to run scenarios with different environments, and have the environment specified in the gherkin file, for example:

Scenario: Test with generic environment
Given I am in the environment "generic"
    And a test scenario exists

Scenario: Test with specialised environment
Given I am in the environment "specialised"
    And a test scenario exists

I then can use add some code in FeatureContext to load up the correct sub-context.

Rationalism answered 18/3, 2013 at 8:38 Comment(0)
S
11

As Rob Squires mentioned, the dynamic context loading will not work.

But I do have a workaround for overriding step definitions, that I use regularly. Don't annotate your method. Behat will pick up the annotation of the overridden method in the superclass, and will map that method name to the matching step. When a matching step is found, the method in your subclass will be invoked. To make it obvious, I've taken to using the @override annotation for this. The @override annotation has no special meaning to Behat.

class SpecialTestContext extends TestContext {
    /**
     * @override Given /^a testScenarioExists$/
     */
    public function aTestscenarioexists() {
       echo "I am a special test scenario\n";
    }
}
Sward answered 23/7, 2013 at 16:52 Comment(1)
This is the actual solution - overriding methods in subclass, without changing the pattern docblock.Ott
M
4

In short...this is not possible : http://docs.behat.org/guides/2.definitions.html#redundant-step-definitions

In terms of loading subcontexts dynamically, this is not possible :

  1. Subcontexts are loaded at 'compile time' - ie. in the main FeatureContext constructor
  2. By the time the first step definition is run, behat has already collected all annotations and mapped them to step definitions, no more can/should be added

Check this out to understand how a Context behaves : http://docs.behat.org/guides/4.context.html#contexts-lifetime

Couple of wider things to consider :

  1. Anything captured in a gherkin scenario must be understandable by non-developers, (or at the very least developers who didn't write the system!). A scenario should convey in-full, one (ideally no more) business rule without having to dig into any code

  2. You don't want to hide too much logic up in step definitions, any rules should be captured in a gherkin scenario

  3. It's up to your how you organise your FeatureContexts, however your going to want to do this by themes/domains within your system, for example:

    • a DatabaseContext may be concerned with reading + writing to a test db
    • an ApiContext may contain steps concerned with validating an api within your system
    • an CommandContext may be concerned with validating your systems console commands
Microphysics answered 8/4, 2013 at 23:36 Comment(1)
Just updating the links provided above. * docs.behat.org/en/latest/user_guide/context/… * docs.behat.org/en/latest/user_guide/…Eelpout
L
-1

Overwrited method cannot be defined with the same sentence.

class SpecialTestContext extends TestContext {

  public function aTestscenarioexists() {
    echo "I am a special test scenario\n";
  }
}
Lecythus answered 18/9, 2013 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.