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)!
- 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"
- 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
- Add in your default controller
/**
* @Route("/_status", name="status")
*/
public function statusAction()
{
return new JsonResponse(array('status' => 'SUCCESS'));
}
- Initiate things with
bin/behat --init --suite=your_suite_name
- Extend
Features\Context\FeatureContext
class of your bundle fromBehat\MinkExtension\Context\MinkContext
- 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"
-
Run from project root dir -
bin/behat
to execute all testsbin/behat "@YourCoolBundle"
to execute all tests of a bundlebin/behat "@YourCoolBundle/status.feature"
to execute a feature
-
Get busy with writing real features and enjoy!
To Test features that depends on javascript -
- sudo composer require --dev --prefer-dist "behat/mink-selenium2-driver"
- Install and Run Selenium standalone server from http://www.seleniumhq.org/download/
- 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!