Mallow's Blog

An Introduction to Test Driven Development

Test-driven Development also called as test-driven design, is a method of implementing software programming that interfaces unit testing, programming and refactoring on the source code. Testing is an essential aspect of providing quality applications. Manual testing is tested only in limited ways. Making tests in depth is only achieved by writing automated test code.TDD consistently produces well-structured, resilient and easy to make changes to as business requirements. PHPUnit which is used to write automated test code is a programmer-oriented testing framework for PHP. Thus the importance of testing and its implementation and usage of PHPUnit is explained below.


Testing means revealing capabilities by putting them under strain; challenging. Testing is required to point out the bugs and defects that were made during development. In general, such tests had been written using Unit Test framework. In best practices testing is carried out via TDD(Test Driven Development).

Test Driven Development:

The process involved in TDD is simply said to be RGR (Red Green Refactor pattern).

  • Red for the failed test case.
  • Green for making test case work by adding code.
  • Refactor for restructuring existing code.

The flow of Test Driven Development:

  • The developer writes the automated test case for the desired new function.
  • Then produce the minimum amount of code to pass the test.
  • Refractor the produced code. (Process of restructuring existing code without changing its external behaviour)

Factors considered for writing testing code,

  • Valid inputs
  • Invalid inputs
  • Errors, exceptions, and events
  • Boundary conditions
  • Everything that might break


Testing in PHP is carried out with Unit Testing provided by PHPUnit.

        It is a programmer-oriented testing framework for PHP.

        It belongs to the family of XUnit test libraries.

        It is created by Sebastian Bergmann.

Running an automated unit test is much faster than manual testing. The name itself describes that Unit-Testing. A Unit is the smallest part of an application, it might be a set of code or function or class. Testing those units provides the term called Unit Testing.

Whenever you make changes to the code, just run those set of tests and watch if anything fails. If everything passes, then it’s assured that you haven’t broken any functionality of your application. A simple example is mentioned below

PHPUnit generates output as

 All we know that testing helps bug fixing and verifying the proper execution of code. But how we know that all our codes are tested by the given set of unit tests. Here comes the tool called Code Coverage.

Code Coverage – PHPUnit:

Testing improves the quality and predictability of an application. But adding more test cases doesn’t mean that the application code has better quality. Sometimes you may end up with situations like testing the same code over and over again while missing an important piece of code entirely.

Code Coverage report helps the developer to make a quick analysis of codes which are covered and uncovered by Unit Testing.

Metrics for Code Coverage,

        Line Coverage checks whether each executable line was executed.

        Function and Method Coverage checks whether each function or method was covered. * PHPUnit Code coverage considers a function or method is covered only when all of its lines are executed.

        Class and Trait Coverage checks whether each method of a class or trait was covered. * PHPUnit Code coverage considers a class or trait is covered only when all of its methods are executed.

        Other coverages are Opcode Coverage, Branch Coverage and Path Coverage. But these coverages are not supported by PHPUnit Code coverage.

For additional Reference:


Thus the apps built with TDD tends to have less duplication, fewer edge cases and a better overall architecture. There is no better way to save money and time while making sure that you have a codebase that is easy to maintain, extend and resistant to change. Will catch you soon with a detailed explanation for RGR pattern in next blog.

Kumaravel K,
PHP Development team,
Mallow Technologies.

Leave a Reply

%d bloggers like this: