32

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?

qrazi
  • 1,376
  • 1
  • 14
  • 17

3 Answers3

37

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.

Jop van Raaij
  • 643
  • 1
  • 6
  • 10
  • 3
    There's nothing about BDD that specifies the level. You could be testing the behavior of a unit. In that sense, BDD could apply to unit testing. – Greg Bell Jan 11 '17 at 23:20
14

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.

SDC
  • 13,745
  • 2
  • 31
  • 48
  • So, for an existing application that was made without any consideration for BDD, you would use PHPUnit? And for a fresh project, using BDD, prefer Behat? – qrazi Feb 12 '13 at 11:29
  • The preference I stated for Mink was mainly answering the question of writing functional browser test scripts due to its flexibility. I did mention the BDD aspect, but I didn't really take that into account for the answer. For myself, I'm using phpUnit for unit testing and Mink/Sahi for functional testing. (but we're considering moving our functional tests to Javascript, to reduce the number of layers of software between the test script and the browser) – SDC Feb 12 '13 at 11:49
  • 4
    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. TDD test single or just a couple of classes. BDD test a complete feature across multiple modules in your application. – useless Aug 13 '13 at 19:52
7

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.

useless
  • 1,803
  • 17
  • 16
  • Phpspec is not for unit testing. As you can see in their website is for BDD https://www.phpspec.net/en/stable/manual/introduction.html – Eleftherios Nov 07 '19 at 11:48