简体   繁体   English

在Behat中配置Symfony

[英]Configure Symfony in Behat

I have a project for which i have written features/scenarios in Behat, which is almost completed now. 我有一个项目,我已经在Behat中编写了功能/场景,现在几乎完成了。 I have to test the email functionality on the site for which symfony comes in handy. 我必须在网站上测试symfony派上用场的电子邮件功能。 But, i could not find any tutorial that helps me configure symfony from within Behat. 但是,我找不到任何帮助我从Behat中配置symfony的教程。 Most of the sites provide Behat in Symfony and not the other way. 大多数网站都提供Symfony中的Behat而不是其他方式。

This is the article i found that has some information on configuration but it is not complete. 这篇文章我发现有一些关于配置的信息,但它并不完整。 http://extensions.behat.org/symfony2 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. 本文http://docs.behat.org/cookbook/using_the_profiler_with_minkbundle.html提供了检查电子邮件功能的代码,但没有说明如何在Behat中配置symfony。 I have symfony extension installed. 我安装了symfony扩展。

This is my composer.json contents: 这是我的composer.json内容:

{
    "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. 在Symfony 2(.2)配置中,您必须将behat.yml文件放在根文件夹中,因此与composer.json所在的文件夹相同。

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

This is an example of a working behat.yml : 这是一个工作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: 现在,您必须启动指定所需Bundle的behat init command

php bin/behat @AcmeDemoBundle --init

The above command will create the Features folder with a FeatureContext class inside it. 上面的命令将创建Features文件夹,其中包含FeatureContext类。 Put in this class the methods of your scenario. 在此类中添加方案的方法。 Below the official hello world example: 官方hello世界示例下面:

/**
 * @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: 现在,您必须在Features文件夹中创建要素文件(来自同一示例的ls.feature ):

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: 因此,您的Features文件夹将如下所示:

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

Finally, launch behat and enjoy! 最后,推出behat并享受!

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. 运行symfony所需的依赖关系是behat,symfony2-extension,mink和drivers,你可能需要格式化程序才能获得所需的输出。

Then you need to configure the behat.yml for drivers, session and browser and to name test suites. 然后,您需要为驱动程序,会话和浏览器配置behat.yml并命名测试套件。

Once this is done you need to add your feature and FeatureContext to write the test scenarios and to define custom steps. 完成此操作后,您需要添加功能和FeatureContext以编写测试方案并定义自定义步骤。

I have added a details configurations with my symfony installation with behat+mink+selenium+symfony here . 在这里使用behat + mink + selenium + symfony添加了我的symfony安装的详细配置。 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. 我不确定我是否完全理解你想要实现的目标,但是你试图实现Symfony2项目的不同配置这一事实让我觉得你应该使用自定义环境。 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. 例如,您可能想要设置一个“behat”环境,该环境具有自己的配置文件( /app/config/config_behat.yml ),并且可以使用您的behat测试中的/app_behat.php/进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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