Behat on Drupal – clearing Data Between Scenarios

Auteur(s) de l'article

A very important, but often neglect, part of a software development strategy is testing.
The thing is, if you don’t test your website, it could happen that you deploy the product and don’t notice any existing bugs until the production. That would be a very painful experience both for you and your client.
Here is where Behat comes in handy.
Well ... we all know that feeling
Here in this article, I will not teach you how to use Behat and create scenarios. There already is plenty of resources on the Internet about that:
This article covers a more common issue developers faces with Behat. How and why should you ever start a data-driven scenario with a predictable database.

The problem with Database-Sensitive Tests

What happens if tests change database content? How to assert database consistency? How to handle database dumps in your tests? Let's answer all those questions together!
You should start every scenario with a blank database.

Ryan Weaver

Well, that's not 100% true. What I want to say is "You should start every scenario with a predictable database".
When working with Behat, the database is not cleared between the scenarios (excepted when explicitly requested, as we will see later). Meaning any changes occurring in any previous scenario will impact future ones, breaking the F.I.R.S.T principles of testing.
Let's assume we have 2 features and we want to make sure that the whole process did not break.
  1. A simple registration form
Scenario: Fill the customer registration form
  Given I am on homepage
  When I follow "Customer registration"
  Then I should be at "/customer-registration"
  And I should see "Welcome to customer registration page!"
As you may already see, a the end of the first scenario the database's state has been changed, a new user has been registered.
  1. A listing of registered user
Scenario: Get the list of registered user
  Given I am on homepage
  When I follow "Registered custom"
  Then I should be at "/customer-list"
  Then I should see 2 "#customer-list .customer" elements
The second scenario is influenced by the first one. Running the assertion of Get the list of registered user before Fill the customer registration form may break because the number of listed customers is inconsistent. This violates the F.I.R.S.T principles of testing.
In short, for any given scenario, it should be independent of everything else so that it results is not influenced by any other factor.

A wise developer

Let's see how we can avoid this trap.

How to implement Scenario Database Isolation on Behat

First of all, not all your scenario will alter the database's state. That's why I will give you a smarter Context Annotation that you should implement in per-scenario cases, in order to keep your database predictable and to avoid slowing down your tests suits (dumping database on each scenario may have a real negative effect, especially when not necessary).
The following Context creates a snapshot before running the scenario and restores it after the scenario is completed, ensuring a clean database.
In order to load your new contexts in your test suite, you need to fine-tune the suite configuration inside behat.yml :
Then, you will need to tag scenarios with @preservDatabase to restore the database once the scenario is completed.
@preservDatabase
Scenario: Fill the customer registration form
  Given I am on homepage
  When I follow "Customer registration"
  Then I should be at "/customer-registration"
  And I should see "Welcome to customer registration page!"

Conclusion

This was a simple Context sharing for Behat with Drupal.
Behat has many advantages that fit all those who are included in the codding process, from developers themselves over project managers to clients.
It enables you to run the Features all over again. There is no need to manually go over the website and click around.
So, test with Behat and sleep well at night.

Sources

For the most curious of you, here are some sources of additional information that inspired the creation of this article.
Robert C. Martin (27 January 2008). The Truth about BDD.
See on sites.google.com/.../.../the-truth-about-bdd.
Ryan Weaver & Saša Stamenković (3 September 2014). Behat Basics (Part 1).
See on youtube.com/watch?v=j8TGm4qWebw.
Ryan Weaver & Saša Stamenković (3 September 2014). Behat Basics (Part 2).
See on youtube.com/watch?v=IopLRcryB8A.
SymfonyCasts (01 November, 2021). The SymfonyExtension & Clearing Data Between Scenarios.
See on symfonycasts.com/.../behat/clear-data...
Michael Lihs (19 Juilly 2015). Behavior Driven Development with Behat.
See on lihsmi.ch/showoff-presentation-behat
Mario Blažek (18 December 2015). How to test with Behat.
See on netgen.io/blog/how-to-test-with-behat