Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Jani Hartikainen:
What’s the difference between Unit Testing, TDD and BDD?
Mar 02, 2015 @ 09:16:28

On his site today Jani Hartikainen has a new post helping to demystify some potential confusion around unit testing, test-driven development and behavior-driven development. He's talking about them in the context of Javascript in this case, but the fundamentals transfer to just about any other language, including PHP.

When you’re just getting started with automating your JavaScript testing, there’s a lot of questions. You’ll probably see people talk about unit testing, TDD or Test-Driven Development, and BDD or Behavior-Driven Development. But which one of them is the best approach? Can you use all of them? I’ve talked to a number of JavaScript developers, and there seems to be some confusion about all this. So, let’s take a look at Unit testing, TDD and BDD, and fix some of the common misconceptions about them out there.

For each he provides an overview of this basic concepts and a bit of sample code showing it in action. For TDD (test-driven development) there's not really a way to show it specifically in code as it's more of a practice. Instead he gives a "checklist" to follow when practicing it.

Unit Testing gives you the what. Test-Driven Development gives you the when. Behavior Driven-Development gives you the how. Although you can use each individually, you should combine them for best results as they complement each other very nicely.
tagged: bdd unittest tdd testdrive behaviordriven development introduction

Link: http://codeutopia.net/blog/2015/03/01/unit-testing-tdd-and-bdd/

Understanding PhpSpec
Sep 04, 2014 @ 11:09:42

The NetTuts.com site (well, TutsPlus) has posted a new tutorial that gets you more intimate with PhpSpec, a PHP-based testing tool that lets you define tests as specifications and using behavior-driven development principles. If you need an introduction to the tool, check out this other tutorial first.

If you compare PhpSpec to other testing frameworks, you will find that it is a very sophisticated and opinionated tool. One of the reasons for this, is that PhpSpec is not a testing framework like the ones you already know. Instead, it is a design tool that helps describing behavior of software. A side effect of describing the behavior of software with PhpSpec, is that you will end up with specs that will also serve as tests afterwards. In this article, we will take a look under the hood of PhpSpec and try to gain a deeper understanding of how it works and how to use it.

They provide a quick overview of some of the internals of the PhpSpec tool and a brief look at the difference between BDD (behavior-driven) and TDD (test-driven) development practices. There's also a look at how the tool differs from the popular PHP testing tool PHPUnit. Code examples are provided through out the post with simple tests, making the separation between the methods and tools easier to follow.

tagged: tutorial phpspec testing tool bdd tdd unittest behaviordriven development

Link: http://code.tutsplus.com/tutorials/understanding-phpspec--cms-21915

Getting Started With Phpspec
May 12, 2014 @ 13:55:10

The NetTuts.com site has a new tutorial that want to help get you started with PHPSpec, the PHP-based tool to help with behavior-driven development.

In this short, yet comprehensive, tutorial, we'll have a look at behavior driven development (BDD) with phpspec. Mostly, it will be an introduction to the phpspec tool, but as we go, we'll touch on different BDD concepts. BDD is a hot topic these days and phpspec has gained a lot of attention in the PHP community recently.

They briefly introduce behavior-driven development (BDD) and where the PHPSpec tool fits into the picture. They guide you through installation of the tool (via Composer) and some configuration updates to get things set up. From there, they help you write your first spec, a test to see if a "TaskCollection" can be created. They build on this simple test adding in checks for adding tasks, making the collection countable and working with expectations and promises. The last part of the tutorial mentions how to make custom matchers like "beTrue" and "beFalse" and the output of the full test set.

tagged: phpspec tutorial introduction behaviordriven development bdd

Link: http://code.tutsplus.com/tutorials/getting-started-with-phpspec--cms-20919

Gonzalo Ayuso:
Building a BDD framework with PHP
Aug 19, 2013 @ 09:49:57

<p. Gonzalo Ayuso wanted to look into BDD (behavior driven development) in PHP and was looking around for a tool to fit his needs. He didn't find one right away and so decided to create a simple one as a proof of concept to try out the method for himself.

I want to write as less code as I can (it’s only a proof of concept), so I will reuse the assertion framework or PHPUnit. As I’ve seen when studying Behat, we can use the assertion part as standalone functions. We only need to include vendor/phpunit/phpunit/PHPUnit/Framework/Assert/Functions.php file.

He includes the sample code showing his basic interface - a string calculation object being passed into a closure for evaluation by the PHPUnit assertion methods. He also includes an example of using it along with Mockery for creating a mock object and testing based on that.

tagged: bdd behaviordriven development framework proofofconcept poc mockery

Link: http://gonzalo123.com/2013/08/19/building-a-bdd-framework-with-php

Francesco Tassi:
Switching From TDD to BDD With Behat and Symfony2
Aug 16, 2013 @ 13:27:32

In this new post to his site Francesco Tassi shares his experience making the switch from doing just test-driven development on his Symfony2-based applications to behavior-driven development using Behat, Mink and PHPSpec.

Recently I started a small extra project with a smart fellow from my local PHP User Group, since the project was quite simple and both of us were willing to learn something new, we decided to give BDD a try. In the PHP world BDD means Behat, Mink and PHPSpec. [...] In this post I won’t cover all the details about BDD, Behat or PHPSpec, I’d rather describe how I switched from PHPUnit and TDD to BDD (and also show some bits of code).

He talks about the switch in mindset that comes along with adopting BDD and how you think about testing - not what it does, but what it should do. He talks some about test organization, configuration and his process for executing them. There's a bit about working with databases (with fixtures and @BeforeScenario) including the code for the feature file to make it happen.

tagged: tdd testdriven bdd behaviordriven behat symfony2 tutorial

Link: http://www.ftassi.com/blog/2013/08/12/switching-from-tdd-to-bdd-with-behat-and-symfony2

Webinar: Behavior Driven Development with Behat
Apr 17, 2013 @ 10:44:21

The QaFoo folks have posted information about a webinar they're putting on for those interested in using Behat for doing functional testing on their applications:

I've already written two blog posts here about Behat: Behavior Driven Development and Code Coverage with Behat. If that made you curious or you wanted to learn about Behat anyway, I can highly recommend to join the free webinar on Behavior Driven Development with Behat I'll be giving on May 8th 2013 on behalf of Qafoo in cooperation with Zend.

As mentioned, the webinar is free to attend, but you'll need to sign up to be able to attend.

tagged: webinar behat testing functional tool behaviordriven development

Link: http://qafoo.com/blog/042_webinar_bdd_behat.html

Behavior Driven Development
Mar 08, 2013 @ 11:54:21

On the QaFoo blog today there's a new post looking at behavior driven development and a PHP-based tool that makes implementing it in your workflow simpler (Behat).

While unit, integration and system tests - especially combined with the methodology of Test Driven Development (TDD) - are great ways to push the technical correctness of an application forward, they miss out one important aspect: the customer. None of these methods verify that developers actually implement what the customer desires. Behavior Driven Development (BDD) can help to bridge this gap.

The introduce some of the basic concepts behind behavior driven development and include an example of a Gherkin-formatted test example checking a page to ensure if has the correct content. They briefly define the structure of the test then take it into a Behat context and show how it would be implemented.

Of course, the examples shown above are only very rudimentary, missing e.g. variables and other advanced features. However, they should have explained what BDD is all about: Communication
tagged: behaviordriven development behat introduction rationale


Major Codeception Update (BDD in PHP)
Aug 08, 2012 @ 11:09:11

The Codeception project (BDD testing in PHP) has gotten a major update to its feature set and has bumped up to v1.1:

Many core classes were refactored to solve the common issues and reduce the level of dark magic inside. Only white magic left. And that's really cool 'cause you don't need to study to source code to implement your custom hooks now. Codeception is rapidly evolving to be the professional testing tool, ready to use by testsers and developers through out the PHP world.

Improvements in this large update include an update to the method of test execution, "grabbers" (helper methods that can extract content from tests), the introduction of XPath support to locate items in the content pulled into the test and the integration with unit testing. Upgrade instructions are also included.

tagged: bdd behaviordriven codeception update


Chris Hartjes' Blog:
Simple API Testing Using Behat
Feb 27, 2012 @ 10:09:18

Chris Hartjes has taken some of his first steps to testing a web service with the help of the Behat BDD testing framework. In this new post he shares some of that knowledge and a bit of code to help guide you if you'd like to get started yourself.

I have been trying to push my JavaScript skills lately by first playing around with Node.js to get a feel for event-driven server-side coding and by trying to build something semi-useful using Knockout. [...] After working with PHPUnit's "assert that the result matches expectations" philosophy, switching to using a behavioural approach has changed how I decide to approach actually testing something.

He shows how to define a "scenario" in a more recognizable sort of English. His example is testing a web service to ensure of a few things:

  • the response is JSON
  • it contains at least one transaction
  • the first transaction has an ID, contains two teams and has a description

Included in the post is the resulting Behat test code (generated and modified with his testing details) and an example of the execution of the test.

I think that the BDD approach maps better to a results-oriented methodology when it comes to testing: you are using real-life scenarios to make sure that you are getting back the results you are expecting. In the end. that is still the goal of testing.
tagged: simple api test behaviordriven behat example slim framework


Codeception Blog:
Introduction to Codeception
Jan 25, 2012 @ 10:19:59

On the Codeception blog there's an introduction to the Codeception tool, a behavior-driven framework written in PHP (in the spirit of Behat).

How often do you see PHP projects with no line of test written? From my experience, this situation happens quite often. We should state the unpleasant fact that tests are not so popular around the PHP world. Surely, the advanced developers with 5+ years of experience in PHP and other programming languages understand importance of testing and PHPUnit usage. But juniors and seniors are just skipping testing and, therefore, produce unstable web applications.

A code example of the framework in use is included in the post - a test on a class using methods like "wantTo", "amOnPage", "fillField" and "click". The installation is pretty simple and it gives you a quick way to start working on acceptance, functional and unit tests. In BDD fashion, the tests are written in a more "natural language" than traditional unit tests making them easier for the non-developers (like QA) to write. Their example loads a page, fills in some form fields and submits the form to check the result.

tagged: behaviordriven development codeception testing tutorial functional