简体   繁体   中英

Configure Symfony in Behat

I have a project for which i have written features/scenarios in Behat, which is almost completed now. I have to test the email functionality on the site for which symfony comes in handy. But, i could not find any tutorial that helps me configure symfony from within Behat. Most of the sites provide Behat in Symfony and not the other way.

This is the article i found that has some information on configuration but it is not complete. http://extensions.behat.org/symfony2

This article http://docs.behat.org/cookbook/using_the_profiler_with_minkbundle.html gives code to check email functionality, but it does not say how to configure symfony in Behat. I have symfony extension installed.

This is my composer.json contents:

{
    "require": {
        "behat/behat": "*",
        "behat/mink": "1.4.0",
        "behat/mink-goutte-driver": "*",
        "behat/mink-selenium-driver": "*",
        "behat/mink-selenium2-driver": "*",
        "behat/mink-sahi-driver": "*",
        "behat/mink-zombie-driver": "*",
        "drupal/drupal-extension": "*",
        "symfony/process": "*",
        "behat/symfony2-extension": "*",
        "symfony/form": "*",
        "symfony/validator": "*",
        "behat/mink-extension": "*",
        "symfony/http-kernel": "*",
        "fabpot/goutte": "dev-master#5f7fd00"
    },
    "minimum-stability": "dev",
    "config": {
      "bin-dir": "bin/"
    }
}

Can anyone please guide me here?

In a Symfony 2(.2) configuration, you have to put your behat.yml file in your root folder, so the same folder where the composer.json is.

app/
bin/
src/
vendor/
web/
behat.yml
composer.json

This is an example of a working behat.yml :

default:
    # ...
    extensions:
        Behat\Symfony2Extension\Extension: ~
        Behat\MinkExtension\Extension:
            goutte:    ~
            selenium2: ~

Now, you have to launch the behat init command specifying your desired Bundle:

php bin/behat @AcmeDemoBundle --init

The above command will create the Features folder with a FeatureContext class inside it. Put in this class the methods of your scenario. Below the official hello world example:

/**
 * @Given /^I am in a directory "([^"]*)"$/
 */
public function iAmInADirectory($dir)
{
    if (!file_exists($dir))
        mkdir($dir);

    chdir($dir);
}

/**
 * @Given /^I have a file named "([^"]*)"$/
 */
public function iHaveAFileNamed($file)
{
    touch($file);
}

/**
 * @When /^I run "([^"]*)"$/
 */
public function iRun($command)
{
    exec($command, $output);
    $this->output = trim(implode("\n", $output));
}

/**
 * @Then /^I should get:$/
 */
public function iShouldGet(PyStringNode $string)
{
    if ((string) $string !== $this->output)
        throw new \Exception("Actual output is:\n" . $this->output);
}

Now you have to create the feature file ( ls.feature from the same example) inside your Features folder:

Feature: ls
    In order to see the directory structure
    As a UNIX user
    I need to be able to list the current directory's contents

    Scenario: List 2 files in a directory
        Given I am in a directory "test"
        And I have a file named "foo"
        And I have a file named "bar"
        When I run "ls"
        Then I should get:
            """
            bar
            foo
            """

So, your Features folder will look like the structure below:

Acme\DemoBundle\Features
    |- Context /
       |- FeatureContext.php
    ls.feature

Finally, launch behat and enjoy!

php bin/behat @AcmeDemoBundle

The dependencies needed to run behat with symfony are behat, symfony2-extension, mink and drivers, and you may need the formatter to get the desired output.

Then you need to configure the behat.yml for drivers, session and browser and to name test suites.

Once this is done you need to add your feature and FeatureContext to write the test scenarios and to define custom steps.

I have added a details configurations with my symfony installation with behat+mink+selenium+symfony here . Please check if it works for you.

I'm not sure if I completely understand what you're trying to achieve, but the fact that you're trying to achieve a different configuration of your Symfony2 project makes me think that you should probably make use of a custom environment. See this cookbook article on how to go about doing it.

For example you may want to setup a "behat" environment, which would have it's own configuration file ( /app/config/config_behat.yml ) and would be accessed using /app_behat.php/ from your behat tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM