Home » Php » php – PHPUnit & Behat; complementing or alternatives?

php – PHPUnit & Behat; complementing or alternatives?

Posted by: admin April 23, 2020 Leave a comment


I have been looking around SO and Google, but I couldn’t really get a definitive answer.

PHPUnit is a framework for unittesting, like JUnit. I use it, also in combination with the Selenium-extension for functional testing. When browsing around I see Behat/Mink keeps on getting mentioned. But I do not completely understand how Behat fits in here.

With Behat you write scenarios in human-readable format. Behat can then translate that into skeleton classes for a new project? But does it also provide skeleton classes for unittesting? Do you write unittests using Behat, or you would use PHPUnit / SimpleTest for those?

But then Behat/Mink does replace PHPUnit_Selenium-extension for functional testing?

Do you use Behat only for new projects, or can it also be adapted to existing projects?

How to&Answers:

Yes, I would consider Behat to be an alternative to phpUnit, both for general code testing and for functional testing using browser automation.

For general testing, phpUnit is a more well-established product, while Behat is focussed on tests written using “BDD” methodology (Behaviour Driven Design). phpUnit does also have BDD features, but Behat is much more targetted toward that style.

Since we’re talking about functional browser tests rather than unit tests, you probably aren’t thinking about some of the more powerful unit testing features that they have; you just want to write the browser automation. For this, you really can pick whichever of the two suits you the best.

There are subtle edge-cases where each of them has strengths over the other, but for most cases they’re both capable of running test scripts across a variety of browsers.

My preference would go to Behat/Mink, on the grounds that it is more flexible; it can support several browser automation engines, including Selenium and Sahi, which means that if you hit an issue with one, you can switch your tests to the other and carry on. We had this exact situation, trying to get some Mink test scripts to work in Selenium; switching to Sahi made it work, and involved changing just a single line of test code.

It’s also worth adding that Mink can be used on its own without Behat, if you don’t want to write BDD style tests.


I would consider Behat to be complementary to PHPunit. I mean complementary by using Behat for testing behavior, PHPUnit for testing code. Using BDD/TDD, one would first write a test based on a user-story. To make the test pass, you would typically write several PHPUnit tests using TDD to create the application which will be able to pass the BDD test.

BDD should test behavior and be unaware of the code of the application, like entities/controllers/interfaces. You should be able to swap out the complete application with another one and still pass all tests when the behavior of the application is the same.

TDD, besides other things like your mind, should guide your structure of classes and interaction between them. Besides that, they prove your methods do what they should do, makes the intent clear and makes it possible to refactor without fear.


seems to be all wrong with this answer.

phpunit is unit testing, and by that it adheres to TDD.

phpspec and behat is a functional testing, so it’s BDD.

they complement each other, a perfect scenario would be a project where you have both type of testings, but this could restricted only to big projects that can afford to do that.

TDD test single or just a couple of classes.
BDD test a complete feature across multiple modules in your application.