What's the state of TDD and/or BDD in PHP? [closed] What's the state of TDD and/or BDD in PHP? [closed] php php

What's the state of TDD and/or BDD in PHP? [closed]


There are at least two mature, stand-alone, JUnit style test suites available, named PHPUnit and SimpleTest, respectively.

As far the MVC Frameworks go, Symfony has its own testing framework named lime, Code Igniter has a unit_test library and CakePHP relies on the aforementioned SimpleTest.

I know that Zend Studio has built in support for PHPUnit tests, and both PHPUnit and SimpleTest have command-line runners so integration into any workflow is possible.

The tools are there in the PHP world if a developer wants to take advantage of them, and smart shops do take advantage of them.

The caveats are your par for the course PHP complaints. There are two PHP communities; PHP as a platform for building software, and PHP as a way to interact with a web server, web browser, and database to produce application-like things on the web. It's less a black and white thing and more a continuum; Among those more on the software developer side unit testing and TDD is supported and used as much as it is on any other platform. Among the "cobble together a bunch of stuff I don't understand but still get results people", it's unheard of.

There's a lot of non-framework/custom-framework legacy PHP code around that's difficult to get a useful test harness around. PHP also lends itself easily to patterns that rely on the existence of a browser environment to run. I don't have any evidence to back this up other than my own observations, but a lot of PHP shops that care about testing end up relying on acceptance testing (i.e. Selenium) as a substitute for actual Unit Testing, test-first, etc. development.

In your specific situation, interview the hell out of the developer your group is going to hire.

  1. Ask them what unit testing framework they use

  2. Ask them to describe, in general terms, a real world example of a time they developed a new feature and its supporting tests

  3. Ask them to describe, in general terms, a real world example of a time their tests failed and what they did to resolve the situation

You're less interested in the specific situation they're going to describe and more interested in how comfortable they are discussing their knowledge of code testing in general.


Whenever I TDD a project with XUnit style tools, I have difficulty getting my head in the right spot. I find that using tools designed for Behaviour Driven Development or "Specification by example" makes it easier for me to do TDD right -- i.e. focus on design, exposing intent and describing behaviour in specific contexts. Not testing.

That said, I would like to introduce pecs into the conversation. From the readme on the project site.

pecs is a tiny behavior-driven development library for PHP 5.3, a la RSpec or JSpec.

If you've used JSpec or better yet, Jasmine-BDD (for JavaScript) the pecs style of describing behaviour should be really familiar. I find this style great for component level specs. If you are looking for a PHP tool for feature level specifications (stories or user acceptance tests) consider Behat.

Going back to pecs, here's an example culled from the pecs project site:

describe("Bowling", function() {  it("should score 0 for a gutter game", function() {    $bowling = new Bowling();    for ($i=0; $i < 20; $i++) {      $bowling->hit(0);    }    expect($bowling->score)->to_equal(0);  });});

Yes that is a PHP spec. Looking through the pecs source, it looks like the author is able to pull this off by leveraging the new hotness in PHP 5.3+, Lambdas and closures. So I guess this means that you cannot use pecs in any project based on PHP < 5.3 (just FYI).

Also, pecs is not as mature as PHPUnit or SimpleTest. However, I think the proponents of BDD in the PHP community should support the growth of tools like pecs that encourage "Specification by example" or BDD without the confusion brought on by having to use legacy XUnit testing tools.

These days I work more in Python than PHP. However, the next time I pick up a PHP project, I'll be extremely happy if I have a mature, community supported tool like pecs to craft the specifications for the software.


I have had an amazing experience with Behat / Mink http://behat.org

I agree with others php as a unit testing platform is not a fun or experience BDD is the best way to go if you are using any php framework

Wrapping my head around composer as a repo build tool was the biggest stumbling block but we were able to use Behat Mink Selenium Webdriver standalone server jar as an amazing design and regression testing tool. We used to run our regression suite against our CakePHP application on a Jenkins server but it proved to be not so very "fail fast" enough

Now our workflow goes like this:Create story in gherkinrefine storywrite feature and stub out any new step defsbegin coding php solution to testThen at the end we have a working feature or bug fix with a bdd test covering it

We setup an Ubuntu VM with a working Behat setup and copied it to every workstation. We baked it into our process. We just pull down changes run tests then begin coding new stuff.

We wrote a shell script to automatically run mysql dumps and load them before each feature which has made refactoring code a breeze.

The Mink WebAssert class gives you all the assertions you need to validate behaviorThe regular session / CommonContext classes are great for using css or xpath.

I have used Capybara / WebDriver with Java and Rails projects before and found the setup overhead / learning curve is too high compared to Behat.