Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajaxray/52251bce4a7e82ac5e36 to your computer and use it in GitHub Desktop.
Save ajaxray/52251bce4a7e82ac5e36 to your computer and use it in GitHub Desktop.
Copy-Peste steps to Up and running behat with Symfony2

Up and running with behat with Symfony2

This is for someone who is good at Symfony2 and have the basic idea about behat. So not explaining things much. Just follow the steps and your Symfony2 project should be ready for testing (with running example bonus)!

  1. Add dependencies
sudo composer require --dev --prefer-dist "behat/behat:^3.0" "behat/symfony2-extension:~2.0" "behat/mink:~1.5" "behat/mink-extension:~2.0" "behat/mink-selenium2-driver" "behat/mink-browserkit-driver:~1.1"
  1. Add behat.yml file in project root with following content
default:
  extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
      selenium2: ~
      sessions:
        default:
          symfony2: ~
      base_url: http://www.your-site.tld # <== Replace with appropriate value
      
  suites:
    your_suite_name:                     # <== Replace with whatever
      type: symfony_bundle
      bundle: YourCoolBundle             # <== Replace with appropriate value
  1. Add in your default controller
    /**
     * @Route("/_status", name="status")
     */
    public function statusAction()
    {
        return new JsonResponse(array('status' => 'SUCCESS'));
    }
  1. Initiate things with bin/behat --init --suite=your_suite_name
  2. Extend Features\Context\FeatureContext class of your bundle from Behat\MinkExtension\Context\MinkContext
  3. Add a feature file status.feature in /src/Your/Bundle/Features/ with -
Feature: Site Status

  Scenario: Site loading successfully
    Given I am on "/_status"
    Then the response should contain "SUCCESS"
  1. Run from project root dir -

    • bin/behat to execute all tests
    • bin/behat "@YourCoolBundle" to execute all tests of a bundle
    • bin/behat "@YourCoolBundle/status.feature" to execute a feature
  2. Get busy with writing real features and enjoy!


Extra Fun

To Test features that depends on javascript -

  1. sudo composer require --dev --prefer-dist "behat/mink-selenium2-driver"
  2. Install and Run Selenium standalone server from http://www.seleniumhq.org/download/
  3. Add @javascript tag with scenario -
  @javascript
  Scenario: Site loading with OK status
    Given I am on "/_status"
    Then the response should contain "SUCCESS"

Now, if everything else is OK, a browser should open while running scenario and yu should see things happening magically!

Resources

  1. https://github.com/Behat/Symfony2Extension/blob/master/doc/index.rst
  2. http://docs.behat.org/en/latest/cookbooks/1.symfony2_integration.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment