Skip to content

Running Behat Tests

This guide will cover how you can run tests using parameters.

Note

Before doing any of this, you will need to make sure selenium is running. Open a terminal and use the following command lxc-selenium "ADD-CONTAINER-NAME-HERE"

Running tests with behat, you have quite alot of freedom and control when running the tests. This can range from running a single test to running the entire test suite.

TL;DR: To simply run the tests all you need to do is run the following from the command line php bin/behat -p chrome "PATH-TO-FEATURE-FILE" also do this from inside your testing folder

These are some of the different methods:

  • Tags
  • Profiles
  • Folder Path
  • Path to feature file
  • Single Scenario

Running in PHP Storm

As of Behat version 3.4.x there is a bug that prevents running individual tests in PHPStorm's behat integration. The solution is to set the testing defaults for Behat with the working directory set to the same directory that hosts your behat.yaml file.

Source

Tags

Working with a rather large test suite it can be difficult to keep track and test the specific areas you want, this is where tags come in. Tags allow you group certain tests together and this then makes it so you can just run them tests.

Note

Make sure you replace the text within the ""

Tags use the following format: @"Tag Name"

When you want run the tests using this new tag simply add --tags="tag name" into the command.

Example: php bin/behat --tags=mobile "path/to/FeatureFileOrFolder"

Profiles

By defining a profile when running your test, you give yourself the ability to specify what the test will run and open on. This can range from chrome (desktop) to running on mobile.

When you want run the tests using this new tag simply add --profile="Plaform" into the command.

Example: php bin/behat --profile="platform" "path/to/FeatureFileOrFolder"

Scenarios

When you get to the end of the path to run the test, all you need to do it add :"Scenario Line Number". This will make it so you can run the file from that specific scenario.

*.feature:22

Also if you do the following it will make it so you can run a set range of tests within the same folder

*.feature:22-33

Folders

Can be used to create definition outlines (They will be placed into the FeatureContext)

vendor/bin/behat --dry-run --append-snippets

Set the tests to run a set amount of times

for i in {1..10}; do php bin/behat --profile=chrome_nexus10 features/GuestHardening/GuestCheckout/DisableGuestCheckout.feature:42; done

Make tests infinitely run

while true; do php bin/behat --profile=chrome_nexus10 features/GuestHardening/GuestCheckout/DisableGuestCheckout.feature:42; done

Note

If you seem to be struggling to stop the loop from running try the following in the terminal CTRL + Z this should stop the loop from running, when it has finished you will also need to make sure you run this aswell to kill the process completely kill %1

Isolating Tests

It may sometime be useful to isolate certain tests to keep things consistent and running smoothly.

Use case:

If a test finishes on a new domain i.e paypal then the test that follows will not have the session reset and looks like it continues on from the session left over by the previous test.

This can definitely be a problem especially if you use a Background for your tests as our issue came from our I am logged in step. which should log us in at the start of every test but due to the use case above it remained logged in and ultimately failed when it came back to that step.

Isolation Method

Using the following tag will isolate any test it is applied to and will start a new session.

@insulated This tag will cause Mink to stop current sessions before scenario
instead of just soft-resetting them

It will be worth putting this on any test that follows a domain switch.

Note

Need to extend to add different methods for isolating tests

Running tests on Desktop, Mobile and Tablet (Bash Files)

When creating any test it is important to take into account the other types of devices people might be using to visit the site on.

Bash files have been created to make the process quicker for running the tests on each device a customer might use and you can get to the files via the following path:

cd testing/bin from in side the root of the project

When you are there you should see the following bash files:

behat_all.bash
behat_desktop.bash
behat_mobile.bash
behat_tablet.bash
Each of the above files will run all the tests in the suite unless you specify exactly what you would like to test, see example below:

bash bin/behat_all.bash PATH/TO/FEATURE/FILE/OR/FOLDER

Note

behat_all.bash will run the tests on each device if you need to check them all at once in a single run through