PHPUnit & Behat; complementing or alternatives?

qrazi picture qrazi · Feb 12, 2013 · Viewed 12.9k times · Source

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?

Answer

Jop van Raaij picture Jop van Raaij · Aug 10, 2013

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.