# Behat on Drupal – clearing Data Between Scenarios

> A very important, but often overlooked, part of a software development process is testing. This article cover a common issue developers faces with Behat. How and why should you ever start scenario with a predictable database.

Source: https://antistatique.net/en/blog/behat-on-drupal-clearing-data-between-scenarios

Category: Development

# Behat on Drupal – clearing Data Between Scenarios

Author(s)

[

![Kevin](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Kevin-300x300.jpg)



](/en/agence/kevin)

[Kevin](/en/agence/kevin)11 November 2021

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:

-   [An Introduction to Automated Testing with Beha](https://webservices.uchicago.edu/learningcenter/article/a_n_introduction_to_automated_testing_with_behat/)
-   [Testing Your Drupal Website just got easier with Behat (A comprehensive tutorial)](https://www.specbee.com/blogs/testing-your-drupal-website-just-got-easier-behat-comprehensive-tutorial)
-   [Drupal 8/9 and Behat tests](https://gorannikolovski.com/blog/drupal-89-and-behat-tests)

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](https://medium.com/@tasdikrahman/f-i-r-s-t-principles-of-testing-1a497acda8d6).

Let's assume we have **2 features** and we want to make sure that the whole process did not break.

-   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.

-   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](https://medium.com/@tasdikrahman/f-i-r-s-t-principles-of-testing-1a497acda8d6).

> “ 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!"
```

## [](https://symfonycasts.com/screencast/behat/clear-data-symfony-extension#tab-comment)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](https://sites.google.com/site/unclebobconsultingllc/the-truth-about-bdd).

**Ryan Weaver & Saša Stamenković** (3 September 2014). Behat Basics (Part 1).  
See on [youtube.com/watch?v=j8TGm4qWebw](https://www.youtube.com/watch?v=j8TGm4qWebw).

**Ryan Weaver & Saša Stamenković** (3 September 2014). Behat Basics (Part 2).  
See on [youtube.com/watch?v=IopLRcryB8A](https://www.youtube.com/watch?v=j8TGm4qWebw).

****SymfonyCasts**** (01 November, 2021). The SymfonyExtension & Clearing Data Between Scenarios.See on [symfonycasts.com/.../behat/clear-data](https://www.wojtek.ch/advertising/ad-operations/why-ip-location-targeting-is-not-accurate/)[...](https://symfonycasts.com/screencast/behat/clear-data-symfony-extension)

**Michael Lihs** (19 Juilly 2015). Behavior Driven Development with Behat.  
See on [lihsmi.ch/showoff-presentation-behat](https://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](https://netgen.io/blog/how-to-test-with-behat)

## We have the skills to make your [Drupal](https://antistatique.net/en/services/drupal) project a success

## More articles

Author(s)

[

![Anthony](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2024/11/anthony-1-300x300.png)



](/en/agence/anthony-salamone)

[Anthony](/en/agence/anthony-salamone)28 September 2026

### [Smashing Conference Freiburg 2026](/en/blog/smashing-conference-freiburg-2026)

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

[

![Smashing Conference Freiburg 2026 poster](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/assets/blog/smashing-2026.png)



](/en/blog/smashing-conference-freiburg-2026)

Author(s)

[

![Akira](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2025/03/akira-small-300x300.jpg)



](/en/agence/akira-kamata)

[Akira](/en/agence/akira-kamata)22 July 2026

### [Making of : “La nuit des Losers”](/en/blog/making-of-la-nuit-des-losers)

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

[

![Making of : “La nuit des Losers”](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/05/La-nuit-des-Losers-Teaser-1-768x576.png)



](/en/blog/making-of-la-nuit-des-losers)

Author(s)

[

![Pierre](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Pierre-300x300.jpg)



](/en/agence/pierre)

[Pierre](/en/agence/pierre)7 July 2026

### [Dark mode: necessity or trend](/en/blog/dark-mode-necessity-or-trend)

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

[

![Dark mode: necessity or trend](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/07/1-768x384.png)



](/en/blog/dark-mode-necessity-or-trend)
