PHPUnit & Behat; complementing or alternatives?
Asked Answered
L

3

35

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?

Lucier answered 12/2, 2013 at 8:17 Comment(0)
C
15

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.

Castera answered 12/2, 2013 at 11:20 Comment(4)
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?Lucier
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)Castera
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.Breadstuff
Behat is not an alternative, it's a complementary approach to testing which should be done in combination with your unit tests.Carpous
B
44

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.

Beyer answered 10/8, 2013 at 18:17 Comment(1)
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.Winna
C
15

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.

Castera answered 12/2, 2013 at 11:20 Comment(4)
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?Lucier
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)Castera
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.Breadstuff
Behat is not an alternative, it's a complementary approach to testing which should be done in combination with your unit tests.Carpous
B
9

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.

Breadstuff answered 13/8, 2013 at 19:55 Comment(3)
Phpspec is not for unit testing. As you can see in their website is for BDD phpspec.net/en/stable/manual/introduction.htmlFescue
This is the correct response.Carpous
@Fescue They said that PHPSpec is BDD :)Carpous

© 2022 - 2024 — McMap. All rights reserved.