Test-Driven Development (TDD) and Behaviour-Driven Development (BDD) in PHP

- Fire development

TDD and BDD PHP tools

New to agile project management and extreme programming? Then, it is time to get acquainted with test-driven development (TDD) and behavior-driven development (BDD) in PHP. Both are intended to improve the quality of your projects, as well as their responsiveness to future requirements. Below, I will explain how to implement TDD and BDD tests in PHP and show where to get all the necessary tools for these methodologies. Let’s start with some basics.

Test-Driven Development (TDD)

TDD is based on the repetition of a short development cycle: you create an automated test case to define a new function, write the minimum amount of code for passing it, and refactor the new code according to acceptable standards. As you can see, the methodology is quite simple and unobtrusive, at the same time it is very effective for making your projects stable and responsive. By focusing on producing the code necessary to run tests successfully, test-driven development oppens lots of opportunities for making your code cleaner and clearer than in case of other approaches.

While working with some advanced design concept, for instance, a design pattern, tests are often written to generate that exact design. Thus, the code is often less complicated than the target pattern, but it doesn’t prevent it from passing all tests. If such approach seems unreliable, think about the ability to focus only on what is important for your project, as of TDD’s core concept.

Writing tests before functions you are going to test has many benefits. Firstly, you get an app written for testability. Secondly, you get tests for all app’s features. Thirdly, you get deeper insights into product requirements, increasing the quality of your software.

Despite the first TDD tests are often uncompiled, since some required classes and methods may not yet exist, they function as the beginning of an executable specification.

Since another important principle of test-driven development is “red/green/refactor,” you are bounded with adding failed test cases, passing them, and refactoring everything. It is also necessary to mention that each test case fails initially to ensure that it really works and is able to catch an error.

Behavior-driven development (BDD)

In its turn, behavior-driven development (BDD) is a derivative of test-driven development. It combines the general principles of TDD with some core ideas of domain-driven and object-oriented design. You write human-readable sentences to describe your app features, implement them in software, and run the test which is based on these human-readable sentences.

The core difference between the aforementioned software development methodologies is in the lack of specification within TDD: this approach doesn’t state what should be tested and how. As for BDD, it offers more specific choices, since tests of any software unit should be specified according to the desired behavior of each specific unit.

Under “the desired behavior” you should understand the set of business requirements (the approach is borrowed from agile software development). Therefore, BDD is about software development managed by technical insights and business interests. The approach utilizes some special tools, which are also used in test-driven development. Since the ubiquitous language is a core feature of BDD, these tools are aimed at automating your interaction with a testing environment.

Test-Driven Development (TDD) and Behaviour-Driven Development (BDD) in PHP

Now, when you understand the basic concepts behind TDD and BDD, I can tell about their implementation in PHP. Below, you will find the most reliable frameworks and tools for running TDD and BDD tests for PHP projects.

Behat

Behat is an open source BDD PHP framework, which utilizes human-readable sentences for running tests against software. If you are familiar with the Cucumber project, you will easily cope with Behat; if not, it also won’t be difficult to master the framework. Behat is a StoryBDD tool. It means that the tool is aimed at understanding the domain. Behat helps to specify feature narratives, their needs, and what you mean by them. It is not the only solution for the implementation of BDD in PHP, but it has a huge comunity and a unique approach to testing.

TDD and BDD PHP tools

PHPUnit

PHPUnit is a programmer-oriented unit testing framework for PHP, based on the idea that coders should find mistakes in new projects quickly without code regression in other parts of the application. Using PHPUnit, you can easily verify if every tested unit behaves as expected. To find out more about TDD in PHP with PHPUnit, check this official documentation. You can read everything about BDD in PHP with PHPUnit here.

TDD and BDD PHP tools

phpspec

phpspec is another tool for writing a clean PHP code with the BDD approach in mind. Designed for the spec level (SpecBDD), the tool incorporates the following algorithm:  you describe the behaviour of a feature you are going to create; write code for making this feature suitable with the specification; and refactor the code. Thet is all: fast and simple BDD PHP solution.

TDD and BDD PHP tools

Codeception

Codeception is another reliable BDD tool for PHP. It combines features of the aforementioned solutions, providing more seamless approach to behaviour-driven development in PHP. Codeception is like PHPUnit on steroids, since it includes everything out-of-the-box. Therefore, you will forget about headaches related to configuring Selenium, or writing XPaths with this tool. Moreover, it looks like Behat, but all its tests are written in PHP, making your development more flexible. You can even use variables and operators, as well as CSS and XPath locators in all your Codeception tests.

TDD and BDD PHP tools

These were the best tools for test-driven development and behavior-driven development in PHP. Between two software development methodologies, I’ve paid more attention to BDD, as it is more flexible and precise, but you can still utilize both, for example, with SimpleTest for TDD PHP coding. What other BDD solutions for PHP development would you recommend to other readers? What about TDD in PHP? Leave your comments below.